C中的简单加密函数

时间:2015-12-25 17:25:58

标签: c encryption

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

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

我该如何解决这个问题?我在哪里弄错了?

#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];
}

1 个答案:

答案 0 :(得分:3)

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

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