Encryption and Decryption text in C programming are used to encrypt any text by using a user-provided key and decrypt text by using the user-provided key.
#include<stdio.h>
#include<conio.h>
#include<string.h> //including Library for working with String.
void encrypt(char name[], int key) //this is parameterized function.
{
unsigned int i;
for(i = 0; i < strlen(name); ++i)
{
name[i] = name[i] - key;
}
}
void decrypt(char name[], int key) //this is parameterized function.
{
unsigned int i;
for(i = 0; i < strlen(name); ++i)
{
name[i] = name[i] + key;
}
}
void main()
{
char name[20];
printf("Enter your name: \n");
scanf("%s", name);
printf("Name = %s\n", name);
encrypt(name, 0xFACA); //passing arguments.
printf("Encrypted name = %s\n", name);
decrypt(name, 0xFACA); //passing arguments.
printf("Decrypted name = %s\n", name);
getch();
}
#include<stdio.h>
#include<conio.h>
void fun();
void main()
{
fun();
getch();
}
void fun()
{
FILE *fs, *ft;
char ch;
printf("Reading MyTxtFile.txt \n");
fs = fopen("C://test/MyTxtFile.txt", "r"); //normal file.
printf("Writing MyTxtFile2.txt \n");
ft = fopen("C://test/MyTxtFile2.txt", "w"); //encrypted file.
if(fs == NULL || ft == NULL)
{
printf("File opening error!");
}
printf("Encrypting MyTxtFile2.txt \n");
while((ch = getc(fs)) != EOF)
putc(~ch, ft);
fclose(fs);
fclose(ft);
printf("Process finished");
}