Simple C program to Encrypt and Decrypt a password
Write a C program to Encryption and Decryption of password.
In this program we encrypt the given string by subtracting the hex value from each character of the password.
Example Program:
#include <stdio.h>
#include <string.h>
void encrypt(char password[],int key)
{
unsigned int i;
for(i=0;i<strlen(password);++i)
{
password[i] = password[i] - key;
}
}
void decrypt(char password[],int key)
{
unsigned int i;
for(i=0;i<strlen(password);++i)
{
password[i] = password[i] + key;
}
}
int main()
{
char password[20] ;
printf("Enter the password: \n ");
scanf("%s",password);
printf("Password = %s\n",password);
encrypt(password,0xFACA);
printf("Encrypted value = %s\n",password);
decrypt(password,0xFACA);
printf("Decrypted value = %s\n",password);
return 0;
}
Output:
In this program we encrypt the given string by subtracting the hex value from each character of the password.
Example Program:
#include <stdio.h>
#include <string.h>
void encrypt(char password[],int key)
{
unsigned int i;
for(i=0;i<strlen(password);++i)
{
password[i] = password[i] - key;
}
}
void decrypt(char password[],int key)
{
unsigned int i;
for(i=0;i<strlen(password);++i)
{
password[i] = password[i] + key;
}
}
int main()
{
char password[20] ;
printf("Enter the password: \n ");
scanf("%s",password);
printf("Password = %s\n",password);
encrypt(password,0xFACA);
printf("Encrypted value = %s\n",password);
decrypt(password,0xFACA);
printf("Decrypted value = %s\n",password);
return 0;
}
Output:
In the above program the hexadecimal value "0XFACA" represents 64202 in decimal. Hence rather than representing it in hexadecimal, any random integer values can also be used.
Leave a Comment