Sunday, 18 October 2015

Practical No-8

AIM: Write a program to Implement RC4 algorithm .

import java.io.*;
class rc4
{
public static void main(String args[])throws IOException
            {          int temp=0;
                        String ptext;
                        String key;
                        int s[]=new int[256];
                        int k[]=new int[256];
                        DataInputStream in=new DataInputStream(System.in);
                        System.out.print("\nENTER PLAIN TEXT\t");
                        ptext=in.readLine();
                        System.out.print("\n\nENTER KEY TEXT\t\t");
                        key=in.readLine();
                        char ptextc[]=ptext.toCharArray();
                        char keyc[]=key.toCharArray();
                        int cipher[]=new int[ptext.length()];
                        int decrypt[]=new int[ptext.length()];
int ptexti[]=new int[ptext.length()];
                        int keyi[]=new int[key.length()];
                        for(int i=0;i<ptext.length();i++)
                        {          ptexti[i]=(int)ptextc[i];
                        }
                        for(int i=0;i<key.length();i++)
                        {          keyi[i]=(int)keyc[i];
                        }
                        for(int i=0;i<255;i++)
                        {
                                    s[i]=i;
                                    k[i]=keyi[i%key.length()];
                        }
                        int j=0;
                        for(int i=0;i<255;i++)
                        {
                                    j=(j+s[i]+k[i])%256;
                                    temp=s[i];
                                    s[i]=s[j];
                                    s[j]=temp;
                        }
                        int i=0;
                        j=0;
                        int z=0;
                        for(int l=0;l<ptext.length();l++)
                        {
                                    i=(l+1)%256;
                                    j=(j+s[i])%256;
                                    temp=s[i];
                                    s[i]=s[j];
                                    s[j]=temp;
                                    z=s[(s[i]+s[j])%256];
                                    cipher[l]=z^ptexti[l];
                                    decrypt[l]=z^cipher[l];
                        }
                        System.out.print("\n\nENCRYPTED:\t\t");
                        display(cipher);
                        System.out.print("\n\nDECRYPTED:\t\t");
                        display(decrypt);
            }

            static void display(int disp[])
            {
                        char convert[]=new char[disp.length];
                        for(int l=0;l<disp.length;l++)
                        {
                                    convert[l]=(char)disp[l];
                                    System.out.print(convert[l]);
                        }
            }

}
Practical No-7

Aim: Write a program to implement Blowfish algorithm

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.swing.JOptionPane;
public class blowfish
{
            public static void main(String[] args) throws Exception
 {
KeyGenerator keygen = KeyGenerator.getInstance("Blowfish");
SecretKey secretkey = keygen.generateKey();
Cipher cip = Cipher.getInstance("Blowfish");
cip.init(Cipher.ENCRYPT_MODE, secretkey);
String inputText = JOptionPane.showInputDialog(" Give Input: ");
byte[] encrypted = cip.doFinal(inputText.getBytes());
cip.init(Cipher.DECRYPT_MODE, secretkey);
byte[] decrypted = cip.doFinal(encrypted);
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"encrypted : " +     new String(encrypted) + "\n" +"decrypted : " + new String(decrypted));
System.exit(0);
}


}

Practical No-6

AIM- Write a program to implement RSA algorithm in Java

import java.math.BigInteger;
import java.security.SecureRandom;
public class rsa
{
            private BigInteger n, d, e;
             private int bitlen = 1024;
              public rsa(BigInteger newn, BigInteger newe)
             {
                        n = newn;
                        e = newe;
            }
 public rsa(int bits)
{
                        bitlen = bits;
                        SecureRandom r = new SecureRandom();
                        BigInteger p = new BigInteger(bitlen / 2, 100, r);
                          BigInteger q = new BigInteger(bitlen / 2, 100, r);
                         n = p.multiply(q);
                        BigInteger m = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE));
                        e = new BigInteger("3");
  while (m.gcd(e).intValue() > 1)
 {
                                     e = e.add(new BigInteger("2"));
                        }
                         d = e.modInverse(m);
             }
public synchronized BigInteger encrypt(BigInteger message)
 {
                         return message.modPow(e, n);
            }
 public synchronized BigInteger decrypt(BigInteger message)
{
                        return message.modPow(d, n);
}
public static void main(String[] args)
{
                         rsa rsa = new rsa(1024);
                        String text1 = "SERO";
                         System.out.println("Plaintext: " + text1);
                        BigInteger plaintext = new BigInteger(text1.getBytes());
                        BigInteger ciphertext = rsa.encrypt(plaintext);
                         System.out.println("Ciphertext: " + ciphertext);
                          plaintext = rsa.decrypt(ciphertext);
                           String text2 = new String(plaintext.toByteArray());
                         System.out.println("Plaintext: " + text2);
             }

}
Practical No-5

Aim: Write a program to implement AES algorithm.

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.swing.JOptionPane;
public class aes
{
            public static void main(String[] args) throws Exception
 {
KeyGenerator keygen = KeyGenerator.getInstance("AES");
SecretKey secretkey = keygen.generateKey();
Cipher cip = Cipher.getInstance("AES");
cip.init(Cipher.ENCRYPT_MODE, secretkey);
String inputText = JOptionPane.showInputDialog(" Give Input: ");
byte[] encrypted = cip.doFinal(inputText.getBytes());
cip.init(Cipher.DECRYPT_MODE, secretkey);
byte[] decrypted = cip.doFinal(encrypted);
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"encrypted : " +     new String(encrypted) + "\n" +"decrypted : " + new String(decrypted));
System.exit(0);
}


}
Practical No-4

