Man of few words said, directly open line and
public class RSAUtils { Private static String PUB_KEY = "xxxxxx"; Private static String PRIV_KEY = "xxxxxx"; public static the Map <Integer, String> genKeyPair () { the Map <Integer, String> KEYMAP = new new the HashMap <Integer, String> ( ); // for enclosing the randomly generated public and private keys the try { // the KeyPairGenerator classes for generating public and private keys based on the RSA algorithm generating object the KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance ( "RSA"); // initialization key pair generator, a key size of 96-1024 bit keyPairGen.initialize (1024, new new a SecureRandom ()); // generate a key pair, stored in the KeyPair KeyPair KeyPair keyPairGen.generateKeyPair = (); RSAPrivateKey the privateKey = (RSAPrivateKey) keyPair.getPrivate (); // get private RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic (); // get the public key // get the public key string String publicKeyString = new new String (Base64.encodeBase64 (publicKey.getEncoded ())); // get private String String privateKeyString = new String (Base64.encodeBase64 ( (privateKey.getEncoded ())) ); // save the public and private key to the Map keyMap.put (0, publicKeyString); // 0 represents a public key keyMap.put (1, privateKeyString); // 1 represents private } catch (Exception e) { null return; } return KEYMAP; } / ** * the RSA public key encryption * * @param STR * required encrypted string * @param publicKey * public key contents of public key encryption * @return * / public static string the encrypt ( String str) { = Null outStr String; the try { // Base64-encoded public byte [] = Decoded Base64.decodeBase64 (PUB_KEY); RSAPublicKey pubkey = (RSAPublicKey) KeyFactory.getInstance ( "the RSA") .generatePublic (new new X509EncodedKeySpec (Decoded)); // RSA encryption the cipher cipher = Cipher.getInstance ( "the RSA"); cipher.init (Cipher.ENCRYPT_MODE, pubkey); outStr = Base64.encodeBase64String (Cipher.doFinal (str.getBytes ( "UTF-. 8"))); the catch} (Exception E) { } return outStr; } / ** * the RSA private key to decrypt * * @param STR * encrypted string * @param privateKey * private content * @return the private key to decrypt * / public static string the decrypt (string STR) { String outStr = null; the try { // 64-digit string after decoding the encrypted byte [] = inputByte Base64.decodeBase64 (str.getBytes ( "UTF-. 8")); // Base64 encoding the private key byte [] = Decoded Base64.decodeBase64 (PRIV_KEY); RSAPrivateKey the prikey = (RSAPrivateKey the) KeyFactory.getInstance ( "the RSA") .generatePrivate (new new PKCS8EncodedKeySpec with (Decoded)); // the RSA decryption cipher cipher = Cipher.getInstance ( "the RSA"); cipher.init (Cipher.DECRYPT_MODE, prikey); outStr = new new String (Cipher.doFinal (inputByte)); } the catch (Exception E) { } return outStr; } }
This class has a method for generating public and private keys, as well as a method of public key encryption and private key decryption. We used to encrypt the public key on the front end, a rear end on the private key to decrypt.
The front need to introduce jsencrypt.min.js
Download: http://travistidwell.com/jsencrypt/
Encryption method:
We can achieve the tip decrypt the encrypted back-end