Reputation: 109
I want to create a simple encryption algorithm but I couldn't do it yet. When I run this program, it was typing on screen
"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];
}
Upvotes: 1
Views: 82
Reputation: 121347
You are printing the same buffer that's been encrypted and decrypted before you print. So either make a copy of the encrypted string or print them in steps to see the process:
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);
Upvotes: 3