简介:原理和des算法一样,指示将秘钥分成3串64位秘钥,对64位数据进行进行处理,加密过程:加密->解密->加密,揭秘过程:解密->加密->解密。秘钥又分双倍长秘钥和三倍长秘钥,三倍长秘钥192位顾名思义直接拆成3个64位秘钥,而双倍长秘钥对半分成L和R,k1=L,k2=R,k3=L来进行处理。
这里采用ecb模式进行加解密,只贴出main函数,其他和上一篇des流程一样,只是修改main的参数和调用加解密函数的顺序和次数。
上一篇des加解密:/jiujiederoushan/article/details/100008198
main函数:
int main(int argc,char **argv){//ECB模式if(argc != 4){printf("Usage: des_tool [1/2/3] [key] [input]\n(1: [encrypt] char16 key , char8 data)\n(2: [encrypt] hex32 key , hex16 data)\n(3: [decrypt] hex32 key , hex16 data)\n");return -1;}if(strcmp(argv[1] , "1") != 0 && strcmp(argv[1] , "2") != 0 && strcmp(argv[1] , "3") != 0){printf("Usage: des_tool [1/2/3] [key] [input]\n(1: [encrypt] char16 key , char8 data)\n(2: [encrypt] hex32 key , hex16 data)\n(3: [decrypt] hex32 key , hex16 data)\n");return -1;}if(strcmp(argv[1] , "1") == 0 && (strlen(argv[2]) != 16 || strlen(argv[3]) != 8)){printf("Usage: des_tool [1/2/3] [key] [input]\n(1: [encrypt] char16 key , char8 data)\n(2: [encrypt] hex32 key , hex16 data)\n(3: [decrypt] hex32 key , hex16 data)\n");return -1;}else if(strcmp(argv[1] , "1") != 0 &&(strlen(argv[2]) != 32 || strlen(argv[3]) != 16)){printf("Usage: des_tool [1/2/3] [key] [input]\n(1: [encrypt] char16 key , char8 data)\n(2: [encrypt] hex32 key , hex16 data)\n(3: [decrypt] hex32 key , hex16 data)\n");return -1;}int ilRet = 0;int ilType = 0;unsigned char alKey1[8+1] = {0};unsigned char alKey2[8+1] = {0};unsigned char alKey3[8+1] = {0};unsigned char alHexKey1[16+1] = {0};unsigned char alHexKey2[16+1] = {0};unsigned char alHexKey3[16+1] = {0};unsigned char alInput[8+1] = {0};unsigned char alOutput[8+1] = {0};int ilKeyLen = 0;int ilInputLen = 0;int ilOutputLen = 0;ilType = atoi(argv[1]);if(ilType == 1){//可见明文,可见密码,加密memcpy((char *)alKey1 , argv[2] , 8);memcpy((char *)alKey2 , argv[2]+8 , 8);memcpy((char *)alKey3 , argv[2] , 8);strcpy((char *)alInput , argv[3]);if(proc_enc_dec(alKey1 , ilKeyLen , alInput , ilInputLen , alOutput , &ilOutputLen , _ENC_OPT_))return -1;memcpy(alInput , alOutput , 8);if(proc_enc_dec(alKey2 , ilKeyLen , alInput , ilInputLen , alOutput , &ilOutputLen , _DEC_OPT_))return -1;memcpy(alInput , alOutput , 8);if(proc_enc_dec(alKey3 , ilKeyLen , alInput , ilInputLen , alOutput , &ilOutputLen , _ENC_OPT_))return -1;}else if(ilType == 2){memcpy((char *)alHexKey1 , argv[2] , 16);memcpy((char *)alHexKey2 , argv[2]+16 , 16);memcpy((char *)alHexKey3 , argv[2] , 16);//参数16进制明文和密码,加密if(hex_str2bytes(alHexKey1 , alKey1 , &ilKeyLen))return -1;if(hex_str2bytes(alHexKey2 , alKey2 , &ilKeyLen))return -1;if(hex_str2bytes(alHexKey3 , alKey3 , &ilKeyLen))return -1;if(hex_str2bytes((unsigned char *)argv[3] , alInput, &ilInputLen))return -1;if(proc_enc_dec(alKey1 , ilKeyLen , alInput , ilInputLen , alOutput , &ilOutputLen , _ENC_OPT_))return -1;memcpy(alInput , alOutput , 8);if(proc_enc_dec(alKey2 , ilKeyLen , alInput , ilInputLen , alOutput , &ilOutputLen , _DEC_OPT_))return -1;memcpy(alInput , alOutput , 8);if(proc_enc_dec(alKey3 , ilKeyLen , alInput , ilInputLen , alOutput , &ilOutputLen , _ENC_OPT_))return -1;}else if(ilType == 3){memcpy((char *)alHexKey1 , argv[2] , 16);memcpy((char *)alHexKey2 , argv[2]+16 , 16);memcpy((char *)alHexKey3 , argv[2] , 16);//参数16进制密文和密码,解密if(hex_str2bytes(alHexKey1 , alKey1 , &ilKeyLen))return -1;if(hex_str2bytes(alHexKey2 , alKey2 , &ilKeyLen))return -1;if(hex_str2bytes(alHexKey3 , alKey3 , &ilKeyLen))return -1;if(hex_str2bytes((unsigned char *)argv[3] , alInput, &ilInputLen))return -1;if(proc_enc_dec(alKey1 , ilKeyLen , alInput , ilInputLen , alOutput , &ilOutputLen , _DEC_OPT_))return -1;memcpy(alInput , alOutput , 8);if(proc_enc_dec(alKey2 , ilKeyLen , alInput , ilInputLen , alOutput , &ilOutputLen , _ENC_OPT_))return -1;memcpy(alInput , alOutput , 8);if(proc_enc_dec(alKey3 , ilKeyLen , alInput , ilInputLen , alOutput , &ilOutputLen , _DEC_OPT_))return -1;}else{printf("Invalid args!");return -2;}return 0;}
./3des_tool 2 31323334353637383131313131313131 6161616161616161
加密结果:219a0a8f171d886d
./3des_tool 3 31323334353637383131313131313131 219a0a8f171d886d
解密结果:6161616161616161