Write a C/JAVA program to perform encryption and decryption using Caesar Cipher algorithms

HARDWARE AND SOFTWARE REQUIREMENT:

1. Intel based Desktop PC: - RAM of 512 MB
2. Notepad/Notepad ++ editor
3.Turbo C++

THEORY: Caesar Cipher      

Ø It is cryptographic technique ( Adopted for secure communication in the presence of third parties(opponent or enemy) i.e. prohibiting third parties or the public from reading private messages.
Ø Also called Shift Cipher or Caesar shift.
Ø It is one of the simplest and most widely known encryption techniques or mono-alphabetic cipher scheme in which each letter of the plain text is substituted by another letter to form the cipher text.
Ø In this technique, both sender and receiver must agree on a “secret shift number”( between 0 and 25 becomes the key of encryption) for shifting the alphabet.
Ø The name ‘Caesar Cipher’ is occasionally used to describe when the ‘shift of three’ is used.

Text         :    RAIS
Shift        :     23
Cipher      :    OXFP

Text          :     YOUTUBER
Shift         :     4
Cipher      :     CSYX YFIV

SOURCE CODE:

#include <stdio.h>
#include <string.h>
#include<conio.h>
#include <ctype.h>
void main()
{
char plain[10], cipher[10];
int key,i,length;
int result;
clrscr();
printf("\n Enter the plain text:");
scanf("%s", plain);
printf("\n Enter the key value:");
scanf("%d", &key);
printf("\n \n \t PLAIN TEXt: %s",plain);
printf("\n \n \t ENCRYPTED TEXT: ");
for(i = 0, length = strlen(plain); i < length; i++)
{
cipher[i]=plain[i] + key;
if (isupper(plain[i]) && (cipher[i] > 'Z'))
cipher[i] = cipher[i] - 26;
if (islower(plain[i]) && (cipher[i] > 'z'))
cipher[i] = cipher[i] - 26;
printf("%c", cipher[i]);
}
printf("\n \n \t AFTER DECRYPTION : ");
for(i=0;i<length;i++)
{
plain[i]=cipher[i]-key;
if(isupper(cipher[i])&&(plain[i]<'A'))
plain[i]=plain[i]+26;
if(islower(cipher[i])&&(plain[i]<'a'))
plain[i]=plain[i]+26;
printf("%c",plain[i]);
}
getch();
}
OUTPUT:

Write a JAVA program to perform encryption and decryption using Caesar Cipher algorithms:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class CeaserCipher
{
static Scanner sc=new Scanner(System.in);
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException
{
//TODO code application logic here

System.out.print("EnteranyString:");
String str=br.readLine();
System.out.print("\nEntertheKey:");
int key=sc.nextInt();
String encrypted=encrypt(str,key);
System.out.println("\nEncryptedStringis:" + encrypted);
String decrypted=decrypt(encrypted,key);
System.out.println("\nDecryptedStringis:" + decrypted);
System.out.println("\n");
}

public static String encrypt(String str,int key)
{
String encrypted="";
for(int i=0;i<str.length();i++)
{
int c=str.charAt(i);
 if(Character.isUpperCase(c))
{
c=c+(key%26);
}
if(c>'Z')
{
 c=c-26;
}
 else if(Character.isLowerCase(c))
{
c=c+(key%26);
 if(c>'z')

 c=c-26;
}
 encrypted +=(char)c;
}
return encrypted;
}

public static String decrypt(String str,int key)
 { 
String decrypted="";
for(int i=0;i<str.length();i++)  

int c=str.charAt(i);
if(Character.isUpperCase(c))
{
c=c-(key%26);
}
if(c<'A')
{

c=c+26;
}
else if(Character.isLowerCase(c))
{
c=c-(key%26);

if(c<'a')

c=c+26;
}
decrypted +=(char)c;
}
return decrypted;
}
}

OUTPUT:
Enter any String: RAIS
Enter the Key:23
Encrypted String is: OXFP

Decrypted String is: RAIS 

Comments

Search related post on google