求助,关于3DES加解密

hnzlk 2015-02-12 05:26:18
已经实现了安桌平台,ios平台的3DES加解密,现在要用C++在windows平台上实现。现在用openssl,但是怎么试结果都跟ios、安桌平中结果对不上。求解。
ios平台和安桌平台的加密是参考
http://blog.csdn.net/lyq8479/article/details/8062867

这是我用openssl的代码:


char key[] = "liuyunqiang@lx100$#365#$";
unsigned char block_key[9];
DES_key_schedule ks,ks2,ks3;


memcpy(block_key, key + 0, 8);
DES_set_key_checked((const_DES_cblock*)block_key, &ks);
memcpy(block_key, key + 8, 8);
DES_set_key_checked((const_DES_cblock*)block_key, &ks2);
memcpy(block_key, key + 16, 8);
DES_set_key_checked((const_DES_cblock*)block_key, &ks3);

char iv[] = "01234567";
DES_cblock cblock;
memcpy(cblock, iv, sizeof(iv));

//DES_cblock cblock = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };

char string[] = "123";
// ---------------------------------------------
// I use sizeof instead of strlen because I want
// to count the '\0' at the end, strlen would
// not count it

int stringLen=sizeof(string);

char* cipher = new char[32];
char* text = new char[stringLen];

memset(cipher,0,32);
memset(text,0,stringLen);

DES_set_odd_parity(&cblock);

DES_ede3_cbc_encrypt((const unsigned char*)string,
(unsigned char*)cipher,
stringLen, &ks, &ks2, &ks3,
&cblock, DES_ENCRYPT);
printf("Encrypted : %32.32s\n",cipher);
int len = strlen(cipher);
for(int i=0;i<len;i++) {

NSLog(@"%02x",cipher<i>);
}

NSData *myData = [NSData dataWithBytes:(const void *)cipher length:strlen(cipher)];
NSString *result = [GTMBase64 stringByEncodingData:myData];
NSLog(@"result:%@",result);

...全文
192 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
hnzlk 2015-02-25
  • 打赏
  • 举报
回复
非常感谢!谢谢!
用户 昵称 2015-02-13
  • 打赏
  • 举报
回复
你加密的结果是多少?你的结果转化出来的字符串是什么样的,大写还是小写?你base64的结果是什么?
用户 昵称 2015-02-13
  • 打赏
  • 举报
回复
//3des24_encode_cbc ( ansi_string ( "01234567" ) , ansi_string ( "123" ) , ansi_string ( "liuyunqiang@lx100$#365#$" ) ) //----ansi_string convert utf16-little-endian format input data to ansi format //---- input = 30 00 31 00 32 00 33 00 34 00 35 00 36 00 37 00 //---- output = 30 31 32 33 34 35 36 37 // //----ansi_string convert utf16-little-endian format input data to ansi format //---- input = 31 00 32 00 33 00 //---- output = 31 32 33 // //----ansi_string convert utf16-little-endian format input data to ansi format //---- input = 6C 00 69 00 75 00 79 00 75 00 6E 00 71 00 69 00 61 00 6E 00 67 00 40 00 6C 00 78 00 31 00 30 00 30 00 24 00 23 00 33 00 36 00 35 00 23 00 24 00 //---- output = 6C 69 75 79 75 6E 71 69 61 6E 67 40 6C 78 31 30 30 24 23 33 36 35 23 24 // //--triple(24k) des cbc encrypt //--key = 6C 69 75 79 75 6E 71 69 61 6E 67 40 6C 78 31 30 30 24 23 33 36 35 23 24 //--icv = 30 31 32 33 34 35 36 37 plain = 31 32 33 00 00 00 00 00 //--xor = 01 03 01 33 34 35 36 37 cipher = 5A 68 5A 2C 8D 73 23 70 // //-----Final result --- 5A 68 5A 2C 8D 73 23 70 //
用户 昵称 2015-02-13
  • 打赏
  • 举报
回复
//base64_encode ( 2F 13 1D BE FD 5F 42 77 ) //--base64 encode make data to string //-- input = 2F 13 1D BE FD 5F 42 77 //-- output = 4C 00 78 00 4D 00 64 00 76 00 76 00 31 00 66 00 51 00 6E 00 63 00 3D 00 // //-----Final result --- LxMdvv1fQnc= // base64也蒙对了,就对16进制数进行操作就行了,你只要注意加密数据的格式就行了。
用户 昵称 2015-02-13
  • 打赏
  • 举报
