如何用C实现3DES算法..

Python013

如何用C实现3DES算法..,第1张

//功能:实现DES及3DES加解密的算法

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include "des.h"

//函数声明

int Do_DES(char* strSrc, char* strKey, char* strDest, char flag)

int Do_3DES(char* strSrc, char* strKey, char* strDest, char flag)

//主函数

int main(int argc, char** argv)

{

char src16[16+1],key16[16+1],key48[48+1],dest16[16+1]

if(argc != 3)

{

fprintf(stderr,"Usage: [%s -e|-d s|3]\n",argv[0])

exit(1)

}

if(strcmp(argv[2],"-s") == 0)

{

if(strcmp(argv[1],"-e") == 0)

{

fprintf(stderr,"Please input the string that you want to encrypt(16 hex number):\n")

memset(src16,0,sizeof(src16))

scanf("%s",src16)

fprintf(stderr,"Please input the Key string(16 hex number):\n")

memset(key16,0,sizeof(key16))

scanf("%s",key16)

memset(dest16,0,sizeof(dest16))

Do_DES(src16,key16,dest16,'e')

fprintf(stderr,"Result: [%s]\n",dest16)

}

else if(strcmp(argv[1],"-d") == 0)

{

fprintf(stderr,"Please input the string that you want to decrypt(16 hex number):\n")

memset(src16,0,sizeof(src16))

scanf("%s",src16)

fprintf(stderr,"Please input the Key string(16 hex number):\n")

memset(key16,0,sizeof(key16))

scanf("%s",key16)

memset(dest16,0,sizeof(dest16))

Do_DES(src16,key16,dest16,'d')

fprintf(stderr,"Result: [%s]\n",dest16)

}

else

return -1

}

else if(strcmp(argv[2],"-3") == 0)

{

if(strcmp(argv[1],"-e") == 0)

{

fprintf(stderr,"Please input the string that you want to encrypt(16 hex number):\n")

memset(src16,0,sizeof(src16))

scanf("%s",src16)

fprintf(stderr,"Please input the Key string(16 hex number):\n")

memset(key48,0,sizeof(key48))

scanf("%s",key48)

memset(dest16,0,sizeof(dest16))

Do_3DES(src16,key48,dest16,'e')

fprintf(stderr,"Result: [%s]\n",dest16)

}

else if(strcmp(argv[1],"-d") == 0)

{

fprintf(stderr,"Please input the string that you want to decrypt(16 hex number):\n")

memset(src16,0,sizeof(src16))

scanf("%s",src16)

fprintf(stderr,"Please input the Key string(16 hex number):\n")

memset(key48,0,sizeof(key48))

scanf("%s",key48)

memset(dest16,0,sizeof(dest16))

Do_3DES(src16,key48,dest16,'d')

fprintf(stderr,"Result: [%s]\n",dest16)

}

else

return -1

}

else

return -1

return 0

}

//做DES加密或解密运算

int Do_DES(char* strSrc, char* strKey, char* strDest, char flag)

