RSA使用示例代码 java版

2022-01-09  本文已影响0人  乡村码农

废话不多说,先上图


A和B用户生成密钥及加密解

1.1.2.1 先对报文进行签名

对报文中的参数按照key的字典序升序排列,然后按照“key1=val1&key2=val2&...”这样的方式组成字符串s1,对字符串s1使用“SHA256withRSA”进行签名并填写到sign字段中。

1.1.2.2 对报文进行加密

对生成签名后的参数构造成json串,对json串整体进行加密,密文采用“RSA/ECB/PKCS1Padding”,生成最终的参数encryptedData。

  1. 生成RSA私钥,以X509编码,指定生成的密钥的位数为2048位,该步生成的私钥只是临时文件,以便第二步将私钥转为PKCS#8编码。
    openssl genrsa -out rsa_private_key.pem 2048

  2. 将上一步生成的RSA私钥转换成PKCS#8编码,作为最终使用的私钥。
    openssl pkcs8 -topk8 -in rsa_private_key.pem -out pkcs8_rsa_private_key.pem -nocrypt

  3. 导出RSA公钥,以X509编码,作为最终交换的公钥。
    openssl rsa -in rsa_private_key.pem -out rsa_public_key.pem -pubout

按以上步骤生成后,A用户与B用户可进行公钥交换。(对应上面的步骤,即交换rsa_public_key.pem文件)

我们用对方给过来的公钥加密,用自己的私钥解密。因为对方发送来的数据,就是用我们给的公钥进行加密的,我们的公私钥是一对的,所以能解密

废话不多说,上代码

package com.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Map;

/**
 *
*/
public class RsaUtils {
    /**
     * 加密算法RSA
     */
    public static final String KEY_ALGORITHM = "RSA";

    /** */
    /**
     * 签名算法
     */
    public static final String SIGNATURE_ALGORITHM = "SHA256withRSA";

    /**
     * 获取公钥的key
     */
    private static final String PUBLIC_KEY = "RSAPublicKey";

    /**
     * 获取私钥的key
     */
    private static final String PRIVATE_KEY = "RSAPrivateKey";

    /**
     * RSA最大加密明文大小
     */
    private static final int MAX_ENCRYPT_BLOCK = 245 ;

    /**
     * RSA最大解密密文大小
     */
    private static final int MAX_DECRYPT_BLOCK = 256;

    /**
     * RSA 位数 
     */
    private static final int INITIALIZE_LENGTH = 2048;

    public static void main(String[] args) throws  Exception{

        //加密公钥
        String publicKey = "rsa_public_key.pem 公钥内容";
        //解密私钥
        String privateKey = "pkcs8_rsa_private_key.pem中密钥内容";

        try {
            
            System.out.println("私钥:" + privateKey);
            System.out.println("公钥:" + publicKey);
            // RSA加密
            String data = "{\"user_name\":\"张三\",\"password\":\"666666\"}";
            String encryptData = encrypt(data, getPublicKey(publicKey));
            System.out.println("加密后内容:" + encryptData);

            // RSA解密
            String decryptData = decrypt(encryptData, getPrivateKey(privateKey));
            System.out.println("解密后内容:" + decryptData);

            // 用私钥RSA签名
            String sign = sign(data, getPrivateKey(privateKey));
            System.out.println("签名后结果:" + decryptData);
            // 用公钥 RSA验签  可以知道是谁发来的
            boolean result = verify(data, getPublicKey(publicKey), sign);
            System.out.print("验签结果:" + result);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.print("加解密异常");
        }

    }

    /**
     * 生成请求串
     * @param publicKey
     * @param thirdPrivateKey
     * @param pamStr
     * @return
     * @throws Exception
     */
    public static JSONObject enRSAjson(String publicKey,String thirdPrivateKey,String pamStr) throws Exception {
        JSONObject json = new JSONObject();
        String encryptData = encrypt(pamStr, getPublicKey(publicKey));
        // RSA签名
        String sign = sign(encryptData, getPrivateKey(thirdPrivateKey));
        json.put("payload",encryptData);
        json.put("sign",sign);
        return json;
    }
    /**
     * 解密请求串
     * @param tslPublicKey 签名公钥
     * @param privateKey 解密私钥
     * @param pamStr 解密内容
     * @param sign 签名
     * @return
     */
    public static String dnRSA(String tslPublicKey,String privateKey,String pamStr,String sign) throws Exception {
        if(verify(pamStr, getPublicKey(tslPublicKey), sign)){
            String result = decrypt(pamStr, getPrivateKey(privateKey));
            return result;
        }else{
            return null;
        }
    }


