0% found this document useful (0 votes)
11 views2 pages

RSA Algorithm

Rsa 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
0% found this document useful (0 votes)
11 views2 pages

RSA Algorithm

Rsa 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 [Link]; 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 [Link].*; ic class RSA { nteger p, q, bitlength = 100; [Link](q); ent — ph = [Link](Bigint Tsa = new RSA(); in=new 0. 9 teststring ; ProbablePrime(bit 2 import [Link]; N, phi, e, d; with in the specified bit length me(bitlength, r); length, r); t= (p-1)*(q-1) [Link]) jnultiply([Link]([Link])); ger -probablePrime(bitlength/2, ); W// In Clequivatent - while ( [Link](e)\> 1/2 e < phi ) 18 while (pht..gcd(e) .compareto(Biginteger ONE) > 0 && [Link](phi) < 0 ) ¢ 19 e-add(Biginteger. one); // In © equivalent. e+1 20} 21d = [Link](phi); 22} 23 7// RSA Main 24 public static void main( 25 RSA Srtngl] args) throws toexceptiony ‘atainputstream([Link]); 38 Systen. out. printin('enter the plain PeRERM) 29 teststring-in. readi ine(); 30 System. [Link]( Encryptilig String: teststring); 31, [Link]( 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 NS 19 [Link](BigInteger ONE) }!'//' 26 aed = [Link]( 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 [Link]("Enter the plain text:!)3))!)\ : t 29 teststring=[Link](); i | Prada 30 [Link]("Encrypting String: " + teststring);, | | iste 31 [Link]("String in Bytes: " + bytesTostring(teststring. getBytes())); i 32 // encrypt 33 byte[] encrypted = rsa. encrypt(teststring. getBytes()); 34 System. [Link]("Encrypted String tn Bytes: " + bytestostring(encrypted)); } 35 // decrypt | iit 36 byte[] decrypted = rsa-decrypt(encrypted); TN EAA 37 [Link]("Decrypted String in Bytes; |'\ + bytesToString(decrypted)); 38 [Link]("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