{

int i,j

unsigned char subKey[16][48+1],byte8[8+1],bits[64+1],strTmp[64+1]

unsigned char L0[32+1],R0[32+1],Lx[32+1],Rx[32+1]

if(!( flag == 'e' || flag == 'E' || flag == 'd' || flag == 'D'))

return -1

if(strSrc == NULL || strKey == NULL)

return -2

if(flag == 'e' || flag == 'E')

{

memset(byte8,0,sizeof(byte8))

BCDToByte(strKey, 16, byte8)

memset(bits,0,sizeof(bits))

ByteToBit(byte8, 8, bits)

Des_GenSubKey(bits,subKey)

BCDToByte(strSrc, 16, byte8)

ByteToBit(byte8, 8, bits)

Des_IP(bits, strTmp)

memcpy(L0,strTmp,32)

memcpy(R0,strTmp+32,32)

for(i=0i<16i++)

{

memcpy(Lx,R0,32)

Des_F(R0,subKey[i],Rx)

Do_XOR(L0,32,Rx)

memcpy(L0,Lx,32)

memcpy(R0,Rx,32)

}

memcpy(bits,R0,32)

memcpy(bits+32,L0,32)

Des_IP_1(bits,strTmp)

BitToByte(strTmp,64,byte8)

ByteToBCD(byte8,8,strDest)

}

else

{

memset(byte8,0,sizeof(byte8))

BCDToByte(strKey, 16, byte8)

memset(bits,0,sizeof(bits))

ByteToBit(byte8, 8, bits)

Des_GenSubKey(bits,subKey)

BCDToByte(strSrc, 16, byte8)

ByteToBit(byte8, 8, bits)

Des_IP(bits, strTmp)

memcpy(L0,strTmp,32)

memcpy(R0,strTmp+32,32)

for(i=0i<16i++)

{

memcpy(Lx,R0,32)

Des_F(R0,subKey[15-i],Rx)

Do_XOR(L0,32,Rx)

memcpy(L0,Lx,32)

memcpy(R0,Rx,32)

}

memcpy(bits,R0,32)

memcpy(bits+32,L0,32)

Des_IP_1(bits,strTmp)

BitToByte(strTmp,64,byte8)

ByteToBCD(byte8,8,strDest)

}

return 0

}

//做3DES加密或解密运算

int Do_3DES(char* strSrc, char* strKey, char* strDest, char flag)

{

unsigned char strBCDKey[32+1],strByteKey[16+1]

unsigned char strMidDest1[16+1],strMidDest2[16+1]

unsigned char strLKey[16+1],strMKey[16+1],strRKey[16+1]

if(!( flag == 'e' || flag == 'E' || flag == 'd' || flag == 'D'))

return -1

if(strSrc == NULL || strKey == NULL)

return -2

if(strlen(strKey) <32)

return -3

if(flag == 'e' || flag == 'E')

{

memset(strBCDKey,0,sizeof(strBCDKey))

memcpy(strBCDKey,strKey,32)

memset(strLKey,0,sizeof(strLKey))

memcpy(strLKey,strBCDKey,16)

memset(strRKey,0,sizeof(strRKey))

memcpy(strRKey,strBCDKey+16,16)

Do_DES(strSrc,strLKey,strMidDest1,'e')

Do_DES(strMidDest1,strRKey,strMidDest2,'d')

Do_DES(strMidDest2,strLKey,strMidDest1,'e')

memcpy(strDest,strMidDest1,16)

}

else

{

memset(strBCDKey,0,sizeof(strBCDKey))

memcpy(strBCDKey,strKey,32)

memset(strLKey,0,sizeof(strLKey))

memcpy(strLKey,strBCDKey,16)

memset(strRKey,0,sizeof(strRKey))

memcpy(strRKey,strBCDKey+16,16)

Do_DES(strSrc,strLKey,strMidDest1,'d')

Do_DES(strMidDest1,strRKey,strMidDest2,'e')

Do_DES(strMidDest2,strLKey,strMidDest1,'d')

memcpy(strDest,strMidDest1,16)

}

return 0

}

1、数据加密的基本过程就是对原来为明文的文件或数据按某种算法进行处理,使其成为不可读的一段代码,通常称为“密文”,使其只能在输入相应的密钥之后才能显示出本来内容,通过这样的途径来达到保护数据不被非法人窃取、阅读的目的。

2、常见加密算法

DES(Data Encryption Standard):数据加密标准,速度较快,适用于加密大量数据的场合;

3DES(Triple DES):是基于DES,对一块数据用三个不同的密钥进行三次加密,强度更高;

RC2和 RC4:用变长密钥对大量数据进行加密,比 DES 快;

IDEA(International Data Encryption Algorithm)国际数据加密算法:使用 128 位密钥提供非常强的安全性;

