Klick Micro
Klick Micro
1 2 3 4
9 10 11 12
4. Implementation of subset-sum.                                                                                                                                                                      5. Authenticating the given signature using the MD5 hash algorithm.                    Output:
                                                                                              Output:
   Code:                                                                                                                                                                                                   Code:
   class SUBSETSUM {                                                                                                                                                                                       import java.math.BigInteger;
   static boolean isSubsetSum(int set[],                                                                                                                                                                   import java.security.MessageDigest;
   int n, int sum)                                                                                                                                                                                         import java.security.NoSuchAlgorithmException;
   {                                                                                                                                                                                                       public class MD5Example
   // Base Cases                                                                                                                                                                                           {
   if (sum == 0)                                                                                                                                                                                           //hash function to get the md5 hash
   return true;                                                                                                                                                                                            public static String getMd5Hash(String input)
   if (n == 0 && sum != 0)                                                                                                                                                                                 {
   return false;                                                                                                                                                                                           try
                                                                                                                                                                                                           {
   // If last element is greater than                                                                                                                                                                      //static getInstance() method is called with hashing MD5
   // sum, then ignore it                                                                                                                                                                                  MessageDigest md = MessageDigest.getInstance("MD5");
   if (set[n - 1] > sum)                                                                                                                                                                                   //calculating message digest of an input that return array of byte
   return isSubsetSum(set, n - 1, sum);                                                                                                                                                                    byte[] messageDigest = md.digest(input.getBytes());
                                                                                                                                                                                                           //converting byte array into signum representation
   /* else, check if sum can be obtained                                                                                                                                                                   BigInteger no = new BigInteger(1, messageDigest);
   by any of the following                                                                                                                                                                                 //converting message digest into hex value
   (a) including the last element                                                                                                                                                                          String hashtext = no.toString(16);
   (b) excluding the last element */                                                                                                                                                                       while (hashtext.length() < 32)
   return isSubsetSum(set, n - 1, sum) || isSubsetSum(set, n - 1, sum - set[n - 1]);                                                                                                                       {
   }                                                                                                                                                                                                       hashtext = "0" + hashtext;
                                                                                                                                                                                                           }
   public static void main(String args[])                                                                                                                                                                  return hashtext;
   {                                                                                                                                                                                                       }
   int set[] = { 3, 34, 4, 12, 5, 2 };                                                                                                                                                                     //for specifying wrong message digest algorithms
   int sum = 9;                                                                                                                                                                                            catch (NoSuchAlgorithmException e)
   int n = set.length;                                                                                                                                                                                     {
   if (isSubsetSum(set, n, sum) == true)                                                                                                                                                                   throw new RuntimeException(e);
   System.out.println("Found a subset"                                                                                                                                                                     }}
   + " with given sum");                                                                                                                                                                                   //driver code
   else                                                                                                                                                                                                    public static void main(String args[]) throws NoSuchAlgorithmException
   System.out.println("No subset with"                                                                                                                                                                     {
   + " given sum");                                                                                                                                                                                        String s = "javatpoint";
   }                                                                                                                                                                                                       System.out.println("HashCode Generated for the string is: " + getMd5Hash(s));
   }                                                                                                                                                                                                       }}
                                            13                                                                                             14                                                                                                    15                                                                               16
                                                                                              {                                                                                                                                                                                }
