package com.rsa; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.HashMap; import java.util.Map; import javax.crypto.Cipher; import org.apache.commons.codec.binary.Base64; public class Test { private static Map<Integer, String> keyMap = new HashMap<Integer, String>(); static void main public (String [] args) throws Exception { // the TODO Auto-Generated Method Stub // packaged for public and private key randomly generated {// generates a public and private key genKeyPair (); // encryption string string Message = "df723820"; System.out.println ( "randomly generated public key:" + keyMap.get (0)); System.out.println ( "randomly generated private key:" + keyMap .get (. 1)); string = messageEn the encrypt (Message, keyMap.get (0)); System.out.println (Message + "\ t of the encrypted string is:" + messageEn); string = messageDe the decrypt ( messageEn, keyMap.get (. 1)); System.out.println ( "reduced to the string:" + messageDe); } } Public static void genKeyPair () {// throws NoSuchAlgorithmException the KeyPairGenerator classes for generating public and private keys based on the RSA algorithm generating object KeyPairGen = KeyPairGenerator.getInstance the KeyPairGenerator ( "the RSA"); // Initializes the key pair generator, a key size of 96-1024 bit keyPairGen.initialize (1024, new new a SecureRandom ()); // generates a key pair, stored in the KeyPair KeyPair KeyPair keyPairGen.generateKeyPair = (); RSAPrivateKey The privateKey = (RSAPrivateKey The ) keyPair.getPrivate (); // get private RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic (); // get public String = publicKeyString new new String (Base64.encodeBase64 (publicKey.getEncoded ())); // get private string string = privateKeyString new new string (Base64.encodeBase64 ((privateKey.getEncoded ()))); // save the public and private key to keyMap.put (0, publicKeyString); // 0 represents a public key keyMap .put (. 1, privateKeyString); // represents a private key. 1 } public static String encrypt(String str, String publicKey) throws Exception { // base64编码的公钥 byte[] decoded = Base64.decodeBase64(publicKey); RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA") .generatePublic(new X509EncodedKeySpec(decoded)); // RSA加密 Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, pubKey); String outStr = Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8"))); return outStr; } public static String decrypt(String str, String privateKey) throws Exception { // 64位解码加密后的字符串 byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8")); // base64-encoded private key byte[] decoded = Base64.decodeBase64(privateKey); RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA") .generatePrivate(new PKCS8EncodedKeySpec(decoded)); // RSA解密 Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, priKey); String outStr = new String(cipher.doFinal(inputByte)); return outStr; } }