IPHONE Objective-C程序使用OPENSSL执行DES加密
我发现了一个关于OPENSSL DES的例子,当我将这个例子应用到Objective-C程序中时,解密的文本并不等于我输入的文本 textField.text是输入文本框 有人能帮我吗?非常感谢 例如IPHONE Objective-C程序使用OPENSSL执行DES加密,iphone,objective-c,openssl,des,Iphone,Objective C,Openssl,Des,我发现了一个关于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
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);
}
------------------------------------------------
这条线
encrypted=(char*)malloc(sizeof(clear));
不做你想做的事。在32位系统上,sizeof(clear)将为4,因为它是指针的大小,而不是指向的数据的长度。因此,您可能只加密/解密4个字节,并打印出这4个字节和一些垃圾 谢谢JeremyP,哪个函数可以代替“sizeof”,因为我是C语言的新手。如果你想加密整个字符串,包括它的终止空字符,你需要
strlen(clear)+1
。请注意,结果是一个字节数组,而不是以C null结尾的字符串。在21世纪,绝对不应该使用DES。