6. Implementation of Diffie-Hellman algorithm.                                                long result = 0;                                                                     7. Implementation of the EL Gamal cryptosystem.                                             else
                                                                                              if (y == 1){                                                                                                                                                                     System.out.println("Sorry, a generator for your prime couldn't be found.");
                                                                                              return x;                                                                                                                                                                        }
  Code:                                                                                                                                                                              Code:
                                                                                              }                                                                                                                                                                                public static BigInteger getNextPrime(String ans) {
  import java.util.*;                                                                                                                                                                import java.util.*;
                                                                                              else{                                                                                                                                                                            BigInteger one = new BigInteger("1");
  // create class DiffieHellmanAlgorithmExample to calculate the key for two persons                                                                                                 import java.math.BigInteger;
                                                                                              result = ((long)Math.pow(x, y)) % P;                                                                                                                                             BigInteger test = new BigInteger(ans);
  class DiffieHellmanAlgorithmExample {
                                                                                              return result;                                                                                                                                                                   while (!test.isProbablePrime(99))
  // main() method start                                                                                                                                                             public class ElGamal {
                                                                                              }                                                                                                                                                                                test = test.add(one);
  public static void main(String[] args)                                                                                                                                             public static void main(String[] args) {
                                                                                              }                                                                                                                                                                                return test;
  {                                                                                                                                                                                  Scanner stdin = new Scanner(System.in);
                                                                                              }                                                                                                                                                                                }
  long P, G, x, a, y, b, ka, kb;                                                                                                                                                     Random r = new Random();
                                                                                                                                                                                                                                                                               public static BigInteger getGenerator(BigInteger p, Random r) {
  // create Scanner class object to take input from user                                                                                                                             System.out.println("Enter the approximate value of the prime number for your El
                                                                                              Output:                                                                                                                                                                          int numtries = 0;
  Scanner sc = new Scanner(System.in);                                                                                                                                               Gamal key.");
  System.out.println("Both the users should be agreed upon the public keys G and P");                                                                                                BigInteger p = getNextPrime(stdin.next());
                                                                                                                                                                                                                                                                               while (numtries < 1000) {
  // take inputs for public keys from the user                                                                                                                                       BigInteger g = getGenerator(p, r);
                                                                                                                                                                                                                                                                               BigInteger rand = new BigInteger(p.bitLength()+1,r);
  System.out.println("Enter value for public key G:");                                                                                                                               if (g != null) {
                                                                                                                                                                                                                                                                               rand = rand.mod(p);
  G = sc.nextLong();                                                                                                                                                                 BigInteger a = new BigInteger(p.bitLength()+1, r);
  System.out.println("Enter value for public key P:");                                                                                                                               a = a.mod(p);
                                                                                                                                                                                                                                                                               BigInteger exp = BigInteger.ONE;
  P = sc.nextLong();                                                                                                                                                                 if (a.equals(BigInteger.ZERO) || a.equals(BigInteger.ONE))
                                                                                                                                                                                                                                                                               BigInteger next = rand;
  // get input from user for private keys a and b selected by User1 and User2                                                                                                        a = a.add(new BigInteger("2"));
                                                                                                                                                                                                                                                                               if (next.equals(BigInteger.ZERO) || next.equals(BigInteger.ONE))
  System.out.println("Enter value for private key a selected by user1:");                                                                                                            BigInteger b = g.modPow(a, p);
                                                                                                                                                                                                                                                                               continue;
  a = sc.nextLong();                                                                                                                                                                 System.out.println("Post p = "+p+" g = "+g+" b = "+b);
                                                                                                                                                                                                                                                                               while (!next.equals(BigInteger.ONE)) {
  System.out.println("Enter value for private key b selected by user2:");                                                                                                            BigInteger k = new BigInteger(p.bitLength()+1, r);
                                                                                                                                                                                                                                                                               next = (next.multiply(rand)).mod(p);
  b = sc.nextLong();                                                                                                                                                                 k = k.mod(p);
                                                                                                                                                                                                                                                                               exp = exp.add(BigInteger.ONE);
  // call calculatePower() method to generate x and y keys                                                                                                                           if (k.equals(BigInteger.ZERO) || k.equals(BigInteger.ONE))
                                                                                                                                                                                                                                                                               }
  x = calculatePower(G, a, P);                                                                                                                                                       k = k.add(new BigInteger("2"));
                                                                                                                                                                                                                                                                               if (exp.equals(p.subtract(BigInteger.ONE)))
  y = calculatePower(G, b, P);                                                                                                                                                       BigInteger c1 = g.modPow(k, p);
                                                                                                                                                                                                                                                                               return rand;
  // call calculatePower() method to generate ka and kb secret keys after the exchange of                                                                                            BigInteger c2 = b.modPow(k, p);
  x and y keys                                                                                                                                                                       System.out.println("Please enter your message. It should be in between 1 and "+p);
                                                                                                                                                                                                                                                                               numtries++;
  // calculate secret key for User1                                                                                                                                                  BigInteger m = new BigInteger(stdin.next());
                                                                                                                                                                                                                                                                               }
  ka = calculatePower(y, a, P);                                                                                                                                                      c2 = c2.multiply(m);
                                                                                                                                                                                                                                                                               return null;
  // calculate secret key for User2                                                                                                                                                  c2 = c2.mod(p);
                                                                                                                                                                                                                                                                               }
  kb = calculatePower(x, b, P);                                                                                                                                                      System.out.println("The corresponding cipher texts are c1 = "+c1+" c2 = "+c2);
  // print secret keys of user1 and user2                                                                                                                                            BigInteger temp = c1.modPow(a,p);
                                                                                                                                                                                                                                                                               }
  System.out.println("Secret key for User1 is:" + ka);                                                                                                                               temp = temp.modInverse(p);
  System.out.println("Secret key for User2 is:" + kb);                                                                                                                               System.out.println("Here is c1^ -a = "+temp);
  }                                                                                                                                                                                  BigInteger recover = temp.multiply(c2);
  // create calculatePower() method to find the value of x ^ y mod P                                                                                                                 recover = recover.mod(p);
  private static long calculatePower(long x, long y, long P)                                                                                                                         System.out.println("The original message = "+recover);
