Professional Documents
Culture Documents
Caesar Cipher
Caesar Cipher
AIM:
ALGORITHM:
PROGRAM:
import java.util.Scanner;
text = text.toLowerCase();
sc.close();
System.out.println("Invalid Key");
return;
//encryption
int newCharPosition = 0;
if(charPosition == -1) {
return;
else
}
System.out.println("Encrypted Text: "+ encryptedText);
//decryption
int newCharPosition = 0;
} else {
Output
computer
AIM:
ALGORITHM:
PROGRAM:
import java.util.Scanner;
class PlayFair {
inputText = inputText.toLowerCase();
scanner.close();
{ if(alphabets.indexOf(currChar) < 0)
return true;
if(keyMatrix[i][j]==currChar || currChar=='J')
return true;
return false;
{
key=key.toUpperCase();
key=key.replaceAll("J", "I");
int x = 0;
int y = 0;
char z ='A';
if(!validateChar(key.charAt(i)))
keyMatrix[x][y++]=key.charAt(i);
if(y>4)
y=0;
x++;
while(x < 5)
while(y < 5)
if(!validateChar(z))
keyMatrix[x][y++] = z;
}
z++;
y=0;
x++;
System.out.println();
System.out.print("\t" + keyMatrix[i][j]);
System.out.println("\n");
int getRowPos(char c)
if(keyMatrix[i][j]==c)
return i;
return -1;
int getColumnPos(char c)
if(keyMatrix[i][j]==c)
return j;
return -1;
plain = plain.toUpperCase();
char x = plain.charAt(0);
char y = plain.charAt(1);
int row1 = 0;
int col1 = 0;
int row2 = 0;
int col2 = 0;
row1 = getRowPos(x);
col1 = getColumnPos(x);
row2 = getRowPos(y);
col2 = getColumnPos(y);
if(col1==col2)
++row1;
++row2;
if(row1>4)
row1=0;
if(row2>4)
row2=0;
cipherChar+=keyMatrix[row1][col2];
cipherChar+=keyMatrix[row2][col1];
else if(row1==row2)
++col1;
++col2;
if(col1>4)
col1=0;
if(col2>4)
col2=0;
cipherChar+=keyMatrix[row1][col1];
cipherChar+=keyMatrix[row2][col2];
else{
cipherChar+=keyMatrix[row1][col2];
cipherChar+=keyMatrix[row2][col1];
return cipherChar;
addKey(key);
text=text.replaceAll("j", "i");
text=text.toUpperCase();
if(textLength/2 != 0)
textLength = textLength + 1;
}
return encrText;
text=text.toUpperCase();
char x = text.charAt(0);
char y = text.charAt(1);
int row1;
int col1;
int row2;
int col2;
row1=getRowPos(x);
col1=getColumnPos(x);
row2=getRowPos(y);
col2=getColumnPos(y);
if(col1==col2)
{
--row1;
--row2;
if(row1 < 0)
row1=4;
if(row2 < 0)
row2=4;
decriptedChar+=keyMatrix[row1][col2];
decriptedChar+=keyMatrix[row2][col1];
else if(row1==row2)
--col1;
--col2;
if(col1 < 0)
col1=4;
if(col2 < 0)
col2=4;
decriptedChar+=keyMatrix[row1][col1];
decriptedChar+=keyMatrix[row2][col2];
else{
decriptedChar+=keyMatrix[row1][col2];
decriptedChar+=keyMatrix[row2][col1];
return decriptedChar;
}
text=text.replaceAll("j", "i");
text=text.toUpperCase();
int len=text.length();
return decriptedText;
Output
Key Matrix
H E L O A
B C D F G
I K M N P
Q R S T U
V W X Y Z
Encrypted text : EC ED LN LW
Decrypted text : WE LC OM EX