Showing posts with label Information and Network Security. Show all posts
Showing posts with label Information and Network Security. Show all posts

Sunday, 16 June 2019

Write a program to implement Digital Signature Algorithm. Information and Network Security (2170709) GTU



Algorithm/Steps:
              For the signing process, A signs a message m by the following procedure:
1. Select a random, secret integer k, such that 0 < k < q − 1.
2. Compute r = (α^k(mod p)) (mod q)
3. Compute s = (k^(−1)(m + ar)) (mod q)
4. A sends the signature (m, r, s) for m to B.

For the verification process, B verifies the signature by the following procedure:
1. B can use A’s public information (p, q, α, β) = (23929, 997, 20424, 1483).
2. Compute u1 ≡ s^(−1) m (mod q), and u2 ≡ s−1 r (mod q). 3. Compute v = (α^u1*β^u2(mod p)) (mod q) 4. B accepts the signature iff v = r. Steps: 1. select public keys (p, q, α, β) 2. sign a message m (0 < m < q − 1) with a random k and report (m, r, s).
3. verifies (m, r, s) for m based on the public key (p, q, α, β).

Code:
import java.math.*;
import java.util.*;
import java.security.*;
import java.io.*;
public class dsa
{
 public static void main(String[] args) throws IOException
{
Scanner sca = new Scanner(System.in);
BigInteger p, b, c, secretKey;
 Random sc = new SecureRandom();
 secretKey = new BigInteger("12345678901234567890");
 System.out.println("secretKey = " + secretKey);
p = BigInteger.probablePrime(64, sc);
b = new BigInteger("3");
c = b.modPow(secretKey, p);
System.out.println("p = " + p);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.print("Enter your Big Number message -->");
String s = sca.nextLine();
BigInteger X = new BigInteger(s);
BigInteger r = new BigInteger(64, sc);
BigInteger EC = X.multiply(c.modPow(r, p)).mod(p);
BigInteger brmodp = b.modPow(r, p);
System.out.println("Plaintext = " + X);
System.out.println("r = " + r);
System.out.println("EC = " + EC);
System.out.println("b^r mod p = " + brmodp);
BigInteger crmodp = brmodp.modPow(secretKey, p);
BigInteger d = crmodp.modInverse(p);
BigInteger ad = d.multiply(EC).mod(p);
System.out.println("\n\nc^r mod p = " + crmodp);
System.out.println("d = " + d);
System.out.println("A decodes: " + ad);
}
} 

Write a program to implement Diffie-Hellman Key Exchange algorithm. Information and Network Security (2170709) GTU



Algorithm/Steps:
1. A and B get public numbers P = 23, G = 9
2. A selected a private key a = 4 and B selected a private key b = 3
3. A and B compute public values
A:    x =(9^4 mod 23) = (6561 mod 23) = 6
B:    y = (9^3 mod 23) = (729 mod 23)  = 16
4. A and B exchange public numbers
5. A receives public key y =16 and B receives public key x = 6
6. A and B compute symmetric keys
A:  ka = y^a mod p = 65536 mod 23 = 9
B:    kb = x^b mod p = 216 mod 23 = 9
7. 9 is the shared secret.
Code:
import java.util.*;
import java.lang.*;
import java.math.BigInteger;
class Main
{
            public static void main(String args[])
            {
             Scanner sc = new Scanner(System.in);
            System.out.println("Enter the value of Xa&Xb");
            BigInteger Xa=  BigInteger.valueOf(sc.nextInt());
            BigInteger Xb=  BigInteger.valueOf(sc.nextInt());
            System.out.println("Enter a Prime no. p");
            BigInteger p =  BigInteger.valueOf(sc.nextInt());
            System.out.println("Enter Primitive Root a, such that a<p");
            BigInteger a =  BigInteger.valueOf(sc.nextInt());
            BigInteger Ya=  a.pow(Xa.intValue()).mod(p);
            BigInteger Yb=  a.pow(Xb.intValue()).mod(p);
            BigInteger Ka =  Yb.pow(Xa.intValue()).mod(p);
            BigInteger Kb=  Ya.pow(Xb.intValue()).mod(p);
            if(Ka.compareTo(Kb)==0)
             {
                        System.out.println("Transmission successful"+Ya+" "+Yb+" "+Ka+" "+Kb);}
            else{
                        System.out.println("Transmission failed"+Ya+" "+Yb+" "+Ka+" "+Kb);}
            }
}

