我找到了一个关于OPENSSL DES的例子,当我将这个例子应用到Objective-C程序时,解密的文本不等于我输入的内容。
textField.text是输入文本框
任何人都可以帮助我吗?非常感谢!!!
例如,
when I input "test", the decrypted text ="test&\264"
when I input "Enter an Text here", the decrypted text ="Ente"
when I input "1234567890", the decrypted text ="1234h&\311"
//ENCRYPTION
char* desen(char *clear, int size)
{
printf("Encrypted text\t %s \n",clear);
char *encrypted;
char key[]="password";
encrypted=(char*)malloc(sizeof(clear));
static char* Res;
int n=0;
DES_cblock Key2;
DES_key_schedule schedule;
Res = ( char * ) malloc( sizeof(clear) );
// Prepare the key for use with DES_cfb64_encrypt /
memcpy( Key2, key,8);
DES_set_odd_parity( &Key2 );
DES_set_key_checked( &Key2, &schedule );
// Encryption occurs here /
DES_cfb64_encrypt( ( unsigned char * ) clear, ( unsigned char * ) Res,
sizeof(clear), &schedule, &Key2, &n, DES_ENCRYPT );
memcpy(encrypted,Res, sizeof(clear));
printf("Key:%s\n",encrypted);
return encrypted;
}
//------------------------------------------------
//DECRYPTION-------------------------------
char* desde(char *clear, int size)
{
char *decrypted;
char key[]="password";
decrypted=(char*)malloc(sizeof(clear));
static char* Res;
int n=0;
DES_cblock Key2;
DES_key_schedule schedule;
Res = ( char * ) malloc( sizeof(clear) );
// Prepare the key for use with DES_cfb64_encrypt /
memcpy( Key2, key,8);
DES_set_odd_parity( &Key2 );
DES_set_key_checked( &Key2, &schedule );
// Encryption occurs here /
DES_cfb64_encrypt( ( unsigned char * ) clear, ( unsigned char * ) Res,
sizeof(clear), &schedule, &Key2, &n, DES_DECRYPT );
memcpy(decrypted,Res, sizeof(clear));
printf("Key:%s\n",decrypted);
return decrypted;
}
//------------------------------------------------
//----------Button------------------------------
- (IBAction)calculateDES_ENCRYPT:(id)sender
{
char key[]="password";
char *en;
char *de;
NSString *string = textField.text;
const char *temp=[string fileSystemRepresentation];
int len=strlen(temp);
char clear[len+1];
//char clear[50];
strcpy(clear,temp);
en=desen( clear,len+1);
de= desde(en, len+1);
}
------------------------------------------------
答案 0 :(得分:0)
这一行
encrypted=(char*)malloc(sizeof(clear));
不符合你的想法。在32位系统上,sizeof(clear)将为4,因为它是指针的大小,而不是指向的数据的长度。所以你可能只加密/解密4个字节,你打印出这4个字节加上一些垃圾。