You are on page 1of 6

Name :- NILAM UGALE

Class :- TYBSC CS Division :- II


Roll No. :- CS-4142
ITNS Practical 1 Batch D

 CAESAR CIPHER :-
 This scheme first explained and proposed by Julius Caesar, and is termed
Caesar cipher.
 It was the first example of substitution cipher. In the substitution-cipher 
 technique, the characters of a plain-text message are replaced by other 
 characters, numbers or symbols. 
 The Caesar cipher is a special case of substitution technique where in
each 
 alphabet in a message is replaced by an alphabet three places down the
line. 
 For instance, using the Caesar cipher, the plain-text Rutuja Nachnekar
will become cipher-text 
 UDKXO1.

Input
public class CeaserCifer {

public static StringBuffer encrypt(String text, int s)


{
StringBuffer result= new StringBuffer();

for (int i=0; i<text.length(); i++)


{
if (Character.isUpperCase(text.charAt(i)))
{
char ch = (char)(((int)text.charAt(i) +
s - 65) % 26 + 65);
result.append(ch);
}
else
{
char ch = (char)(((int)text.charAt(i) +
s - 97) % 26 + 97);
result.append(ch);
}
}
return result;
}

// Driver code
public static void main(String[] args)
{
String text = "My name is nilam ";
int s = 3;
System.out.println("Text : " + text);
System.out.println("Shift : " + s);
System.out.println("Cipher: " + encrypt(text, s));
}

}
Output

MONOALPHABETIC CIPHER :-

Monoalphabetic cipher is a substitution cipher in which for a given key, the


cipher alphabet for each plain alphabet is fixed throughout the encryption
process.
For example, if 'A' is encrypted as 'D', for any number of occurrence in that
plaintext, 'A' will always get encrypted to 'D'.
INPUT –

import java.util.Scanner;
public class MonoalphabeticCipher {

public static char p[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z' };
public static char ch[] = { 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O',
'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C',
'V', 'B', 'N', 'M' };

public static String doEncryption(String s)


{
char c[] = new char[(s.length())];
for (int i = 0; i < s.length(); i++)
{
for (int j = 0; j < 26; j++)
{
if (p[j] == s.charAt(i))
{
c[i] = ch[j];
break;
}
}
}
return (new String(c));
}

public static String doDecryption(String s)


{
char p1[] = new char[(s.length())];
for (int i = 0; i < s.length(); i++)
{
for (int j = 0; j < 26; j++)
{
if (ch[j] == s.charAt(i))
{
p1[i] = p[j];
break;
}
}
}
return (new String(p1));
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the message: ");
String en = doEncryption(sc.next().toLowerCase());
System.out.println("Encrypted message: " + en);
System.out.println("Decrypted message: " + doDecryption(en));
sc.close();
}
}

OUTPUT

You might also like