gpt4 book ai didi

C基本编程概念

转载 作者:行者123 更新时间:2023-12-04 06:39:19 25 4
gpt4 key购买 nike

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

最佳答案

不要使用全局变量 - 而是使用 free()一旦调用代码 ( main() ) 处理结果后返回的指针。

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

这样想——malloc()做同样的事情(除了它分配内存而不是填充它)。怎么可能malloc()使用调用代码可访问的全局变量更清晰?

关于C基本编程概念,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4469663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com