Aim: Write a program to implement DES algorithm.


import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.swing.JOptionPane;
public class aes
{
            public static void main(String[] args) throws Exception
 {
KeyGenerator keygen = KeyGenerator.getInstance("AES");
SecretKey secretkey = keygen.generateKey();
Cipher cip = Cipher.getInstance("AES");
cip.init(Cipher.ENCRYPT_MODE, secretkey);
String inputText = JOptionPane.showInputDialog(" Give Input: ");
byte[] encrypted = cip.doFinal(inputText.getBytes());
cip.init(Cipher.DECRYPT_MODE, secretkey);
byte[] decrypted = cip.doFinal(encrypted);
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"encrypted : " +     new String(encrypted) + "\n" +"decrypted : " + new String(decrypted));
System.exit(0);
}

}



Practical No-3

AIM: To Implement Diffie-Hellman Key Exchange Algorithm in Java


 import java.util.*;
import java.math.BigInteger.*;
import java.math.*;
class diffiehellman
{public static void main(String args[])
{
BigInteger A, B, K1, K2, x, y, g, n;
Scanner s = new Scanner(System.in);
System.out.println("Enter A’s prime number!");
n = s.nextBigInteger();
System.out.println("Enter B’s prime number!");
g = s.nextBigInteger();
System.out.println("Enter A’s secret key!");
x = s.nextBigInteger();
System.out.println("This key is sent to B!");
System.out.println("Enter B’s secret key!");
y = s.nextBigInteger();
System.out.println("This key is sent to A!");
A = g.modPow(x, n);
B = g.modPow(y, n);
K1 = B.modPow(x, n);
K2 = A.modPow(y, n);
System.out.println("A’s key is " + K1);
System.out.println("B’s key is " + K2);
}

}
Practical No-2(b)


AIM- Write a program to Implement vernam cipher(one time pad) in Java

import java.lang.Math;
public class vernamcipher
 {
public static void main(String args[])
{
String text = new String("vernam");
char[] arText = text.toCharArray();
String cipher = new String("abcdef");
char[] arCipher = cipher.toCharArray();
char[] encoded = new char[5];
System.out.println("Encoded " + text + " to be… ");
for (int i = 0; i < arText.length; i++)
{
encoded[i] = (char) (arText[i] ^ arCipher[i]);
System.out.print(encoded[i]);
}
System.out.println("\nDecoded to be… ");
for (int i = 0; i < encoded.length; i++)
{
char temp = (char) (encoded[i] ^ arCipher[i]);
System.out.print(temp);
}
}

}


Practical No -2(a)


AIM – Write a program to implement Railfence algorithm.

public class railfence
{
              public static void main(String args[])
             {
                        String input = "inputstring";
                         String output = "";
                        int len = input.length(),flag = 0;
                        System.out.println("Input String : " + input);
                         for(int i=0;i<len;i+=2)
{
                                     output += input.charAt(i);
                        }
                        for(int i=1;i<len;i+=2)
{
                                       output += input.charAt(i);
                        }      
                        System.out.println("Cipher Text : "+output);
            }

}

OUTPUT-

Input String : inputstring
Cipher Text : ipttignusrn

Practical No-1(c)


Aim: Write a program to implement Monoalphabetic Substitution Cipher Technique.

public class monoalphabetic
{
             public static void main(String[] args)
{
                        String plaintext = "hello";
                        char[] myarr = plaintext.toCharArray();
                        for(int count=0 ; count < myarr.length ; count++)
                        {
                                    char cipher = myarr[count]; 
                                    switch (cipher)
                                    {
 case 'a':
                                                            System.out.print("z");
                                                break;
                                                case 'b':
                                                              System.out.print("y");
                                                 break;
                                                 case 'c':
                                                            System.out.print("x");
                                                 break;
                                                 case 'd':
                                                             System.out.print("w");
                                                break;
                                                case 'e':
                                                             System.out.print("v");
                                                  break;
                                                case 'f':
                                                             System.out.print("u");
                                                 break;
                                                  case 'g':
                                                            System.out.print("t");
                                                break;
                                                case 'h':
                                                            System.out.print("s");
   break;
                                                  case 'i':
                                                             System.out.print("r");
                                                break;
                                                case 'j':
                                                             System.out.print("q");
                                                  break;
                                                case 'k':
                                                            System.out.print("p");
                                                 break;
                                                case 'l':
                                                            System.out.print("o");
                                                   break;
                                                 case 'm':
                                                            System.out.print("n");
                                                break;
                                    case 'n':
                                                System.out.print("m");
                                    break;
                                     case 'o':
                                                   System.out.print("l");
                                    break;
                                      case 'p':
                                                 System.out.print("k");
                                     break;
                                    case 'q':
                                                 System.out.print("j");
                                    break;
                                      case 'r':
    System.out.print("i");
                                      break;
                                     case 's':
    System.out.print("h");
                                      break;
                                      case 't':
                                                   System.out.print("g");
                                     break;
                                      case 'u':
    System.out.print("f");
                                      break;
                                      case 'v':
  System.out.print("e");
                                     break;
                                    case 'w':
                                                   System.out.print("d");
                                     break;
                                    case 'x':
                                                   System.out.print("c");
                                     break;
                                      case 'y':
                                                System.out.print("b");
                                      break;
                                    case 'z':
                                                System.out.print("a");
                                    break;
                                      case ' ':
                                                  System.out.print("1");
                                     break;
   case '1':
                                                  System.out.print(" ");
 }
                         }
}