himanshu cns 5-11

The man the myth is none other than himanshu

5. PROGRAM TO IMPLEMENT BLOWFISH ALGORITHM LOGIC

AIM: Write a C/JAVA program to implement the BlowFish algorithm logic.

 

Source Code:

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import javax.swing.JOptionPane;

public class BlowFishCipher {

public static void main(String[] args) throws Exception {

KeyGenerator keygenerator = KeyGenerator.getInstance("Blowfish");

// create a key

SecretKey secretkey = keygenerator.generateKey();

// create a cipher based upon Blowfish

Cipher cipher = Cipher.getInstance("Blowfish");

// initialise cipher to with secret key

cipher.init(Cipher.ENCRYPT_MODE, secretkey);

// get the text to encrypt

String inputText = JOptionPane.showInputDialog("Input your message: ");

// encrypt message

byte[] encrypted = cipher.doFinal(inputText.getBytes());

// re-initialise the cipher to be in decrypt mode

cipher.init(Cipher.DECRYPT_MODE, secretkey);

// decrypt message

byte[] decrypted = cipher.doFinal(encrypted);

// and display the results

JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),

"\nEncrypted text: " + new String(encrypted) + "\n" +

"\nDecrypted text: " + new String(decrypted));

System.exit(0);

}

}

 

Output:

Input your message: Hello world

Encrypted text: 3ooo&&(*&*4r4

Decrypted text: Hello world

 


6. PROGRAM TO IMPLEMENT RIJNDAEL ALGORITHM LOGIC

AIM: Write a C/JAVA program to implement the Rijndael algorithm logic.

 

Source Code:

import java.security.*;

import javax.crypto.*;

import javax.crypto.spec.*;

import java.io.*;

Public class AES {

public static String asHex (byte buf[]) {

StringBuffer strbuf = new StringBuffer(buf.length * 2);

int i;

for (i = 0; i < buf.length; i++) {

if (((int) buf[i] & 0xff) < 0x10)

strbuf.append("0");

strbuf.append(Long.toString((int) buf[i] & 0xff, 16)); }

return strbuf.toString(); }

public static void main(String[] args) throws Exception {

String message="AES still rocks!!";

// Get the KeyGenerator

KeyGenerator kgen = KeyGenerator.getInstance("AES");

kgen.init(128); // 192 and 256 bits may not be available

// Generate the secret key specs.

SecretKey skey = kgen.generateKey();

byte[] raw = skey.getEncoded();

SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

// Instantiate the cipher

Cipher cipher = Cipher.getInstance("AES");

cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

byte[] encrypted = cipher.doFinal((args.length == 0 ? message :

args[0]).getBytes());

System.out.println("encrypted string: " + asHex(encrypted));

cipher.init(Cipher.DECRYPT_MODE, skeySpec);

byte[] original = cipher.doFinal(encrypted);

String originalString=new String(original);

System.out.println("Original string: " + originalString + " " + asHex(original));

}

}

Output:

Enter the string: Welcome

String To Encrypt: Welcome

Encrypted Value: BPQGHKIoMNMwc0wKvg=

 

                                                 7. RC4 LOGIC

AIM: Write the RC4 logic in Java.

PROGRAM:  Encrypt a string using BlowFish algorithm

AIM: Using Java Cryptography, encrypt the text “Hello world” using BlowFish. Create your own key using Java keytool.

Source Code:

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import javax.swing.JOptionPane;

public class BlowFishCipher {

public static void main(String[] args) throws Exception {

// create a key generator based upon the Blowfish cipher

KeyGenerator keygenerator = KeyGenerator.getInstance("Blowfish");

// create a key

SecretKey secretkey = keygenerator.generateKey();

// create a cipher based upon Blowfish

Cipher cipher = Cipher.getInstance("Blowfish");

// initialise cipher to with secret key

cipher.init(Cipher.ENCRYPT_MODE, secretkey);

// get the text to encrypt

String inputText = JOptionPane.showInputDialog("Input your message: ");

// encrypt message

byte[] encrypted = cipher.doFinal(inputText.getBytes());

// re-initialise the cipher to be in decrypt mode

cipher.init(Cipher.DECRYPT_MODE, secretkey);

// decrypt message

byte[] decrypted = cipher.doFinal(encrypted);

// and display the results

JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),

"\nEncrypted text: " + new String(encrypted) + "\n" +

"\nDecrypted text: " + new String(decrypted));

System.exit(0);

}

}

 

Output:

Input your message: Hello world

