Import java.io.fileinputstream;import java.io.fileoutputstream;import java.io.objectinputstream ; import java.io.objectoutputstream;import java.security.key;import java.security.keypair;import java.security.KeyPairGenerator;import java.security.SecureRandom;import javax.crypto.Cipher; import sun.misc.base64decoder;import sun.misc.base64encoder;public class rsa_encrypt {/** specifies that the encryption algorithm is desede */private static string algorithm = "RSA";/** Specify the size of the key */private static int KEYSIZE = 1024;/** specify the public key to store the file */private static String PUBLIC_KEY_FILE = "publickey";/** Specify the private key to store files */private static string private_key_file = "privatekey";/** * generate key pair */private The Static void generatekeypair () throws exception {/** rsa algorithm requires a trustworthy random number source */ securerandom sr = new securerandom ();/** creates a Keypairgenerator object for the RSA algorithm */keypairgenerator kpg = keypairgenerator.getinstance (algorithm);/** uses the above random data source to initialize the Keypairgenerator object */kpg.initialize (keysize, sr);/** generate key pair */keypair kp = kpg.generatekeypair ();/** get public key */key publickey = kp.getpublic ();/** get the private key */Key privateKey = Kp.getprivate ();/** writes the generated key to the file with the object stream */ObjectOutputStream oos1 = new ObjectOutputStream (new fileoutputstream (public_key_file)); objectoutputstream oos2 = new objectoutputstream (new fileoutputstream (private_key_file)); oos1.writeobject (publicKey); o Os2.writeobject (privatekey);/** empties the cache, closes the file output stream */oos1.close (); oos2.close ();} /** * Encryption Method source: source data */public static string encrypt (string Source) throws exception {generatekeypair ();/** to read the public key object in the fileOut */objectinputstream ois = new objectinputstream (new fileinputstream (PUBLIC_KEY _file)); key key = (key) ois.readobject (); ois.close ();/** get cipher object to implement RSA encryption of source data */ Cipher cipher = cipher.getinstance (algorithm); cipher.init (cipher.encrypt_mode, key); byte[] b = source.getbytes ();/** performs cryptographic operations */byte[] b1 = cipher.dofinal (b); Base64encoder encoder = new base64encoder (); Return encoder.encode (b1);} /** * decryption algorithm cryptograph: ciphertext */public static string decrypt (String Cryptograph) throws Exception {/** read out the private key object in the file */objectinputstream ois = new objectinputstream (new fileinputstream (private_key_file)); key key = (key) ois.readobject ();/** Gets the Cipher object to decrypt the data encrypted with the public key RSA */Cipher Cipher = cipher.getinstance (algorithm); Cipher.init (cipher.decrypt_mode, key); Base64decoder decoder = new base64decoder ();byte[] b1 = Decoder.decodebuffer (cryptograph);/** Perform decryption operation */byte[] b = cipher.dofinal (b1); return new string (b);} Public static void main (string[] args) throws exception {string source = "hello world!"; / to encrypt string String cryptograph = encrypt (source);// generated ciphertext System.out.println (cryptograph) ; System.out.println ("====================="); String target = decrypt (cryptograph);// decryption System.out.println (target);}}
This article is from the "simple life" blog, so be sure to keep this source http://simplelife.blog.51cto.com/9954761/1872830
Simple implementation of RSA encryption and decryption algorithm in Java