    /**
     * 获取密钥对
     *
     * @return 密钥对
     */
    public static KeyPair getKeyPair() throws Exception {
        KeyPairGenerator generator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
        generator.initialize(1024);
        return generator.generateKeyPair();
    }

    /**
     * 获取私钥
     *
     * @param privateKey 私钥字符串
     * @return
     */
    public static PrivateKey getPrivateKey(String privateKey) throws Exception {
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        byte[] decodedKey = Base64.decodeBase64(privateKey.getBytes());
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decodedKey);
        return keyFactory.generatePrivate(keySpec);
    }

    /**
     * 获取公钥
     *
     * @param publicKey 公钥字符串
     * @return
     */
    public static PublicKey getPublicKey(String publicKey) throws Exception {
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        byte[] decodedKey = Base64.decodeBase64(publicKey.getBytes());
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(decodedKey);
        return keyFactory.generatePublic(keySpec);
    }

    /**
     * RSA加密
     *
     * @param data 待加密数据
     * @param publicKey 公钥
     * @return
     */
    public static String encrypt(String data, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        int inputLen = data.getBytes().length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offset = 0;
        byte[] cache;
        int i = 0;
        // 对数据分段加密
        while (inputLen - offset > 0) {
            if (inputLen - offset > MAX_ENCRYPT_BLOCK) {
                cache = cipher.doFinal(data.getBytes(), offset, MAX_ENCRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(data.getBytes(), offset, inputLen - offset);
            }
            out.write(cache, 0, cache.length);
            i++;
            offset = i * MAX_ENCRYPT_BLOCK;
        }
        byte[] encryptedData = out.toByteArray();
        out.close();
        // 获取加密内容使用base64进行编码,并以UTF-8为标准转化成字符串
        // 加密后的字符串
        return new String(Base64.encodeBase64String(encryptedData));
    }

    /**
     * RSA解密
     *
     * @param data 待解密数据
     * @param privateKey 私钥
     * @return
     */
    public static String decrypt(String data, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] dataBytes = Base64.decodeBase64(data);
        int inputLen = dataBytes.length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offset = 0;
        byte[] cache;
        int i = 0;
        // 对数据分段解密
        while (inputLen - offset > 0) {
            if (inputLen - offset > MAX_DECRYPT_BLOCK) {
                cache = cipher.doFinal(dataBytes, offset, MAX_DECRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(dataBytes, offset, inputLen - offset);
            }
            out.write(cache, 0, cache.length);
            i++;
            offset = i * MAX_DECRYPT_BLOCK;
        }
        byte[] decryptedData = out.toByteArray();
        out.close();
        // 解密后的内容
        return new String(decryptedData, "UTF-8");
    }

    /**
     * 签名
     *
     * @param data 待签名数据
     * @param privateKey 私钥
     * @return 签名
     */
    public static String sign(String data, PrivateKey privateKey) throws Exception {
        byte[] keyBytes = privateKey.getEncoded();
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        PrivateKey key = keyFactory.generatePrivate(keySpec);
        Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
        signature.initSign(key);
        signature.update(data.getBytes());
        return new String(Base64.encodeBase64(signature.sign()));
    }

    /**
     * 验签
     *
     * @param srcData 原始字符串
     * @param publicKey 公钥
     * @param sign 签名
     * @return 是否验签通过
     */
    public static boolean verify(String srcData, PublicKey publicKey, String sign) throws Exception {
        byte[] keyBytes = publicKey.getEncoded();
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        PublicKey key = keyFactory.generatePublic(keySpec);
        Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
        signature.initVerify(key);
        signature.update(srcData.getBytes());
        return signature.verify(Base64.decodeBase64(sign.getBytes()));
    }
}

上一篇下一篇

猜你喜欢

热点阅读