Java Asymmetric Encryption RSA

Source: Internet
Author: User
Tags base64 asymmetric encryption

 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.