16,548
社区成员




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);