C基本编程概念

时间:2010-12-17 10:40:19

标签: c

我是C编程的新手,我正在编写一个程序来进行3DES加密。

但是这个代码中存在一些基本错误,例如在函数中执行malloc而不是取消分配。有人可以通过使用全局变量然后解除分配来帮助我重写这个吗?我想优化这段代码。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/des.h>
#include <openssl/rand.h>

#define BUFSIZE 128 


char *
Encrypt( char *Key, char *Msg, int size)
{

        static char*    Res;
        unsigned char in[BUFSIZE], out[BUFSIZE], back[BUFSIZE];
        unsigned char *e = out;
        char buffer[21]="";
        char *pbuffer = buffer;
        int len;

        DES_cblock key1, key2, key3;
        DES_cblock seed = {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10};
        DES_cblock ivsetup = {0xE1, 0xE2, 0xE3, 0xD4, 0xD5, 0xC6, 0xC7, 0xA8};
        DES_cblock ivec;
        DES_key_schedule ks1, ks2, ks3;

        memset(in, 0, sizeof(in));
        memset(out, 0, sizeof(out));
        memset(back, 0, sizeof(back));

        DES_string_to_key (Key, &key1);
        DES_string_to_key (Key, &key2);
        DES_string_to_key (Key, &key3);

        DES_set_key((C_Block *)key1, &ks1);
        DES_set_key((C_Block *)key2, &ks2);
        DES_set_key((C_Block *)key3, &ks3);

        strcpy(in, Msg);

        //printf("In Encrypt, Plaintext: [%s]\n", in);

        len = strlen(in);
        memcpy(ivec, ivsetup, sizeof(ivsetup));
        DES_ede3_cbc_encrypt(in, out, len, &ks1, &ks2, &ks3, &ivec, DES_ENCRYPT);

        //printf("In Encrypt, Ciphertext:");
        while (*e) 
        {
            //printf("%02x", *e);
            sprintf(pbuffer, "%02x", *e);
            pbuffer +=2;
            *e++;
        }
        //printf("\n");
        //printf("In Encrypt, Returning Text: [%s]\n", buffer);

        Res = ( char * ) malloc(sizeof(buffer));
        memcpy(Res, buffer, sizeof(buffer));
        return(( unsigned char * )Res);
}

char *
Decrypt( char *Key, char *Msg, int size)
{

        static char*    Res;

        unsigned char in[BUFSIZE], out[BUFSIZE], back[BUFSIZE];
        unsigned char *e = out;
        char buffer[21] = "";
        char *pbuffer = buffer;
        int len;

        DES_cblock key1, key2, key3;
        DES_cblock seed = {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10};
        DES_cblock ivsetup = {0xE1, 0xE2, 0xE3, 0xD4, 0xD5, 0xC6, 0xC7, 0xA8};
        DES_cblock ivec;
        DES_key_schedule ks1, ks2, ks3;

        memset(in, 0, sizeof(in));
        memset(out, 0, sizeof(out));
        memset(back, 0, sizeof(back));

        DES_string_to_key (Key, &key1);
        DES_string_to_key (Key, &key2);
        DES_string_to_key (Key, &key3);

        DES_set_key((C_Block *)key1, &ks1);
        DES_set_key((C_Block *)key2, &ks2);
        DES_set_key((C_Block *)key3, &ks3);

        strcpy(in, Msg);

        //printf("In Decrypt, Plaintext: [%s]\n", in);

        len = strlen(in);
        memcpy(ivec, ivsetup, sizeof(ivsetup));
        DES_ede3_cbc_encrypt(in, out, len, &ks1, &ks2, &ks3, &ivec, DES_DECRYPT);

        //printf("In Decrypt, Ciphertext:");
        while (*e) 
        {
            //printf("%02x", *e);
            sprintf(pbuffer, "%02x", *e);
            pbuffer +=2;
            *e++;
        }
        //printf("\n");
        //printf("In Decrypt, Returning Text: [%s]\n", buffer);

        Res = ( char * ) malloc(sizeof(buffer));
        memcpy(Res, buffer, sizeof(buffer));
        return(( unsigned char * )Res);
}

int main(void)
{
    char key[]="1234567890123456"; // 16
    char clear[]="Arun Das";
    char *decrypted;
    char *encrypted;

    printf("In Main, Plain text\t : %s \n",clear);
    encrypted=Encrypt(key,clear,sizeof(clear));
    decrypted=Decrypt(key,encrypted,sizeof(encrypted)); 
    printf("In Main, Encrypted text\t : %s \n",encrypted);
    printf("In Main, Decrypted text\t : %s \n",decrypted);
    system("PAUSE");
    exit(0);
}

1 个答案:

答案 0 :(得分:3)

不要使用全局变量 - 在调用代码(free())完成结果后,请在返回的指针上使用main()

您的实现非常好 - 两个函数都分配内存,将其所有权传递给调用代码。调用代码负责释放该内存。这简洁明了。引入全局变量会使情况变得更糟。

以这种方式思考 - malloc()做同样的事情(除了它分配内存而不填充内存)。使用调用代码可访问的全局变量如何更清楚地完成malloc()