0

我找到了一个关于 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);    
}    
------------------------------------------------    
4

1 回答 1

0

这条线

encrypted=(char*)malloc(sizeof(clear));  

不按你的想法做。在 32 位系统上,sizeof(clear) 将为 4,因为它是指针的大小,而不是指向的数据的长度。因此,您可能只加密/解密 4 个字节,而您正在打印出这 4 个字节加上一些垃圾。

于 2011-11-30T16:23:27.347 回答