1.
a) Ceasar Cipher
import [Link];
public class Caesar {
public static void main(String[] args){
String cip=[Link]();
[Link](cip);
}
// Caesar Encryption Function
private static String encrypt() {
char chars[] =
{'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'};
String empty = "empty";
Scanner input = new Scanner([Link]);
[Link]("Enter the plaintext");
String plainText = [Link]();
String cipher = null;
char[] plain = [Link]();
for(int i = 0;i<[Link];i++){
for(int j = 0 ; j<=25;j++){
if(j<=22){
if(plain[i]==chars[j]){
plain[i] = chars[j+3];
break;
}
}//End nested If
else if(plain[i] == chars[j])
{
plain[i] = chars [j-23];
} //End else
} //End nested for loop
} //End of For loop
cipher = [Link](plain);
[Link](" cipher text is "+cipher);
Scanner in = new Scanner([Link]);
[Link]("To Decrypt plaintext enter 1");
int choice = [Link]();
if(choice == 1){
return cipher;
}
else{
[Link]("Thank you");}
return empty;
}
// Caesar Decryption Function
private static String decrypt(String cip) {
char chars[] =
{'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'};
String cipher = null;
String empty = "empty";
char[] cipher1 = [Link]();
if(cip .equals(empty)){
[Link](" No text is Decrypted");
}
else{ //char[] cipher1 = [Link]();
for(int i = 0;i<[Link];i++){
for(int j = 0 ; j<=25;j++){
if(j>=3 && cipher1[i]==chars[j]){
cipher1[i] = chars[j-3];
break;
}
if(cipher1[i] == chars[j] && j<3){
cipher1[i] = chars[23+j];
break;
} //End IF
} //End nested for loop
} //End of For loop
}
cipher=[Link](cipher1);
[Link](" Plain text is '"+cipher+"'");
return cipher;
}
}
1.b) Modified Ceasar cipher
import [Link].*;
public class modicaesarcipher
{
public String encrypt(int shift, String line)
{
String result="";
int offset;
for(int i=0;i<[Link]();i++)
{
offset=((int)[Link](i)+shift)%256;
result+=(char)(offset);
}
return result;
}
public String decrypt(int shift, String line)
{
String result="";
int offset;
for(int i=0;i<[Link]();i++)
{
offset=((int)[Link](i)-shift)%256;
if(offset<0)
offset+=256;
result+=(char)(offset);
}
return result;
}
public static void main(String args[])throws IOException
{
modicaesarcipher obj=new modicaesarcipher();
BufferedReader in=new BufferedReader(new
InputStreamReader([Link]));
int choice;
[Link]("Menu:\n1: Encryption\n2: Decryption");
choice=[Link]([Link]());
[Link]("Enter the shift: ");
int shift=[Link]([Link]());
[Link]("Enter the line: ");
String line=[Link]();
[Link]("Result:");
switch(choice)
{
case 1:[Link]([Link](shift,line));
break;
case 2:[Link]([Link](shift,line));
break;
default:
[Link]("Invalid input!");
break;
}
}
}
1.c) Mono Alphabetic Cipher
import [Link];
import [Link].*;
class MonoalphabeticCipher
{
static String c1="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static String c2="LMNOPQRSTUVWXYZACEBFDGHIJK";
public static void main(String[] args)
{
char current;
String ciphertxt="";
String plaintxt,str2;
int key, indx=0,len;
Scanner in1=new Scanner([Link]);
Scanner in =new Scanner([Link]);
[Link]("\n SUBSTITUTION CIPHER ALGORITHM FOR
ENCRYPTION");
[Link]("Enter the message to be Encrypted:");
plaintxt=[Link]();
plaintxt=[Link]();
len=[Link]();
for(int i=0;i<len;i++)
{
current=[Link](i);
for(int j=0;j<26;j++)
{
if([Link](j)==current)
indx=j;
}
ciphertxt=ciphertxt+[Link](indx);
}
[Link]("The Ciphertext is:"+ciphertxt);
[Link]("\nDo you want to Decrypt the message(Type 'Yes' or
'No')");
String choice=([Link]()).readLine();
plaintxt=" ";
if([Link]("yes")==true)
{
for(int i=0; i<[Link]();i++)
{
current=[Link](i);
for(int j=0;j<26;j++)
{
if([Link](j)==current)
indx=j;
}
plaintxt=plaintxt+[Link](indx);
}
[Link]("The PlainText is:"+[Link]());
}
else
[Link](0);
}
}
1.d) Poly Alphabetic cipher (Vigenere Cipher)
import [Link].*;
public class Viginere
{
public static void main(String[] args)
{
Scanner in=new Scanner([Link]);
String p="",c="",k="",l="abcdefghijklmnopqrstuvwxyz";
[Link]("give the pl text ");
p=[Link]();
[Link]("give the key ");
k=[Link]();
int klen=[Link]();
int plen=[Link]();
String y =k;
int m = plen%klen;
for( int i=1; i<plen/klen ; i++)
{
k=k+y;
}
k=k+[Link](0,m);
for (int j=0; j<plen; j++ )
{
c=c + [Link](([Link]([Link](j)) + [Link]([Link](j)))%26);
}
c=[Link]();
[Link]("Ciphertext "+c);
// Decryption
c = [Link]();
String pl = "";
for (int r=0; r<plen; r++)
{
pl=pl + [Link]((([Link]([Link](r)) - [Link]([Link](r))) +26
)%26);
}
[Link]("decipher: "+p); } }
2.a) Rail fence Cipher
public class railfence {
public static void main(String args[])
{
String input = "inputstring";
String output = "";
int len = [Link]();
[Link]("Input String : " + input);
for(int i=0;i<len;i+=2) {
output += [Link](i);
}
for(int i=1;i<len;i+=2) {
output += [Link](i);
}
[Link]("Ciphered Text : "+output);
}
}
2.b) Simple Columnar Cipher
import [Link].*;
import [Link].*;
class SimpleColumner
{
public static void main(String[] args)
{
String pt,ct;
int key;
char matrix[][];
Console c=[Link]();
[Link]("Its simple Columnar Technique.");
pt=[Link]("Enter the message to be encrypted\n");
int len=[Link]();
int rows;
if(len%6==0)
rows=len/6;
else
rows=(len/6)+1;
matrix=new char[rows][6];
int count=0;
key=[Link]([Link]("Enter the key for encryption : "));
for(int i=0;i<rows;i++)
{
for(int j=0;j<6;j++)
{
if(count<len)
matrix[i][j]=[Link](count);
else
matrix[i][j]='$';
count++;
}
}
ct="";
int mul=100000;
int num=key;
for(int i=0;i<6;i++)
{
int r=num/mul;
num=num%mul;
mul=mul/10;
for(int j=0;j<rows;j++)
ct=ct+matrix[j][r-1];
}
[Link]("The cipher text is: "+ct);
String choice=[Link]("\nDo you want to Decrypt the cipher text: ");
pt="";
if([Link]("yes")==true)
{
for(int i=0;i<rows;i++)
{
for(int j=0;j<6;j++)
if(matrix[i][j]!='$')
pt=pt+matrix[i][j];
}
[Link]("The Decrypted message id: "+pt);
}
else
[Link](0);
}
}
2.c) Multi Columnar Cipher
import [Link].*;
import [Link].*;
class MultiColumnar
{
String pt,ct;
int key;
char matrix1[][][], matrix2[][][];
int round,rows,count=0,len,mul,num;
Console c=[Link]();
public void encrypt()
{
pt=[Link]("Enter the message to be encrypted\n");
round=[Link]([Link]("Enter the number of iterations
: "));
len=[Link]();
if(len%6==0)
rows=len/6;
else
rows=(len/6)+1;
matrix1=new char[round][rows][6];
key=[Link]([Link]("Enter the key for encryption : "));
for(int k=0;k<round;k++)
{
count=0;
for(int i=0;i<rows;i++)
{
for(int j=0;j<6;j++)
{
if(count<len)
matrix1[k][i][j]=[Link](count);
else
matrix1[k][i][j]='$';
count++;
}
}
ct="";
mul=100000;
num=key;
for(int i=0;i<6;i++)
{
int r=num/mul;
num=num%mul;
mul=mul/10;
for(int j=0;j<rows;j++)
ct=ct+matrix1[k][j][r-1];
}
pt=ct;
len=[Link]();
}
[Link]("The cipher text is: "+ct);
}
public void decrypt()
{
rows=len/6;
matrix2=new char[round][rows][6];
for(int k=0;k<round;k++)
{
pt="";
len=[Link]();
count=0;
num=key;
mul=100000;
do
{
int r=num/mul;
num=num%mul;
mul=mul/10;
for(int j=0;j<rows;j++)
{
matrix2[k][j][r-1]=[Link](count);
count++;
}
}
while(num>0);
for(int i=0;i<rows;i++)
{
for(int j=0;j<6;j++)
{
if(k==round-1)
{
if(matrix2[k][i][j]!='$')
pt=pt+matrix2[k][i][j];
}
else
pt=pt+matrix2[k][i][j];
}
}
ct=pt;
}
[Link]("The plain text is: "+pt);
}
public static void main(String[] args)
{
[Link]("Multi Columnar Transposition Technique: ");
MultiColumnar mc=new MultiColumnar();
[Link]();
Console c=[Link]();
String choice=[Link]("\n Do ypu want to Decrypt the cpher text: ");
if([Link]("yes")==true)
{
[Link]();
}
else
[Link](0);
}}
2.d) Vernam Cipher
import [Link];
public class Vernam {
public static void main(String args[]) {
String text = new String("hello");
char[] arText = [Link]();
String cipher = new String("XYZHG");
char[] arCipher = [Link]();
char[] encoded = new char[5];
[Link]("Encoded " + text + " to be... ");
for (int i = 0; i < [Link]; i++) {
encoded[i] = (char) (arText[i] ^ arCipher[i]);
[Link](encoded[i]);
}
[Link]("\nDecoded to be... ");
for (int i = 0; i < [Link]; i++) {
char temp = (char) (encoded[i] ^ arCipher[i]);
[Link](temp);
}}}
3) Diffie Hellman Key Exchange Algorithm
import [Link].*;
import [Link].*;
import [Link];
class Diffie
{
public static void main( String [] args)
{
Scanner sc = new Scanner ([Link]);
int g,n,x,y;
[Link]("enter value of n");
n=[Link]();
[Link]("enter value of g");
g=[Link]();
[Link]("enter value of x");
x=[Link]();
[Link]("enter value of y");
y=[Link]();
double a=([Link](g,x)) %n;
[Link]("Value of a =" + a);
double b=([Link](g,y)) %n;
[Link]("Value of b =" + b);
double k1=([Link](b,x)) %n;
[Link]("Value of k1 =" + k1);
double k2=([Link](a,y)) %n;
[Link]("Value of k2 =" + k2);
if(k1==k2)
[Link]("Both are same");
else
[Link]("Both are not same");
} // end main
} //end class
[Link] of DES algorithm
import [Link].*;
import [Link].*;
import [Link];
import [Link].*;
import [Link];
import [Link].*;
public class DES
{
Cipher ec;
Cipher dc;
DES( SecretKey key)
{
try
{
ec = [Link]("DES");
dc = [Link]("DES");
[Link](Cipher.ENCRYPT_MODE, key);
[Link](Cipher.DECRYPT_MODE, key);
}
catch( Exception e){ }
}
public String encrypt(String str)
{
try
{
// Encode the string into bytes using utf8
byte[] utf8 = [Link]("UTF8");
// Encrypt
byte[] enc = [Link](utf8);
// Encode bytes to base64 to get a string
return new [Link].BASE64Encoder().encode(enc);
}
catch(Exception e) { }
return null;
}
public String decrypt(String str)
{
try
{
// Decode base64 to get bytes
byte[] dec=new [Link].BASE64Decoder().decodeBuffer(str);
// Decrypt
byte[] utf8 = [Link](dec);
// Decode using utf-8
return new String(utf8, "UTF8");
}
catch(Exception e) {}
return null;
}
public static void main(String [] args)
{
SecretKey key=null;
try
{
// Generate a DES key
KeyGenerator keyGen = [Link]("DES");
key = [Link]();
DES desenc = new DES(key);
String s = "Welcome to our college....";
String en = [Link](s);
String de = [Link](en);
[Link]("Original Text:" +s);
[Link]("Encrypted Text:" +en);
[Link]("Decrypted Text:" +de);
}
catch (Exception e) {}
}// end main
} //end class
5) Implementation of AES algorithm
import [Link].*;
import [Link].*;
import [Link].*;
public class AES
{
Cipher ecipher;
Cipher dcipher;
AES(SecretKey key)
{
try
{
ecipher = [Link]("AES");
dcipher = [Link]("AES");
[Link](Cipher.ENCRYPT_MODE, key);
[Link](Cipher.DECRYPT_MODE, key);
}
catch (Exception e) {}
}
public String encrypt(String str)
{
try
{
// Encode the string into bytes using utf8
byte[] utf8 = [Link]("UTF8");
// Encrypt
byte[] enc = [Link](utf8);
// Encode bytes to base64 to get a string
return new [Link].BASE64Encoder().encode(enc);
}
catch(Exception e) {}
return null;
}
public String decrypt(String str)
{
try
{
// Decode base64 to get bytes
byte[] dec=new [Link].BASE64Decoder().decodeBuffer(str);
// Decrypt
byte[] utf8 = [Link](dec);
// Decode using utf-8
return new String(utf8, "UTF8");
}
catch(Exception e) {}
return null;
}
public static void main(String args[])
{
SecretKey key=null;
try
{
// Generate a AES key
KeyGenerator keyGen = [Link]("AES");
key = [Link]();
}
catch (Exception e)
{
[Link]();
}
AES dese = new AES(key);
String o = "Welcome to our college....";
String en = [Link](o);
String de = [Link](en);
[Link]("Original Text:"+o);
[Link]("Encrypted Text:"+en);
[Link]("Decrypted Text:"+de);
}
}
6) Implementation of RSA algorithm
import [Link].*;
import [Link].*;
public class RSA
{
BigInteger p,q,n,d,e,ph,t;
SecureRandom r;
public RSA()
{
r=new SecureRandom();
p=new BigInteger(512,100,r); // Prime nos.
q=new BigInteger(512,100,r);
[Link]("prime nos p and q are "+[Link]()+" ,
"+[Link]());
n=[Link](q); // n=p*q
ph=([Link](new BigInteger("1"))); // -----ph=(p-1)
ph=[Link]([Link](new BigInteger("1"))); // ------ ph=ph * (q-1)
e=new BigInteger("2");//initial factor
while([Link](e).intValue() > 1 || [Link](ph) !=-1) // checks factors
encryption key e not as the factors of ph
e = [Link](new BigInteger("1"));//or "2" when bug
d=[Link](ph); // select d private or decryption key such that (d*e)
mod (p-1)*(q-1) = 1
[Link]("public key is ("+[Link]()+" , "+[Link]()+")");
[Link]("pvt key is ("+[Link]()+" , " +[Link]()+")");
BigInteger msg= new BigInteger("15"); // original msg
[Link]("\nMessage is: "+msg);
BigInteger enmsg=encrypt(msg,e,n);
[Link]("\nEncrypted msg is: "+[Link]());
BigInteger demsg=decrypt(enmsg,d,n);
[Link]("\nDecrypted msg is: "+[Link]());
}
BigInteger encrypt(BigInteger msg,BigInteger e,BigInteger n)
{
return [Link](e, n); //PT raised to e mod n
}
BigInteger decrypt(BigInteger msg,BigInteger d,BigInteger n)
{
return [Link](d, n); //CT raised to d mod n
}
public static void main(String[] args)
{
new RSA();
}
}
7) Implementation of Rc4 Algorithm
class RC4Demo
{
String strPlainText;
static char cipher[];
RC4Demo(String strPlainText,int []key)
{
[Link] = strPlainText;
int S[] = new int[255];
cipher = new char[[Link]()];
for (int i=0;i<[Link];i++)
{
S[i] = i;
}
int i=0;
int j=0;
for (int k=0;k < [Link]();k++)
{
int modk = (k%[Link]);
int Kc = key[modk];
j = (S[i] + j + Kc) % 256 + 1;
int temp = S[i];
S[i] = S[j];
S[j] = temp;
int Sc = (S[i]+S[j]) % 256;
int Ck = S[Sc];
cipher[k] = (char) (Ck ^ (int)[Link](k));
i = i+1;
}
}
public static void main(String[] args)
{
int K[] = {1, 2, 3, 4, 5};
String strOriginal = "Hello World";
[Link]("Original String--> "+strOriginal);
new RC4Demo(strOriginal,K);
for (int i=0;i<[Link];i++)
{
[Link](" "+cipher[i]);
}}}
8) Implementation of Blowfish Algorithm
import [Link];
import [Link];
import [Link];
import [Link];
public class BlowfishCipher {
public static void main(String[] args) throws Exception
{
KeyGenerator keygen =
[Link]("Blowfish"); // create a key
SecretKey secretkey = [Link]();
Cipher cip = [Link]("Blowfish"); // initialise cipher to with
secret key
[Link](Cipher.ENCRYPT_MODE, secretkey);
String inputText = [Link](" Give Input: ");
byte[] encrypted = [Link]([Link]());
[Link](Cipher.DECRYPT_MODE, secretkey);
byte[] decrypted = [Link](encrypted);
[Link]([Link](),
"encrypted : " + new String(encrypted) + "\n" +
"decrypted : " + new String(decrypted));
[Link](0);
}
}