回复
//3des24_encode_cbc ( ansi_string ( "01234567" ) , ansi_string ( "123" ) 0505050505 , ansi_string ( "liuyunqiang@lx100$#365#$" ) ) //----ansi_string convert utf16-little-endian format input data to ansi format //---- input = 30 00 31 00 32 00 33 00 34 00 35 00 36 00 37 00 //---- output = 30 31 32 33 34 35 36 37 // //----ansi_string convert utf16-little-endian format input data to ansi format //---- input = 31 00 32 00 33 00 //---- output = 31 32 33 // //----ansi_string convert utf16-little-endian format input data to ansi format //---- input = 6C 00 69 00 75 00 79 00 75 00 6E 00 71 00 69 00 61 00 6E 00 67 00 40 00 6C 00 78 00 31 00 30 00 30 00 24 00 23 00 33 00 36 00 35 00 23 00 24 00 //---- output = 6C 69 75 79 75 6E 71 69 61 6E 67 40 6C 78 31 30 30 24 23 33 36 35 23 24 // //--triple(24k) des cbc encrypt //--key = 6C 69 75 79 75 6E 71 69 61 6E 67 40 6C 78 31 30 30 24 23 33 36 35 23 24 //--icv = 30 31 32 33 34 35 36 37 plain = 31 32 33 05 05 05 05 05 //--xor = 01 03 01 36 31 30 33 32 cipher = 2F 13 1D BE FD 5F 42 77 // //-----Final result --- 2F 13 1D BE FD 5F 42 77 // 加密格式已经蒙对了。
用户 昵称 2015-02-13
  • 打赏
  • 举报
回复
//3des24_decode_cbc ( ansi_string ( "01234567" ) , 5f bf d8 5d d5 79 6b 7a 9b a8 4e dd f1 3e 51 14 , ansi_string ( "liuyunqiang@lx100$#365#$" ) ) //----ansi_string convert utf16-little-endian format input data to ansi format //---- input = 30 00 31 00 32 00 33 00 34 00 35 00 36 00 37 00 //---- output = 30 31 32 33 34 35 36 37 // //----ansi_string convert utf16-little-endian format input data to ansi format //---- input = 6C 00 69 00 75 00 79 00 75 00 6E 00 71 00 69 00 61 00 6E 00 67 00 40 00 6C 00 78 00 31 00 30 00 30 00 24 00 23 00 33 00 36 00 35 00 23 00 24 00 //---- output = 6C 69 75 79 75 6E 71 69 61 6E 67 40 6C 78 31 30 30 24 23 33 36 35 23 24 // //--triple(24k) des cbc decrypt //--key = 6C 69 75 79 75 6E 71 69 61 6E 67 40 6C 78 31 30 30 24 23 33 36 35 23 24 //--icv = 30 31 32 33 34 35 36 37 cipher = 5F BF D8 5D D5 79 6B 7A //-- plain = 01 03 01 52 56 56 02 02 xor = 31 32 33 61 62 63 34 35 //--icv = 5F BF D8 5D D5 79 6B 7A cipher = 9B A8 4E DD F1 3E 51 14 //-- plain = 69 DA BE 3A D1 7D 6F 7E xor = 36 65 66 67 04 04 04 04 // //-----Final result --- 31 32 33 61 62 63 34 35 36 65 66 67 04 04 04 04 // //to_ansi_string ( 31 32 33 61 62 63 34 35 36 65 66 67 04 04 04 04 ) //--to_ansi_string convert ansi format input data to string //-- input = 31 32 33 61 62 63 34 35 36 65 66 67 04 04 04 04 //-- output = "123abc456efg" // //-----Final result --- "123abc456efg" // 这是你的字符串,解密之后的结果,可以看出,后面补了4个04,所以如果加密123的话,后面可能补5个05
hnzlk 2015-02-13
  • 打赏
  • 举报
回复
加密前字符:123abc456efg base64加密后结果:X7/YXdV5a3qbqE7d8T5RFA== 加密后十六进制: 5f bf d8 5d d5 79 6b 7a 9b a8 4e dd f1 3e 51 14 非常感谢!
hnzlk 2015-02-13
  • 打赏
  • 举报
回复
字符串:123 加密后base64:LxMdvv1fQnc= key:liuyunqiang@lx100$#365#$ iv:01234567 加密后十六进制: 2f 13 1d be fd 5f 42 77

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