java 非对称加密RSA

package com.aarony.test;

import java.io.IOException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class EncryptionRSADemo2 {

    /**
     * 
     * 此方法描述的是:解密密匙
     * 
     * @author: Aarony
     * @version: 2018年6月20日 下午9:44:38
     */
    public static byte[] privateEncrypt(byte[] bytes, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
        return cipher.doFinal(bytes);
    }

    /**
     * 
     * 此方法描述的是:加密
     * 
     * @author: Aarony
     * @version: 2018年6月20日 下午9:44:47
     */
    public static byte[] publicEncrypt(byte[] bytes, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        return cipher.doFinal(bytes);
    }

    /**
     * 
     * 此方法描述的是:解密base64 位的密钥
     * 
     * @author: Aarony
     * @version: 2018年6月20日 下午9:40:51
     */
    public static PrivateKey string2PrivateKey(String privateStr) throws Exception {
        byte[] bytes = base642byte(privateStr);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePrivate(keySpec);
    }

    /**
     * 
     * 此方法描述的是:解密base64 位的公钥
     * 
     * @author: Aarony
     * @version: 2018年6月20日 下午9:40:51
     */
    public static PublicKey string2PublicKey(String pubStr) throws Exception {
        byte[] bytes = base642byte(pubStr);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(bytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePublic(keySpec);
    }

    /**
     * 
     * 此方法描述的是:生成keypair
     * 
     * @author: Aarony
     * @version: 2018年6月20日 下午9:35:43
     * @throws NoSuchAlgorithmException
     */
    public static KeyPair getKeyPair() throws NoSuchAlgorithmException {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(512);
        return keyPairGenerator.generateKeyPair();
    }

    /**
     * 
     * 此方法描述的是:获取公钥
     * 
     * @author: Aarony
     * @version: 2018年6月20日 下午9:37:13
     */
    public static String getPublicKey(KeyPair keyPair) {
        PublicKey key = keyPair.getPublic();
        return byte2base64(key.getEncoded());
    }

    /**
     * 
     * 此方法描述的是:获取公钥
     * 
     * @author: Aarony
     * @version: 2018年6月20日 下午9:37:13
     */
    public static String getPrivateKey(KeyPair keyPair) {
        PrivateKey key = keyPair.getPrivate();
        return byte2base64(key.getEncoded());
    }

    /**
     * 
     * 此方法描述的是:base64 解码
     * 
     * @author: Aarony
     * @version: 2018年6月20日 下午9:16:57
     */
    public static byte[] base642byte(String base64) throws IOException {
        BASE64Decoder decoder = new BASE64Decoder();
        return decoder.decodeBuffer(base64);
    }

    /**
     * 
     * 此方法描述的是: base 64编码
     * 
     * @author: Aarony
     * @version: 2018年6月20日 下午9:15:14
     */
    public static String byte2base64(byte[] bytes) {
        BASE64Encoder base = new BASE64Encoder();
        return base.encode(bytes);
    }
}

猜你喜欢

转载自www.cnblogs.com/wucaifang/p/9206261.html