RSA:由 RSA 公司发明,是一个支持变长密钥的公共密钥算法,需要加密的文件块的长度也是可变的;

DSA(Digital Signature Algorithm):数字签名算法,是一种标准的 DSS(数字签名标准);

AES(Advanced Encryption Standard):高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法;

BLOWFISH,它使用变长的密钥,长度可达448位,运行速度很快;

其它算法,如ElGamal、Deffie-Hellman、新型椭圆曲线算法ECC等。

比如说,MD5,你在一些比较正式而严格的网站下的东西一般都会有MD5值给出,如安全焦点的软件工具,每个都有MD5。

3、例程:

#include<stdio.h>

#include<process.h>

#include<conio.h>

#include<stdlib.h>

#define maxim 65537

#define fuyi 65536

#define one 65536

#define round 8

unsigned int inv(unsigned int xin)

unsigned int mul(unsigned int a,unsigned int b)

void cip(unsigned int IN[4],unsigned int OUT[4],unsigned int Z[7][10])

void key(unsigned int uskey[9],unsigned int Z[7][10])

void de_key(unsigned int Z[7][10],unsigned int DK[7][10])

void main()

 int i,j,k,x

    unsigned int Z[7][10],DK[7][10],XX[5],TT[5],YY[5]

    unsigned int uskey[9]

FILE *fpout,*fpin

printf("\n Input Key")

for(i=1i<=8i++)

    scanf("%6u",&uskey[i])

