// RSA basic implementation Edition
Import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstream; import Java. io. inputstreamreader; import Java. math. biginteger; import Java. util. random; import Java. util. logging; import Org. OMG. CORBA. public_member; public class rsaframe {private RSA = New RSA (); Private biginteger p, q, E, N, D, ran; private random RND = new random (); // global variable private int numbit = 10; public static void main (string [] ARGs) {rsaframe myrsa = new rsaframe (); myrsa. beforemessage (); // calculate the value of P, Q, N, ran, E, D biginteger [] encodingm = myrsa. encryption (); // encrypt the input information M. myrsa. decryption (encodingm); // decrypts the ciphertext to myrsa. showvalue (); // The value of P, Q, N, ran, E, D} public void beforemessage () // before the information is generated, calculate the {P = RSA. getprimes (RND); // obtain the prime number p q = RSA. getprimes (RND); // obtain the prime number Q n = RSA. getn (p, q); // obtain p * q ran = RSA. getran (p, q); // get (p-1) * (q-1), ran E = RSA. gete (RAN); // obtain the public key e d = RSA. getkey (E, ran); // The Key D system is generated based on the public key E, and ran. out. println ("Enter the number of digits of the random number (greater than 9):"); random input = new random (system. in); numbit = input. nextint (); If (numbit <9) {system. out. println ("the random number of digits should be greater than 9") ;}public biginteger [] encryption () {string message = ""; system. out. println ("Enter the message to be sent m (please in English):"); inputstream mym = system. in; bufferedreader RD = new bufferedreader (New inputstreamreader (mym); try {message = RD. readline (); system. out. println ("the input message is:" + message);} catch (ioexception e) {system. out. println (E. getmessage ();} Char [] C = message. tochararray (); biginteger [] deal_message = new biginteger [C. length]; for (INT I = 0; I <C. length; I ++) {// system. out. println (C [I]); // It is decomposed into every bit: int A = (INT) C [I]; biginteger = biginteger. valueof (a); deal_message [I] = biginteger; // value assigned successfully} deal_message = RSA. encodersa (deal_message, E, n); // directly modify return deal_message;} public void decryption (biginteger [] encodingm) {encodingm = RSA. dencodersa (encodingm, d, n); // The decoded information. The two-dimensional char [] C = new char [encodingm. length]; for (INT I = 0; I <encodingm. length; I ++) {// system. out. println (encodingm [I]); // It is decomposed into every bit: int d = encodingm [I]. intvalue (); C [I] = (char) D;} string message = string. valueof (c); system. out. println ("decrypted information:" + message);} private void showvalue () {system. out. println (""); system. out. println ("prime number P:" + p); system. out. println ("prime q:" + q); system. out. println ("Calculate p * Q:" + n); system. out. println ("computing (p-1) * (q-1):" + ran); system. out. println (""); system. out. println ("Public Key E:" + E); system. out. println ("Key D:" + d );}}
Import Java. math. biginteger; import Java. util. random; public class RSA {private biginteger primes = biginteger. zero; private biginteger N, ran, D = biginteger. zero; private biginteger E = biginteger. zero; private string m; random RND = new random (); Private int numbit = 10; Public int getnumbit (int n) {numbit = N; return numbit ;} public biginteger getprimes (random RND) {return primes = biginteger. Probableprime (numbit, RND);} public biginteger getn (biginteger P, biginteger q) {return n = P. multiply (Q);} public biginteger getran (biginteger P, biginteger q) {/* long r = (P. intvalue ()-1) * (Q. intvalue ()-1); string TR = string. valueof (r); */ran = (P. subtract (biginteger. one )). multiply (Q. subtract (biginteger. one); Return ran;} public biginteger Gete (biginteger ran) {biginteger temp = NULL; // The intermediate variable E = biginteger. zero; do {temp = biginteger. probableprime (numbit, RND); // randomly generate a prime number to check whether it is equal to 1 in the ran. If it is 1, E = temp, exit the loop if (temp. gcd (RAN )). equals (biginteger. one) {e = temp ;}} while (! (Temp. gcd (RAN )). equals (biginteger. one); Return e;} public biginteger getkey (biginteger E, biginteger ran) {d = E. modinverse (RAN); Return D;} public static biginteger [] encodersa (biginteger [] encodem, biginteger E, biginteger N) {// use encodem directly: for (INT I = 0; I <encodem. length; I ++) {encodem [I] = encodem [I]. modpow (E, n);} return encodem;} public static biginteger [] dencodersa (biginteger [] encodem, biginteger D, biginteger N) {If (encodem = NULL) return NULL; For (INT I = 0; I <encodem. length; I ++) {encodem [I] = encodem [I]. modpow (d, n);} return encodem; // return directly }}
RSA job completed ~~