RSA Tool class. Provides encryption, decryption, and generation of key equivalence methods.
RSA Encryption Principle Overview:
The security of RSA relies on the decomposition of large numbers, and the public and private keys are functions of two large primes (decimal bits greater than 100).
It is assumed that the difficulty of inferring clear text from a key and cipher is equivalent to decomposing the product of two large primes
The creation of the key:
1. Select two large prime p,q, calculate n=p*q;
2. Randomly select the encryption key E, which requires E and (p-1) * (q-1) coprime
3. Use the EUCLID algorithm to compute the decryption key D to meet e*d = 1 (mod (p-1) * (q-1)) (where N,d also coprime)
4: This concludes that the public key is (N,E) the private key is (N,D)
RSA speed
Due to the large number of calculations, RSA is the fastest case is 100 times times slower than DES, whether it is software or hardware implementation.
Speed has been a drawback of RSA. Generally, only a small amount of data encryption is used.
Package Com.security.rsa;import Java.security.keyfactory;import Java.security.keypair;import Java.security.keypairgenerator;import Java.security.privatekey;import Java.security.publickey;import Java.security.interfaces.rsaprivatekey;import Java.security.interfaces.rsapublickey;import Java.security.spec.pkcs8encodedkeyspec;import Java.security.spec.x509encodedkeyspec;import Javax.crypto.Cipher; public class Rsasecurity {public static string src= "RSA cryptographic string"; public void Prienpubde () {try {//1. Initialize secret key Keypairgenerator keypairgenerator = Keypairgenerator.getinstance ("R SA "); Secret key length Keypairgenerator.initialize (512); Initialize secret key pair KeyPair KeyPair = Keypairgenerator.generatekeypair (); Public key Rsapublickey Rsapublickey = (rsapublickey) keypair.getpublic (); Private key Rsaprivatekey Rsaprivatekey = (rsaprivatekey) keypair.getprivate (); 2. Private key encryption, public key decryption----encryption//Generate private key pkcs8encodedkeyspec Pkcs8encodedkeyspec = new Pkcs8encodedkeyspec (Rsaprivatekey.getenco DeD ()); Keyfactory keyfactory =keyfactory.getinstance ("RSA"); Privatekey Privatekey = keyfactory.generateprivate (Pkcs8encodedkeyspec); The Cipher class provides cryptographic functionality for encryption and decryption by getinstance instantiating objects Cipher Cipher =cipher.getinstance ("RSA"); Initialize cryptographic cipher.init (Cipher.encrypt_mode,privatekey); Byte[] result = Cipher.dofinal (Src.getbytes ()); SYSTEM.OUT.PRINTLN ("Private key encryption, public key decryption----encryption:" +base64.encode (result)); 3. Private key encryption, public key decryption----decryption//Generate public key x509encodedkeyspec X509encodedkeyspec = new X509encodedkeyspec (rsapublickey.getencoded ( )); Keyfactory=keyfactory.getinstance ("RSA"); PublicKey publickey= keyfactory.generatepublic (X509ENCODEDKEYSPEC); cipher = Cipher.getinstance ("RSA"); Initial dissolve dense cipher.init (cipher.decrypt_mode,publickey); result = cipher.dofinal (result); SYSTEM.OUT.PRINTLN ("Private key encryption, public key decryption----decryption:" +new String (Result)); } catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();} } public void PuBenpride () {try {//1. Initialize key Keypairgenerator Keypairgenerator = Keypairgenerator.getinstance ("RSA"); Secret key length Keypairgenerator.initialize (512); Initialize secret key pair KeyPair KeyPair = Keypairgenerator.generatekeypair (); Public key Rsapublickey Rsapublickey = (rsapublickey) keypair.getpublic (); Private key Rsaprivatekey Rsaprivatekey = (rsaprivatekey) keypair.getprivate (); 2. Public key encryption, private key decryption----encryption X509encodedkeyspec x509encodedkeyspec=new X509encodedkeyspec (rsapublickey.getencoded ()); Keyfactory keyfactory = keyfactory.getinstance ("RSA"); PublicKey PublicKey = Keyfactory.generatepublic (X509encodedkeyspec); Initializes the cryptographic//cipher class to provide cryptographic functionality for encryption and decryption by getinstance instantiating objects Cipher Cipher =cipher.getinstance ("RSA"); Cipher.init (Cipher.encrypt_mode,publickey); Encrypted string byte[] result = Cipher.dofinal (Src.getbytes ()); SYSTEM.OUT.PRINTLN ("Public key cryptography, private key decryption----encryption:" +base64.encode (result)); 3. Public key encryption, private key decryption-----decryption Pkcs8encodedkeyspec Pkcs8encodedkeyspec = new Pkcs8encodedkeyspec (rsaprivatekey.getencoded ()); Keyfactory=keyfactory.getinstance ("RSA"); Privatekey Privatekey = keyfactory.generateprivate (Pkcs8encodedkeyspec); Initial dissolution of the dense cipher.init (Cipher.decrypt_mode, Privatekey); Decrypt string result = cipher.dofinal (result); SYSTEM.OUT.PRINTLN ("Public key cryptography, private key decryption-----decryption:" +new String (Result)); } catch (Exception e) {//TODO auto-generated catch block E.printstacktrace (); } }}
Test:
Package Com.secutiry.rsa;import Com.security.rsa.rsasecurity;public class Rsatest {public static void main (string[] args) { rsasecurity rsasecurity = new rsasecurity (); SYSTEM.OUT.PRINTLN ("Private key encryption Public key decryption Example:"); Rsasecurity.prienpubde (); SYSTEM.OUT.PRINTLN ("Public key encryption private key decryption example:"); Rsasecurity.pubenpride (); }}
Test results:
Source
http://download.csdn.net/download/u013400939/8517989 Click to open link
Java encryption and decryption for RSA