for(i=0i<9i++)

    uskey[i]=100+i*3

    key(uskey,Z)/*产生加密子密钥*/

    de_key(Z,DK)/*计算解密子密钥*/

    if((fpin=fopen("ekey.txt","w"))==NULL)

    {

    printf("cannot open file!")

    exit(EXIT_FAILURE)

    }

    for(i=0i<7i++)

    {

    for(j=0j<10j++)

    fprintf(fpin,"%6u",Z[i][j])

    fprintf(fpin,"\n")

    }

    fclose(fpin)

    

    /*XX[1..5]中为明文*/

    for(i=0i<4i++) XX[i]=2*i+101

    clrscr()

    printf("Ming wen %6u %6u %6u %6u \n",XX[0],XX[1],XX[2],XX[3])

    if((fpin=(fopen("ideaming.txt","w")))==NULL)

    {printf("cannot open file!")

    exit(EXIT_FAILURE)

    }

    fprintf(fpin,"%6u,%6u,%6u,%6u \n",XX[0],XX[1],XX[2],XX[3])

    fclose(fpin)

    for(i=1i<=30000i++)

    cip(XX,YY,Z)/*用密钥Z加密XX中的明文并存在YY中*/

    printf("\n\n Mingwen %6u %6u %6u %6u \n",YY[0],YY[1],YY[2],YY[3])

 if((fpin=fopen("ideamiwn.txt","w"))==NULL)

 {

 printf("cannot open file!")

    exit(EXIT_FAILURE)

    }

    fprintf(fpout,"%6u %6u %6u %6u\n",YY[0],YY[1],YY[2],YY[3])

    {

    printf("cannot open file!")

    exit(EXIT_FAILURE)

    }

    fprintf(fpout,"%6u %6u %6u %6u \n",YY[0],YY[1],YY[2],YY[3])

    fclose(fpout)

    for(i=1i<=30000i++)

    cip(YY,TT,DK)/*encipher YY to TT with Key DK*/

    printf("\n Jie Mi %6u %6u %6u %6u \n",TT[0],TT[1],TT[2],TT[3])

    if((fpout=fopen("dideaout.txt","w"))==NULL)

    {

    printf("cannot open file!")

    exit(EXIT_FAILURE)

    }

    fprintf(fpout,"%6u %6u %6u %6u \n",TT[0],TT[1],TT[2],TT[3])

    fclose(fpout)

    }

    /* 此函数执行IDEA算法中的加密过程*/

    

    void cip(unsigned int IN[4],unsigned int OUT[4],unsigned int Z[7][10])

    {

    unsigned int r,x1,x2,x3,x4,kk,t1,t2,a

    x1=IN[0]x2=IN[1]x3=IN[2]x4=IN[3]

    for(r=1r<=8r++)

    {

    /* 对64位的块进行分组运算*/

    x1=mul(x1,Z[1][r])x4=mul(x4,Z[4][r])

    x2=x2+Z[2][r]&onex3=(x3+Z[3][r])&one

    /* MA结构的函数 */

    kk=mul(Z[5][r],(x1^x3))

    t1=mul(Z[6][r],(kk+(x2^x4))&one

    /* 随机变换PI*/

    x1=x1^t1x4=x4^t2a=x2^t2x2=x3^t1x3=a

    }

    /* 输出转换*/

    OUT[0]=mul(x1,Z[1][round+1])

    OUT[3]=mul(x4,Z[1][round+1])

    OUT[1]=(x3+Z[2][round+1])&one

    OUT[2]=(x2+Z[3][round+1])&one

    }

    

    /* 用高低算法上实现乘法运算*/

    unsigned int mul(unsigned int a,unsigned int b)

    {

    long int p

    long unsigned q

    if(a==0) p=maxim-b

    else if(b==0) p=maxim-a

    else

    {

    q=(unsigned long)a*(unsigned long)b

    p=(q&one)-(q>>16)

    if(p<=0) p=p+maxim

    {

    return (unsigned) (p&one)

    }

    

    /*通过Euclidean gcd算法计算xin的倒数*/

    unsigned int inv(unsigned int xin)

    {

    long n1,n2,q,r,b1,b2,t

    if(xin==0)

    b2=0

    else

    {n1=maximn2=xinb2=1b1=0

    do{

    r=(n1%n2)q=(n1-r)/n2

    if(r==0)

    if(b2<0) b2=maxim+b2

    else

    {n1=n2n2=r

    t=b2

    b2=b1-q*b2b1=t

    }

    }while(r!=0)

    }

    return (unsigned long int)b2

    }

/*产生加密子密钥Z*/

    void key(unsigned int uskey[9],unsigned int Z[7][10])

    {

    unsigned int S[54]

    int i,j,r

    for(i=1i<9i++)

    S[i-1]=uskey[i]

    /* shifts */

    for(i=8i<54i++)

    {

    if(i+2)%8==0)/* 对于S[14],S[22],...进行计算 */

    S[i]=((S[i-7]<<0)^(S[i-14]>>7)&one

    else if((i+1)%8==0)/* 对于S[15],S[23],...进行计算 */

    S[i]=((S[i-15]<<9)^(S[i-14]>>7)&one

    else

    S[i]=((S[i-7]<<9)^(S[i-6]>>7)&one

    }

    /*取得子密钥*/

    for(r=1r<=round+1r++)

      for(j=1j<7j++)

      Z[j][r]=S[6*(r-1)+j-1]

    }

    

    /* 计算解子密钥DK */

    void de_key(unsigned int Z[7][10],unsigned int DK[7][10])

    {

    int j

    for(j=1j<=round+1j++)

    {DK[1][round-j+2]=inv(Z[1][j])

    DK[4][round-j+2]=inv(Z[4][j])

    if(i==1|j==round+1)

    {

    DK[2][round-j+2]=(fuyi-Z[2][j])&one

    DK[3][round-j+2]=(fuyi-Z[3][j])&one

    }

    else

    {

    DK[2][round-j+2]=inv(Z[3][j])

    DK[3][round-j+2]=inv(Z[2][j])

    }

    }

    for(j=1j<=round+1j++)

    {

    DK[5][round-j+2]=inv(Z[5][j])

    DK[6][round-j+2]=inv(Z[6][j])

    }

    

    }