IPHONE Objective-C 程序使用 OPENSSL 执行 DES 加密

发布于 2024-12-19 02:42:22 字数 2738 浏览 0 评论 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);    
}    
------------------------------------------------    

I have found an example about OPENSSL DES, when I applied this example into Objective-C program, the decrypted text was not equal to what I have input.

textField.text is the input textbox

Can anyone help me? Many thanks!!!

for e.g,

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱她像谁 2024-12-26 02:42:22

这条线

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

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

This line

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

doesn't do what you think. On a 32 bit system, sizeof(clear) will be 4 because it is the size of the pointer, not the length of the data pointed to. So you are probably only encrypting/decrypting 4 bytes and you are printing out those 4 bytes plus some garbage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文