Encrypted text: 3ooo&&(*&*4r4

Decrypted text: Hello world

 

                                                     8. RSA ALGORITHM

AIM: Write a Java program to implement RSA Algoithm.

Description:

RSA Algorithm is used to encrypt and decrypt data in modern computer systems and other electronic devices. RSA algorithm is an asymmetric cryptographic algorithm as it creates 2 different keys for the purpose of encryption and decryption. ... RSA makes use of prime numbers (arbitrary large numbers) to function.

Source Code:

import java.util.*;

public class RSA {

          public static void main(String args[]) {

                    Scanner sc=new Scanner(System.in);

                    int d=0,e,i;

                    double c,msg;

                    System.out.println("Enter the numbered message: ");

                    int m=sc.nextInt();

                    System.out.println("Enter two prime numbers: ");

                    int p=sc.nextInt();

                    int q=sc.nextInt();

                    int n=p*q;

                    int phi=(p-1)*(q-1);

                    System.out.println("the value of totent function = "+phi);                   

                    for(e=2;e<phi;e++) {

                              if(gcd(e,phi)==1)              

                                        break;

                    }

                    System.out.println("The value of e = "+e);                                

                    for(i=1;i<phi;i++) {

                              if((e*i)%phi == 1) {

                                        d=i;

                                        break;

                              }

                    }

                    System.out.println("The value of d = "+d);            

                    c=(Math.pow(m,e))%n;

                    System.out.println("Encrypted message is: ");

                    System.out.println(c);

                    msg=(Math.pow(c,d))%n;

                    System.out.println("Derypted message is: ");

                    System.out.println(msg);

          }

          static int gcd(int a,int b) {

                    if(a%b == 0)

                              return b;     

                    else

                              return gcd(b,a%b);

          }

Output:

Enter the numbered message:  2

Enter two prime numbers: 7 11

the value of totent function = 60

The value of e = 7

The value of d = 43

Encrypted message is:  51.0

Derypted message is:  12.0

 

 

 

 

9. DIFFIE-HELLMAN

AIM: Implement the Diffie-Hellman Key Exchange mechanism using HTML and JavaScript. Consider the end user as one of the parties (Alice) and the JavaScript application as other party (bob).

Description:

The Diffie-Hellmann key exchange is a secure method for exchanging cryptographic keys. This method allows two parties which have no prior knowledge of each other to establish a shared, secret key, even over an insecure channel.

Source Code:

import java.io.*;

import java.util.*;

 

class DiffieHellman {

          public static void main(String args[]) {

                    Scanner s=new Scanner(System.in);

                    System.out.println("Enter modulo(p)");

                    int p=s.nextInt();

                    System.out.println("Enter primitive root of "+p);

                    int g=s.nextInt();

                    System.out.println("Choose 1st secret no(Alice)");

                    int a=s.nextInt();

                    System.out.println("Choose 2nd secret no(BOB)");

                    int b=s.nextInt();

                    int A = (int)Math.pow(g,a)%p;

                    int B = (int)Math.pow(g,b)%p;

                    int S_A = (int)Math.pow(B,a)%p;

                    int S_B =(int)Math.pow(A,b)%p;

                    if(S_A==S_B) {

                              System.out.println("ALice and Bob can communicate with each other!!!");

                              System.out.println("They share a secret no = "+S_A);

                    }

                    else

                              System.out.println("ALice and Bob cannot communicate with each other!!!");

          }

}

 

Output:

Enter modulo(p) :  17

Enter primitive root of : 17 3

Choose 1st secret no(Alice) : 12

Choose 2nd secret no(BOB) : 14

ALice and Bob can communicate with each other!!!

They share a secret no = 16

 

 

                                        10. SHA-1

 

AIM: Calculate the message digest of a text using the SHA-1 algorithm in JAVA.

 

Description:

SHA-1 or Secure Hash Algorithm 1 is a cryptographic hash function which takes an input and produces a 160-bit (20-byte) hash value. This hash value is known as a message digest. ... To calculate cryptographic hashing value in Java, Message Digest Class is used, under the package java.security.

 

Source Code:

 

import java.util.*;

import java.io.IOException;

public class SHA {

    private static Random r = new Random(9999);

    public static void main(String args[]) throws Exception {

                    Scanner s = new Scanner(System.in);

                    String st = s.nextLine();

                    System.out.println("Encrypted string :" + encrypt(st));

                    System.out.println("Decrypted string :" + decrypt(encrypt(st)));

    }

    public static String encrypt(String str) {

                    Base64.Encoder e = Base64.getEncoder();

                    byte[] s = new byte[8];

                    r.nextBytes(s);

                    return e.encodeToString(s) + e.encodeToString(str.getBytes());

    }

    public static String decrypt(String estr) {

                    if (estr.length() > 12) {

                              String c = estr.substring(12);

                              Base64.Decoder d = Base64.getDecoder();

                    return new String(d.decode(c));

                    }

          return null;

   }

}

 

Output:

Hello

Encrypted string :5OKn4ZevXxA=SGVsbG8=

Decrypted string :Hello



 

                                                         11. MD5

AIM: Calculate the message digest(MD5) of a text using the SHA-1 algorithm in JAVA

Source Code:

import java.security.*;

public class MD5 {

public static void main(String[] a) {

// TODO code application logic here

try {

MessageDigest md = MessageDigest.getInstance("MD5");

System.out.println("Message digest object info: ");

System.out.println(" Algorithm = " +md.getAlgorithm());

System.out.println(" Provider = " +md.getProvider());

System.out.println(" ToString = " +md.toString());

String input = "";

md.update(input.getBytes());

byte[] output = md.digest();

System.out.println();

System.out.println("MD5(\""+input+"\") = " +bytesToHex(output));

input = "abc";

md.update(input.getBytes());

output = md.digest();

System.out.println();

System.out.println("MD5(\""+input+"\") = " +bytesToHex(output));

input = "abcdefghijklmnopqrstuvwxyz";

md.update(input.getBytes());

output = md.digest();

System.out.println();

System.out.println("MD5(\"" +input+"\") = " +bytesToHex(output));

System.out.println("");

}

catch (Exception e) {

System.out.println("Exception: " +e); }

}

public static String bytesToHex(byte[] b) {

char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

StringBufferbuf = new StringBuffer();

for (int j=0; j<b.length; j++) {

buf.append(hexDigit[(b[j] >> 4) & 0x0f]);

buf.append(hexDigit[b[j] & 0x0f]); }

return buf.toString();

}

}

Output:

Message digest object info:

Algorithm = MD5

Provider = SUN version 1.6

ToString = MD5 Message Digest from SUN, <initialized>

MD5("") = D41D8CD98F00B204E9800998ECF8427E

MD5("abc") = 900150983CD24FB0D6963F7D28E17F72

MD5("abcdefghijklmnopqrstuvwxyz") =  C3FCD3D76192E4007DFB496CCA67E13B