See the detailed procedure code comments
Import java.security.KeyFactory; Import java.security.KeyPair; Import java.security.KeyPairGenerator; Import java.security.PrivateKey; Import java.security.PublicKey; Import java.security.spec.PKCS8EncodedKeySpec; Import in java.security.spec .X509EncodedKeySpec; Import javax.crypto.Cipher; Import org.apache.commons.codec.binary.Base64; public class ResUtils { public static void main (String [] args) throws Exception { / * * asymmetrical encryption Description: * * general the public key is the private key is private * encrypt data using the public key can only be decrypted with the private key data * data encrypted using a private key can only be decrypted with the public data * * / the KeyPairGenerator keygen = the KeyPairGenerator. the getInstance ( "RSA"); // the KeyPairGenerator asymmetric encryption target for generating public and private keys, RSA asymmetric encryption algorithm selection keyGen.initialize (2048); // key initializes 2048 KeyPair keyPair = keyGen. the generateKeyPair (); // generate a key pair PublicKey keyPair.getPublic publicKey = (); // get the public key of the object the privateKey privateKey keyPair.getPrivate = (); //Obtain a private key object Pubkey Base64.encodeBase64String = String (publicKey.getEncoded ()); // get the public key encoded string String prikey = Base64.encodeBase64String (privateKey.getEncoded ()); // encoded secret key string System.out. the println (pubkey); System.out.println (prikey); // The reduction of the public key to the public key string objects byte [] = pubKeyb Base64.decodeBase64 (pubkey); // -encoded key strings X509EncodedKeySpec x509KeySpec = new new X509EncodedKeySpec (pubKeyb); // create a public key material in accordance with the public key encoded string? KeyFactory = KeyFactory.getInstance the KeyFactory ( "RSA"); // key using the RSA algorithm plant PubKeyr = keyFactory.generatePublic PublicKey (x509KeySpec); // key material to generate a public key using a public key factory objects // restore the private key object private String byte [] = priKeyb Base64.decodeBase64 (prikey); // encoding private string PKCS8EncodedKeySpec pkcs8KeySpec = new new PKCS8EncodedKeySpec (priKeyb); // create a private key material in accordance with the private key encoded string? PriKeyr = keyFactory.generatePrivate the PrivateKey (pkcs8KeySpec); // key plant material using the private key to generate a private key object // encrypted data (objects using the public key encryption) String Data = "data Data"; // data to be encrypted Cipher.getInstance cipher = cipher ( "RSA"); // cipher for encrypting or decrypting data objects, RSA algorithm chosen to encrypt or decrypt cipher.init (Cipher.ENCRYPT_MODE, pubKeyr); // cipher encryption mode ENCRYPT_MODE selected objects, and using public key encryption target byte [] = encData Cipher.doFinal (data.getBytes ( "the UTF8")); // the Cipher objects generate encrypted data string = encdatas Base64.encodeBase64String (encData); // encoded encrypted data string obtained System.out.println (encdatas); // decrypt data (using the private key to decrypt the object) cipher.init (Cipher.PRIVATE_KEY, priKeyr); // the Cipher PRIVATE_KEY decryption mode selected objects, and the object using the private key to decrypt byte[] = Cipher.doFinal DecData (Base64.decodeBase64 (encdatas)); // decode the encrypted data and decrypted data String = decdatas new new String (DecData, "the UTF8"); // get the data decrypted string System.out. the println (decdatas); } }
(Finish)