0 ratings0% found this document useful (0 votes) 35 views2 pagesRSA Algorithm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
oon =) 1]
limport java.math.BigInteger;
ents
rads
priya
ree
je
led Folder
Space
k
m
3 impo
4 publ
5 Bigl
6 int
1p =
13.N =
7 Random r;
8 public RSA() {
96 = new Random();
10 // returns a prime number
BigInteger. probableprii
12q = BigInteger.
14.// In c equival
15 phi =
‘\Aee = Biginte.
26 DataInputstream
.|27 Strin
rt java.io.*;
ic class RSA {
nteger p, q,
bitlength = 100;
P.multiply(q);
ent — ph
= p.subtract(Bigint
Tsa = new RSA();
in=new 0.
9 teststring ;
ProbablePrime(bit
2 import java.util.Random;
N, phi, e, d;
with in the specified bit length
me(bitlength, r);
length, r);
t= (p-1)*(q-1)
eger.ONE)
jnultiply(q.subtract(Biginteger.oNE));
ger -probablePrime(bitlength/2, );
W// In Clequivatent - while ( phi.gcd(e)\> 1/2 e < phi )
18 while (pht..gcd(e) .compareto(Biginteger ONE) > 0 && e.compareto(phi) < 0 ) ¢
19 e-add(Biginteger. one); // In © equivalent. e+1
20}
21d = e.modinverse(phi);
22}
23 7// RSA Main
24 public static void main(
25 RSA
Srtngl] args) throws toexceptiony
‘atainputstream(system.in);
38 Systen. out. printin('enter the plain PeRERM)
29 teststring-in. readi ine();
30 System. out.printin( Encryptilig String: teststring);
31, Systen.out.printin( tring + bytesTostring(teststring. getaytes()));
32/7 encrypt
33 byte[] encrypted = reprencryet(teststring getaytes());
34 Ty Rens Out PrtnelnCahcryp ual Strtng \ta\aytes: ey bytesTostring(encrypted));
lecrypt { SAVERS
36 byteL] decrypted + fe2-decryptCencrypted); SS
n¢ RAE HYRRRA SE tag ty Bub es sO bytesTostring(decrypted)) a
ACIDG SH VAtRMStctngs (en SORA SEER CSS a
ng HANA dae cy cee et RN NS19 e.add(BigInteger ONE) }!'//'
26
aed = e.modInverse( phi);
22}
23 // RSA Main
24 public static void main(String[ ]) args) throws ToExceptton{ JAGAN \
25 RSA rsa = new RSA();
26 DataInputstream in=new DataInputstream(systen. 3 i Pash
27 String teststring ; \ avy
28 Systen.out.println("Enter the plain text:!)3))!)\ : t
29 teststring=in.readLine(); i | Prada
30 System.out.println("Encrypting String: " + teststring);, | | iste
31 System.out.println("String in Bytes: " + bytesTostring(teststring. getBytes())); i
32 // encrypt
33 byte[] encrypted = rsa. encrypt(teststring. getBytes());
34 System. out.println("Encrypted String tn Bytes: " + bytestostring(encrypted)); }
35 // decrypt | iit
36 byte[] decrypted = rsa-decrypt(encrypted); TN EAA
37 System.out.printtn("Decrypted String in Bytes; |'\ + bytesToString(decrypted));
38 System.out.printLn("Decrypted string: ")3) AR
39 System. out. printLn(new String(decrypted)); {
40} ! { \ nail
41 private static String bytestostring(byteL] encrypted) WANA
42 String test = \ i
43 for (byte b : encrypted) {
44 test += Byte. toString(b);
45} i
46 return test; Hilt
47}
48 // Encrypt message
49 public byte[] encrypt(byte[] message) {
50 return (new BigInteger(message)),modPow(e, N). toByteArray(.);
51} \
52 // Decrypt message UT eee
53 public byte[] decrypt(byte[] ada { \ |
54 return (new Btornteget (es sauH) modPow(d,| N). toby tearray();\ \ ut
SS ity | i ,
56 }| ! \ i Hit
You might also like
INS 3,4,5,6,7,8,9
INS 3,4,5,6,7,8,9
26 pages