gpt4 book ai didi

c - C 中的简单加密函数

转载 作者:太空宇宙 更新时间:2023-11-04 01:55:50 25 4
gpt4 key购买 nike

我想创建一个简单的加密算法,但我还做不到。当我运行这个程序时,它在屏幕上打字

"Name: John Nash, Cryptioned Data: John Nash, Decryptioned Data: John Nash"

How can I solve this problem? Where am I making a mistake?

#include<stdio.h>


char *ecrypt(char data[]);
char *decrypt(char data[]);

int i; // Global variable...

void main(void)
{
char name[] = "John Nash",*data_encryptioned,*data_decryption;
data_encryptioned = ecrypt(name);
data_decryption = decrypt(data_encryptioned);
printf("Name: %s, Cryptioned Data: %s, Decryptioned Data: %s\n",name,data_encryptioned,data_decryption);
}

char *ecrypt(char data[])
{
for(i=0;data[i]!='\0';i++)
{
data[i]+=i+12;
}
return &data[0];
}

char *decrypt(char data[])
{
for(i=0;data[i]!='\0';i++)
{
data[i]-=(i+12);
}
return &data[0];
}

最佳答案

您正在打印在打印前加密和解密的相同缓冲区。因此,要么制作加密字符串的副本,要么分步打印它们以查看过程:

    printf("%s\n", name);
data_encryptioned = ecrypt(name);
printf("Cryptioned Data: %s\n",data_encryptioned);
data_decryption = decrypt(data_encryptioned);
printf("Decryptioned Data: %s\n",data_decryption);

关于c - C 中的简单加密函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34464920/

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