Simple and complete code, through which you will have a preliminary understanding of the RSA encryption algorithm in Java implementation method, this class, you can directly use, the level of high, on their own modified to improve the code.
Package security;
Import java.security.*;
Import java.security.spec.*;
Import java.security.interfaces.*;
Import javax.crypto.spec.*;
Import javax.crypto.interfaces.*;
Import java.io.*;
Import java.math.*; public class Rsademo {public Rsademo () {} public static void GenerateKey () {try {keypairgenerator KPG = Keypai
Rgenerator.getinstance ("RSA");
Kpg.initialize (1024);
KeyPair KP = Kpg.genkeypair ();
PublicKey Pbkey = Kp.getpublic ();
Privatekey Prkey = Kp.getprivate ();
Save Public key FileOutputStream F1 = new FileOutputStream ("Pubkey.dat");
ObjectOutputStream B1 = new ObjectOutputStream (F1);
B1.writeobject (Pbkey);
Save the private key FileOutputStream F2 = new FileOutputStream ("Privatekey.dat");
ObjectOutputStream b2 = new ObjectOutputStream (F2);
B2.writeobject (Prkey);
The catch (Exception e) {}} is public static void Encrypt () throws Exception {String s = "Hello world!";
Gets the public key and the parameter e,n fileinputstream f = new FileInputStream ("Pubkey.dat"); ObjectinpUtstream B = new ObjectInputStream (f);
Rsapublickey pbk = (rsapublickey) b.readobject ();
BigInteger e = Pbk.getpublicexponent ();
BigInteger n = pbk.getmodulus ();
System.out.println ("e=" + e);
System.out.println ("n=" + N);
Get plaintext m byte ptext[] = S.getbytes ("UTF-8");
BigInteger m = new BigInteger (ptext);
Compute ciphertext C BigInteger C = M.modpow (e, N);
System.out.println ("c=" + C);
Save ciphertext String cs = c.tostring ();
BufferedWriter out = new BufferedWriter (new OutputStreamWriter) (New FileOutputStream ("Encrypt.dat"));
Out.write (CS, 0, Cs.length ());
Out.close (); public static void Decrypt () throws Exception {//Read ciphertext BufferedReader in = new BufferedReader (New Inputstr
Eamreader (New FileInputStream ("Encrypt.dat"));
String Ctext = In.readline ();
BigInteger C = new BigInteger (ctext);
Read the private key fileinputstream f = new FileInputStream ("Privatekey.dat");
ObjectInputStream B = new ObjectInputStream (f); Rsaprivatekey PRK = (rsaprivatekey)B.readobject ();
BigInteger d = prk.getprivateexponent ();
Gets the private key parameter and decrypts BigInteger n = prk.getmodulus ();
System.out.println ("d=" + D);
System.out.println ("n=" + N);
BigInteger m = C.modpow (d, N);
Displays the decryption result System.out.println ("m=" + M);
byte[] Mt = M.tobytearray ();
System.out.println ("plaintext is");
for (int i = 0; i < mt.length i++) {System.out.print ((char) mt[i]);
} public static void Main (String args[]) {try {generatekey ();
Encrypt ();
Decrypt ();
catch (Exception e) {System.out.println (e.tostring ()); }
}
}
Above this RSA encryption algorithm Java Simple implementation method (must see) is a small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.