|
| 1 | +import java.math.*; |
| 2 | +import java.io.*; |
| 3 | +import java.security.*; |
| 4 | +/* |
| 5 | + ALGORITHM |
| 6 | + ========== |
| 7 | + STEP 1 > pick 2 large prime numbers , say 'p' and 'q' |
| 8 | +
|
| 9 | + STEP 2 > Compute their product, say n = p * q |
| 10 | +
|
| 11 | + STEP 3 > Compute PHI(n) as PHI(n) = (p-1) * (q-1) |
| 12 | +
|
| 13 | + STEP 4 > pick a value 'e' such that GCD(e,PHI(n))=1 , here 'e' is the encrytion key |
| 14 | +
|
| 15 | + STEP 5 > calculate 'd' as d = e^(-1) mod PHI(n) , here 'd' is the decryption key |
| 16 | +
|
| 17 | + STEP 6 > now we have our PublicKey=(e,n) and PrivateKey=(d,n) |
| 18 | +
|
| 19 | + STEP 7 > Encryption : CipherText = (Message)^e mod n |
| 20 | +
|
| 21 | + STEP 8 > Decryption : PlainMsg = (CipherText)^d mod n |
| 22 | + |
| 23 | +*/ |
| 24 | + |
| 25 | +class ED_helper |
| 26 | +{ |
| 27 | + private int bitlen,r; |
| 28 | + private BigInteger p,q,n,phi,e,d; |
| 29 | + |
| 30 | + ED_helper(int bit) |
| 31 | + { |
| 32 | + bitlen=bit;//number of bits strong we want our encryption to be |
| 33 | + SecureRandom r=new SecureRandom();//we use this because its a 128bit rand_generator,hence low chances of repeats. |
| 34 | + |
| 35 | + //<================================< STEP 1 >======================================> |
| 36 | + |
| 37 | + //SYNTAX |
| 38 | + //BigInteger(int bitLength, int certainty, Random rnd) |
| 39 | + |
| 40 | + //we want to generate encryption keys as a product of p and q hence each of p and q much contain half as many digits |
| 41 | + //as bitlen.And the second value to the BigInt tells us we want to generate primes with 100% certainity using 'r' |
| 42 | + p=new BigInteger(bitlen/2,100,r); |
| 43 | + q=new BigInteger(bitlen/2,100,r); |
| 44 | + |
| 45 | + System.out.println("P value :"+p+"\n\n\n "+"Q value="+q); |
| 46 | + |
| 47 | + //<================================< STEP 2 >======================================> |
| 48 | + n=p.multiply(q);//BigInteger Multiplication |
| 49 | + |
| 50 | + //<================================< STEP 3 >======================================> |
| 51 | + phi=p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE)); |
| 52 | + |
| 53 | + //<================================< STEP 4 >======================================> |
| 54 | + e=new BigInteger(bitlen/2,100,r);//BigInteger will take care of GCD() <UNDERATED STEP> |
| 55 | + |
| 56 | + //<================================< STEP 5 >======================================> |
| 57 | + d=e.modInverse(phi);//e^-1 then % phi |
| 58 | + |
| 59 | + //<================================< STEP 6 >======================================> |
| 60 | + System.out.println("\n\n\nValue of D :"+d+"\n\n\nValue of E : "+e);//Displaying keys |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + //<================================< STEP 7 >======================================> |
| 66 | + public BigInteger encrypt(BigInteger Msg) |
| 67 | + { |
| 68 | + //CipherText = (Msg)^e mod n |
| 69 | + return(Msg.modPow(e,n)); |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + //<================================< STEP 8 >======================================> |
| 74 | + public BigInteger decrypt(BigInteger Msg) |
| 75 | + { |
| 76 | + //DecodedMsg = (Ciphext)^d mod n |
| 77 | + return(Msg.modPow(d,n)); |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | +} |
| 83 | + |
| 84 | +//Main Driver Class to demonstrate functionalities implemented by the above class |
| 85 | +class ED |
| 86 | +{ |
| 87 | + |
| 88 | + public static void main(String args[]) throws IOException |
| 89 | + { |
| 90 | + //Create object and pass the bit length |
| 91 | + ED_helper rsa=new ED_helper(1200); |
| 92 | + |
| 93 | + BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); |
| 94 | + |
| 95 | + String text1,text2; |
| 96 | + BigInteger Pt,Ct; |
| 97 | + |
| 98 | + //Input Message from user |
| 99 | + System.out.println("\n\nEnter Plaintext :"); |
| 100 | + text1=br.readLine(); |
| 101 | + |
| 102 | + //pass the input message to the encrypt method to obtain cipher text |
| 103 | + Pt=new BigInteger(text1.getBytes()); |
| 104 | + Ct=rsa.encrypt(Pt); |
| 105 | + System.out.println("\n\nCiperText is : "+Ct); |
| 106 | + |
| 107 | + //Pass the CipherText to the decrypt method to obtain original message |
| 108 | + Pt=rsa.decrypt(Ct); |
| 109 | + text2=new String(Pt.toByteArray()); |
| 110 | + System.out.println("\n\n\nData after decrypt :"+text2); |
| 111 | + } |
| 112 | + |
| 113 | +} |
0 commit comments