












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Java code examples for writing programs to encrypt and decrypt text using different cryptography algorithms such as monoalphabetic, playfair, polyalphabetic, columnar transposition, and rail-fence cipher. Each program is accompanied by a brief explanation of the aim and the code. These examples can be useful for students in the field of information security & cyber law, specifically for those enrolled in the it308 course at bvm engineering college.
Typology: Exercises
1 / 20
This page cannot be seen from the preview
Don't miss anything!













AIM: Write a program to encrypt and decrypt text using Caesar cipher algorithm
CODE: import java.util.Scanner;
public class caesar { public static void main(String args[]){ String msg, emsg = ""; int key; char ch; Scanner sc = new Scanner(System.in); System.out.println("Enter a message: "); msg = sc.nextLine(); System.out.println("Enter key: "); key = sc.nextInt(); for(int i = 0; i < msg.length(); i++){ ch = msg.charAt(i); if(ch >= 'a' && ch <= 'z'){ ch = (char)(ch + key); if(ch > 'z'){ ch = (char)(ch - 'z' + 'a' - 1); } emsg += ch; } else if(ch >= 'A' && ch <= 'Z'){ ch = (char)(ch + key); if(ch > 'Z'){ ch = (char)(ch - 'Z' + 'A' - 1);
1
emsg += ch; } else emsg += ch; } System.out.println("encrypted message = " + emsg); String dmsg = ""; for(int i = 0; i < emsg.length(); i++){ ch = emsg.charAt(i); if(ch >= 'a' && ch <= 'z'){ ch = (char)(ch - key); if(ch < 'a'){ ch = (char)(ch + 'z' - 'a' + 1); } dmsg += ch; } else if(ch >= 'A' && ch <= 'Z'){ ch = (char)(ch - key); if(ch < 'A'){ ch = (char)(ch + 'Z' - 'A' + 1); } dmsg += ch; } else dmsg += ch; } System.out.println("decrypted message = " + dmsg); } 2
import java.util.Scanner;
public class Monoalpha{ public static void main(String args[]) { 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' }; char ch[] = { 'q', 'a', 'z', 'w', 's', 'x', 'e', 'd', 'c', 'r', 'f', 'v', 't', 'g', 'b', 'y', 'h', 'n', 'u', 'j', 'm', 'i', 'k', 'o', 'l', 'p' }; String emsg="",dmsg=""; Scanner sc = new Scanner(System.in); System.out.println("enter the message: "); String msg = sc.next().toLowerCase(); for (int i = 0; i < msg.length(); i++){ for (int j = 0; j < 26; j++){ if (p[j] == msg.charAt(i)) { emsg+=ch[j]; break; } } } System.out.println("encrypted message: " + emsg); for (int i = 0; i < emsg.length(); i++){ for (int j = 0; j < 26; j++){ if (ch[j] == emsg.charAt(i)){ dmsg+= p[j]; break; } } 4
System.out.println("decrypted message: " + dmsg); } }
OUTPUT:
AIM: Write a program to encrypt and decrypt text using Playfair cipher algorithm
CODE: 5
int a=0,b=0;
for(int k=0;k < key.length();k++){ if(!repeat(key.charAt(k))){ keyMatrix[a][b++]=key.charAt(k); if(b>4){ b=0; a++; }}} char p='A'; while(a < 5){ while(b < 5){ if(!repeat(p)) { keyMatrix[a][b++]=p; } p++; } b=0; a++; } System.out.print("key matrix :"); for(int i=0;i < 5;i++){ System.out.println(); for(int j=0;j < 5;j++){ System.out.print("\t"+keyMatrix[i][j]); }}} int rowPos(char c){ for(int i=0;i < keyMatrix.length;i++){ for(int j=0;j < keyMatrix[i].length;j++){ if(keyMatrix[i][j]==c) 7
return i;
}} return -1; } int columnPos(char c){ for(int i=0;i < keyMatrix.length;i++){ for(int j=0;j < keyMatrix[i].length;j++){ if(keyMatrix[i][j]==c) return j; }} return -1; } String encryptChar(String plain){ plain=plain.toUpperCase(); char a=plain.charAt(0),b=plain.charAt(1); String cipherChar=""; int r1,c1,r2,c2; r1=rowPos(a); c1=columnPos(a); r2=rowPos(b); c2=columnPos(b); if(c1==c2){ ++r1; ++r2; if(r1>4)r1=0; if(r2>4)r2=0; cipherChar+=keyMatrix[r1][c2]; cipherChar+=keyMatrix[r2][c1]; } 8
return cipherText;
} String decryptChar(String cipher){ cipher=cipher.toUpperCase(); char a=cipher.charAt(0),b=cipher.charAt(1); String plainChar=""; int r1,c1,r2,c2; r1=rowPos(a); c1=columnPos(a); r2=rowPos(b); c2=columnPos(b); if(c1==c2){ --r1; --r2; if(r1 < 0)r1=4; if(r2 < 0)r2=4; plainChar+=keyMatrix[r1][c2]; plainChar+=keyMatrix[r2][c1]; } else if(r1==r2){ --c1; --c2; if(c1 < 0)c1=4; if(c2 < 0)c2=4; plainChar+=keyMatrix[r1][c1]; plainChar+=keyMatrix[r2][c2]; } else{ plainChar+=keyMatrix[r1][c2]; 10
plainChar+=keyMatrix[r2][c1];
} return plainChar; } String Decrypt(String cipherText,String key){ String plainText=""; cipherText=cipherText.replaceAll("j", "i"); cipherText=cipherText.replaceAll(" ", ""); cipherText=cipherText.toUpperCase(); int len=cipherText.length(); for(int i=0;i < len-1;i=i+2){ plainText+=decryptChar(cipherText.substring(i,i+2)); plainText+=" "; } return plainText; }} class Pfc{ public static void main(String args[])throws Exception{ PlayFair p=new PlayFair(); Scanner scn=new Scanner(System.in); String key,cipherText,plainText; System.out.println("Enter plaintext:"); plainText=scn.nextLine(); System.out.println("Enter Key:"); key=scn.nextLine(); cipherText=p.Encrypt(plainText,key); System.out.println("\n\nEncrypted text:"); System.out.print(cipherText); String encryptedText=p.Decrypt(cipherText, key); 11
String msg =sc.nextLine();
System.out.println("enter key :"); String key =sc.nextLine(); String emsg = ""; msg = msg.toUpperCase(); for (int i = 0, j = 0; i < msg.length(); i++){ char c = msg.charAt(i); if (c < 'A' || c > 'Z') continue; emsg += (char) ((c + key.charAt(j) - 2 * 'A') % 26 + 'A'); j = ++j % key.length(); } String dmsg = ""; emsg = emsg.toUpperCase(); for (int i = 0, j = 0; i < emsg.length(); i++){ char c = emsg.charAt(i); if (c < 'A' || c > 'Z') continue; dmsg += (char) ((c - key.charAt(j) + 26) % 26 + 'A'); j = ++j % key.length(); } System.out.println("message: " + msg); System.out.println("encrypted message: " + emsg); System.out.println("decrypted message: " + dmsg); }}
OUTPUT:
13
AIM: Write a program to encrypt and decrypt text using Columnar Transposition cipher algorithm
CODE:
import java.util.*; class Ctc{ public static void main(String sap[]){ Scanner sc = new Scanner(System.in); System.out.print("\nEnter plaintext: "); 14
System.out.println();
} static int[][] encrypt(int plainText[][], int cipherText[][], String message, int rowCount, int columnCount, String key){ int i,j; int k=0; for(i=0; i<rowCount; i++){
for(j=0; j<columnCount; j++){ if(k < message.length()){ plainText[i][j] = (int)message.charAt(k); k++; } else{ break; }}} for(i=0; i<columnCount; i++) { int currentCol= ( (int)key.charAt(i) - 48 ) -1; for(j=0; j<rowCount; j++){ cipherText[j][i] = plainText[j][currentCol]; }} System.out.print("cipher array: \n"); for(i=0;i<rowCount;i++){ for(j=0;j<columnCount;j++){ System.out.print((char)cipherText[i][j]+"\t"); } System.out.println(); } return cipherText;
16
static int[][] decrypt(int plainText[][], int cipherText[][], String message, int rowCount, int columnCount, String key){ int i,j; int k=0; for(i=0; i<columnCount; i++){ int currentCol= ( (int)key.charAt(i) - 48 ) -1;
for(j=0; j<rowCount; j++){ plainText[j][currentCol] = cipherText[j][i]; }} System.out.print("plain array: \n"); for(i=0;i<rowCount;i++){ for(j=0;j<columnCount;j++){ System.out.print((char)plainText[i][j]+"\t"); } System.out.println(); } return plainText; }}
OUTPUT:
17
import java.util.*;
public class Rfc { public static void main(String[] args) { String message; String codedmessage; String input; String emsg="",dmsg=""; int length,arlength,lengthmod,i,e; Scanner sc = new Scanner(System.in); System.out.println("enter message:"); message = sc.next(); length = message.length(); for(i = 0; i < length; i =i+2){ emsg+= message.charAt(i); } for(i = 1; i < length; i =i+2){ emsg+= message.charAt(i); } System.out.print("\nencoded message:"); System.out.print(emsg); for(i=0;i<(length/2);i++){ dmsg+=emsg.charAt(i); if((length)%2==0) dmsg+=emsg.charAt(i+(length/2)); else dmsg+=emsg.charAt(i+(length/2)+1); } if((length)%2==1) dmsg+=emsg.charAt((length/2)); 19
System.out.print("\ndecoded message:");
System.out.print(dmsg); } }
OUTPUT:
20