RSA encryption Algorithm Java Simple implementation method (must SEE) _java

Source: Internet
Author: User
Tags decrypt

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.