17 18 19 20
  public static BigInteger CoprimeGenerator(BigInteger Modulus,int Length){                   if(RawData[i])Ciphertext[i]=integer.pow(2).mod(PuKP.Modulus);                                                                                                                    public static BigInteger[] decrypt(BigInteger c,
  BigInteger CoprimeNum;                                                                      else                                                                                 9. Implementation of Rabin Cryptosystem.                                                    BigInteger p,
  //BigInteger a,b,residue;                                                                   Ciphertext[i]=integer.pow(2).multiply(PuKP.Q_Nonresidue).mod(PuKP.Modulus);
  Random rnd;                                                                                 }                                                                                                                                                                                BigInteger q)
                                                                                                                                                                                     Code:
  do{                                                                                         return Ciphertext;
  rnd=new Random(new Date().getTime());                                                                                                                                                                                                                                        {
                                                                                              }                                                                                      import java.math.BigInteger;
  CoprimeNum=BigInteger.probablePrime(Length, rnd);                                           public static byte decrypt(BigInteger[] Ciphertext,PrivateKeyPair PrKP) throws                                                                                                   BigInteger n = p.multiply(q);
  /*a=Modulus;                                                                                Exception{                                                                             import java.nio.charset.Charset;
  b=CoprimeNum;                                                                               byte Plaintext;                                                                                                                                                                  BigInteger p1 = c.modPow(p.add(BigInteger.ONE).divide(fourth),p);
                                                                                                                                                                                     import java.security.SecureRandom;
  residue=new BigInteger("0");                                                                boolean a=false,b=false;                                                                                                                                                         BigInteger p2 = p.subtract(p1);
  do{                                                                                         boolean[] RawData=new boolean[8];                                                      import java.util.Random;
  residue=a.mod(b);                                                                           for(int i=0;i<Ciphertext.length;i++){                                                                                                                                            BigInteger q1 = c.modPow(q.add(BigInteger.ONE).divide(fourth),q);
  a=b;                                                                                        a=LegendreSymbolCalculate(Ciphertext[i],PrKP.Pr1);                                                                                                                               BigInteger q2 = q.subtract(q1);
  b=residue;                                                                                  b=LegendreSymbolCalculate(Ciphertext[i],PrKP.Pr2);                                     class CryptographyClassic {
  }while(residue.compareTo(new BigInteger("0"))==0);                                          if(a&&b)RawData[i]=true;                                                                                                                                                         BigInteger[] ext = Gcd(p, q);
                                                                                                                                                                                     private static Random random = new SecureRandom();
  }while(a.compareTo(new BigInteger("1"))!=0);*/                                              else RawData[i]=false;
                                                                                                                                                                                                                                                                               BigInteger yp = ext[1];
  }while(CoprimeNum.gcd(Modulus).compareTo(new BigInteger("1"))!=0);//already                 }                                                                                      private static BigInteger second = BigInteger.valueOf(2);
  had a gcd algorithm                                                                         Plaintext=BooleanToByte(RawData);                                                                                                                                                BigInteger yq = ext[2];
                                                                                                                                                                                     private static BigInteger third = BigInteger.valueOf(3);
  return CoprimeNum;                                                                          return Plaintext;
                                                                                                                                                                                                                                                                               BigInteger d1 = yp.multiply(p).multiply(q1).add(yq.multiply(q).multiply(p1)).mod(n);
  }                                                                                           }                                                                                      private static BigInteger fourth = BigInteger.valueOf(4);
  public static PublicKeyPair PuKGenerator(PrivateKeyPair PrKP) throws Exception{             }                                                                                                                                                                                BigInteger d2 = yp.multiply(p).multiply(q2).add(yq.multiply(q).multiply(p1)).mod(n);
                                                                                                                                                                                     public static BigInteger[] generateKey(int Bitlength)
  PublicKeyPair PuKP=new PublicKeyPair();
                                                                                                                                                                                                                                                                               BigInteger d3 = yp.multiply(p).multiply(q1).add(yq.multiply(q).multiply(p2)).mod(n);
  boolean a,b;                                                                                Output:                                                                                {
  BigInteger Coprime;                                                                                                                                                                                                                                                          BigInteger d4 = yp.multiply(p).multiply(q2).add(yq.multiply(q).multiply(p2)).mod(n);
                                                                                                                                                                                     BigInteger p1 = blumPrime(Bitlength / 2);
  PuKP.Modulus=PrKP.Pr1.multiply(PrKP.Pr2);
  do{                                                                                                                                                                                                                                                                          return new BigInteger[] { d1, d2, d3, d4 };
                                                                                                                                                                                     BigInteger q1 = blumPrime(Bitlength / 2);
  Coprime=CoprimeGenerator(PuKP.Modulus,PuKP.Modulus.bitLength());                                                                                                                                                                                                             }
  a=LegendreSymbolCalculate(Coprime,PrKP.Pr1);                                                                                                                                       BigInteger n = p1.multiply(q1);
  b=LegendreSymbolCalculate(Coprime,PrKP.Pr2);                                                                                                                                                                                                                                 public static BigInteger[] Gcd(BigInteger a, BigInteger b)
                                                                                                                                                                                     return new BigInteger[] { n, p1, q1 };
  }while(a||b);//a||b then y may or may not be a pseudo quadratic residue,a&&b then y                                                                                                                                                                                          {
  must be a pseudo quadratic residues                                                                                                                                                }
  PuKP.Q_Nonresidue=Coprime;                                                                                                                                                                                                                                                   BigInteger s = BigInteger.ZERO;
                                                                                                                                                                                     public static BigInteger encrypt(BigInteger m,
  return PuKP;                                                                                                                                                                                                                                                                 BigInteger olds = BigInteger.ONE;
  }                                                                                                                                                                                  BigInteger n)
  public static BigInteger[] encrypt(byte Plaintext,PublicKeyPair PuKP){                                                                                                                                                                                                       BigInteger t = BigInteger.ONE;
                                                                                                                                                                                     {
  BigInteger[] Ciphertext=new BigInteger[8];
                                                                                                                                                                                                                                                                               BigInteger oldt = BigInteger.ZERO;
  BigInteger integer;                                                                                                                                                                return m.modPow(second, n);
  boolean[] RawData=ByteToBoolean(Plaintext);                                                                                                                                                                                                                                  BigInteger r = b;
                                                                                                                                                                                     }
  for(int i=0;i<RawData.length;i++){
                                                                                                                                                                                                                                                                               BigInteger oldr = a;
  integer=CoprimeGenerator(PuKP.Modulus,PuKP.Modulus.bitLength());
                                            25                                                                                       26                                                                                       27                                                                                       28
{ Charset.forName("ascii"));
BigInteger p; if (dec.equals(s)) {
do { finalMessage = dec;
p = BigInteger.probablePrime(bitLength, random); }
return p; }
} System.out.println(
class RabinCryptoSystem { }
                                            29                                                                                       30                                                                                       31                                                                                       32
                                                                                                                                                                 String cleartextFile = "cleartext.txt";
     14.    Message authentication codes.                        15.      Elliptic curve cryptosystems                                                           String ciphertextFile = "ciphertextECIES.txt";
43 44 45 50
57