Write a program to implement RSA Algorithm. Information and Network Security (2170709) GTU



Algorithm/Steps:
               1. First take the plain text in one input file i.e say input.txt.
               2. Then ask the user to enter the necessary input data to encrypt the plain text.
               3. Read the text (character by character) from the file and generate the cipher text using
               RSA algorithm encryption C = me mod N..
               4. Then the cipher text must be saved in separate output file i.e say output.txt.
               5. After encryption, in similar manner perform the decryption using m = cd mod N
      which will give us the original plain text.
Code:
import java.math.BigInteger;
import java.util.Scanner;
public class RSA_Array {
            public static int gcd(int a,int b)
            {
                        int temp;
                        do
                        {
                                    temp=a%b;
                                    a=b;
                                    b=temp;
                        }while(temp!=0);
                        return a;
            }
            public static void main(String args[])
            {
                        Scanner sc = new Scanner(System.in);
                        Scanner sc1 = new Scanner(System.in);
                        System.out.println("Enter plain text");
                        String m = sc.nextLine();
                        m = m.toUpperCase();
                        int cipher_m[]  = new int[m.length()];
                        System.out.println("Enter P");
                        int P = sc.nextInt();
                        System.out.println("Enter Q");
                        int Q = sc.nextInt();
                        int n=P*Q;
                        int fn = (P-1)*(Q-1);
                        int temp;
                        int i=2;
                        do
                        {
                                    temp = gcd(i,fn);
                                    i++;
                        }while(temp!=1);
                        i--;
                        //System.out.println(i);
                        int e=i;
                        System.out.println("Enter e if not than enter 0");
                        int temp3 = sc.nextInt();
                        if(temp3!=0)
                        {
                                    e = temp3;
                        }
                        int temp2;
                        int k=0;
                        do
                        {
                                    temp2 = (1+k*fn)%e;
                                    k++;
                        }while(temp2!=0);
                        k--;
                        int d = (1+k*fn)/e;
                        //System.out.println(d);
                        double c = 0;
                        for(int z=0;z<m.length();z++)
                        {
                                    int l;
                                    l = (int)m.charAt(z)-64;
                                    //System.out.println(l);
                                    if(l<n)
                                    {
                                                c = Math.pow(l,e)%n;
                                    }
                        //System.out.println("Cipher Text="+c);
                        cipher_m[z]= (int) c;
                        }
                        /*BigInteger bi1, bi2, bi3;
                        BigInteger exponent = new BigInteger(String.valueOf(d));
                  bi1 = new BigInteger(String.valueOf((int)c));
                  bi2 = new BigInteger(String.valueOf(n));

                  // perform modPow operation on bi1 using bi2 and exp
                  bi3 = bi1.modPow(exponent, bi2);
                        System.out.println("Plain Text = "+bi3);*/
                        System.out.println("Cipher Text");
                        System.out.println("Integer Form");
                        for(int z=0;z<cipher_m.length;z++)
                                    System.out.print(cipher_m[z]+",");
                        System.out.println("\nCharacter Form");
                        for(int z=0;z<cipher_m.length;z++)
                                    System.out.print((char)(cipher_m[z]+64)+",");
                        int plain_m[] = new int[cipher_m.length];
                        for(int z=0;z<cipher_m.length;z++)
                        {
                                    BigInteger bi1, bi2, bi3;
                                    BigInteger exponent = new BigInteger(String.valueOf(d));
                              bi1 = new BigInteger(String.valueOf(cipher_m[z]));
                              bi2 = new BigInteger(String.valueOf(n));
                              // perform modPow operation on bi1 using bi2 and exp
                              bi3 = bi1.modPow(exponent, bi2);
                              plain_m[z] = bi3.intValue();
                                    //System.out.println("Plain Text = "+bi3);
                        }
                        System.out.println("\n\nPlain Text");
                        System.out.println("Integer Form");
                        for(int z=0;z<cipher_m.length;z++)
                                    System.out.print(plain_m[z]+",");
                        System.out.println("\nCharacter Form");
                        for(int z=0;z<cipher_m.length;z++)
                                    System.out.print((char)(plain_m[z]+64)+",");                        
            }
}

It's time To increase blogging capability. To have a chance to contribute in digital world. Any Interested People who want to make t...