Packagecom.aarony.test;Importjava.io.IOException;Importjava.security.KeyFactory;ImportJava.security.KeyPair;ImportJava.security.KeyPairGenerator;Importjava.security.NoSuchAlgorithmException;ImportJava.security.PrivateKey;ImportJava.security.PublicKey;ImportJava.security.spec.PKCS8EncodedKeySpec;ImportJava.security.spec.X509EncodedKeySpec;ImportJavax.crypto.Cipher;ImportSun.misc.BASE64Decoder;ImportSun.misc.BASE64Encoder; Public classEncryptionRSADemo2 {/*** * This method describes: decryption key * *@author: Aarony *@version: June 20, 2018 PM 9:44:38*/ Public Static byte[] Privateencrypt (byte[] bytes, Privatekey privatekey)throwsException {Cipher Cipher= Cipher.getinstance ("RSA"); Cipher.init (Cipher.encrypt_mode, Privatekey); returncipher.dofinal (bytes); } /*** * This method describes: Encryption * *@author: Aarony *@version: June 20, 2018 PM 9:44:47*/ Public Static byte[] Publicencrypt (byte[] bytes, PublicKey publickey)throwsException {Cipher Cipher= Cipher.getinstance ("RSA"); Cipher.init (Cipher.encrypt_mode, PublicKey); returncipher.dofinal (bytes); } /*** * This method describes: Decrypt the key of the base64 bit * *@author: Aarony *@version: June 20, 2018 PM 9:40:51*/ Public StaticPrivatekey String2privatekey (String privatestr)throwsException {byte[] bytes =Base642byte (PRIVATESTR); Pkcs8encodedkeyspec KeySpec=Newpkcs8encodedkeyspec (bytes); Keyfactory keyfactory= Keyfactory.getinstance ("RSA"); returnkeyfactory.generateprivate (KEYSPEC); } /*** * This method describes: decrypts the public key of the base64 bit * *@author: Aarony *@version: June 20, 2018 PM 9:40:51*/ Public StaticPublicKey String2publickey (String pubstr)throwsException {byte[] bytes =Base642byte (PUBSTR); X509encodedkeyspec KeySpec=Newx509encodedkeyspec (bytes); Keyfactory keyfactory= Keyfactory.getinstance ("RSA"); returnkeyfactory.generatepublic (KEYSPEC); } /*** * This method describes: Generate KeyPair * *@author: Aarony *@version: June 20, 2018 PM 9:35:43 *@throwsnosuchalgorithmexception*/ Public StaticKeyPair Getkeypair ()throwsnosuchalgorithmexception {keypairgenerator keypairgenerator= Keypairgenerator.getinstance ("RSA"); Keypairgenerator.initialize (512); returnKeypairgenerator.generatekeypair (); } /*** * This method describes: get the public key * *@author: Aarony *@version: June 20, 2018 PM 9:37:13*/ Public StaticString Getpublickey (KeyPair KeyPair) {publickey key=keypair.getpublic (); returnbyte2base64 (key.getencoded ()); } /*** * This method describes: get the public key * *@author: Aarony *@version: June 20, 2018 PM 9:37:13*/ Public StaticString Getprivatekey (KeyPair KeyPair) {Privatekey key=keypair.getprivate (); returnbyte2base64 (key.getencoded ()); } /*** * This method describes: Base64 decoding * *@author: Aarony *@version: June 20, 2018 PM 9:16:57*/ Public Static byte[] Base642byte (String base64)throwsIOException {Base64decoder decoder=NewBase64decoder (); returnDecoder.decodebuffer (base64); } /*** * This method describes: Base 64 encoding * *@author: Aarony *@version: June 20, 2018 PM 9:15:14*/ Public StaticString Byte2base64 (byte[] bytes) {Base64encoder base=NewBase64encoder (); returnBase.encode (bytes); }}
Java Asymmetric Encryption RSA