欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

关于RAS加密解密的一个文章,看着一个就够了!!!

程序员文章站 2024-03-14 14:12:46
...

RAS在网上看了太多文章很多都是不全的,复制过去各种问题,所有就有写个博客的想法,一是方便大家,二是方便自己,好了废话不多说了!

 

首先要注意一下证书级别

1024位的证书,加密时最大支持117个字节,解密时为128;
2048位的证书,加密时最大支持245个字节,解密时为256。

 

创建RAS工具类

package club.loserblog.blogreception.util;

import org.apache.tomcat.util.http.fileupload.ByteArrayOutputStream;
import sun.misc.BASE64Decoder;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.IOException;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

/**
 * @ClassName RsaUtil
 * @Description TODO
 * @Author loser
 * @Date 2020/4/12 18:26
 **/
public class RsaUtil {
    private PublicKey commonKey;

    private PrivateKey secrecyKey;

    /*
    * 1024位的证书,加密时最大支持117个字节,解密时为128;
    * 2048位的证书,加密时最大支持245个字节,解密时为256。
    * */

    /**
     * @Author: dingmingming
     * @Description: 初始化公钥和**
     * @Date: 2020/4/12 18:37
     * @Param:
     * @Return:
     **/
    public RsaUtil() throws GeneralSecurityException{
        KeyPairGenerator pairGenerator = KeyPairGenerator.getInstance("RSA");
        pairGenerator.initialize(1024);
        KeyPair keyPair = pairGenerator.generateKeyPair();
        this.commonKey = keyPair.getPublic();
        this.secrecyKey = keyPair.getPrivate();
    }

    /**
     * @Author: dingmingming
     * @Description: 恢复公钥和**
     * @Date: 2020/4/12 18:37
     * @Param:
     * @Return:
     **/
    public RsaUtil(byte[] commonKey,byte[] secrecyKey)  throws GeneralSecurityException{
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(commonKey);
        this.commonKey = keyFactory.generatePublic(x509EncodedKeySpec);
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(secrecyKey);
        this.secrecyKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
    }

    /**
     * @Author: dingmingming
     * @Description: 将公钥转换为字节
     * @Date: 2020/4/12 18:39
     * @Param:
     * @Return:
     **/
    public byte[] getCommonKey(){
        return commonKey.getEncoded();
    }

    /**
     * @Author: dingmingming
     * @Description: 将私钥转换为字节
     * @Date: 2020/4/12 18:39
     * @Param:
     * @Return:
     **/
    public byte[] getSecrecyKey(){
        return secrecyKey.getEncoded();
    }

    /**
     * @Author: dingmingming
     * @Description: 利用公钥加密
     * @Date: 2020/4/12 18:45
     * @Param:
     * @Return:
     **/
    public String encryptStr(String encryptStr) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, IOException {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(1, this.commonKey);
        byte[] result = encryptStr.getBytes();
        int inputLen = result.length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offSet = 0;

        for(int i = 0; inputLen - offSet > 0; offSet = i * 117) {
            byte[] cache;
            if(inputLen - offSet > 117) {
                cache = cipher.doFinal(result, offSet, 117);
            } else {
                cache = cipher.doFinal(result, offSet, inputLen - offSet);
            }

            out.write(cache, 0, cache.length);
            ++i;
        }

        byte[] encryptedData = out.toByteArray();
        out.close();
        String encryptData = Base64.getEncoder().encodeToString(encryptedData);
        return encryptData;
    }

    /**
     * @Author: dingmingming
     * @Description: 利用私钥解密
     * @Date: 2020/4/12 18:45
     * @Param:
     * @Return:
     **/
    public String decryptStr(String decryptStr) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, IOException {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(2, secrecyKey);
        byte[] result = new BASE64Decoder().decodeBuffer(decryptStr);
        int inputLen = result.length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offSet = 0;

        for(int i = 0; inputLen - offSet > 0; offSet = i * 128) {
            byte[] cache;
            if(inputLen - offSet > 128) {
                cache = cipher.doFinal(result, offSet, 128);
            } else {
                cache = cipher.doFinal(result, offSet, inputLen - offSet);
            }

            out.write(cache, 0, cache.length);

            ++i;
        }

        byte[] decryptedData = out.toByteArray();
        out.close();
        String decryptData = new String(decryptedData,"UTF-8");
        return decryptData;
    }

}

获取前端数据处理

 public ModelAndView getBlogInfoForLook(HttpServletRequest request) throws IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, IOException {
        //blogId 代表的是前端的参数名称 这个大家应该都了解
        String blogId = request.getParameter("blogId").trim();
        //这里是重点要注意的 一定要替换
        //为什么要替换 因为参数传递的过程中 会将加密字符中的加号变成空格,因此我们需要将其替换回来 避免解密失败
        blogId = blogId.replaceAll(" ","+");

        //可以进行揭秘操作了
}

前端这一块我只是重点说一下解密 ,因为解密猜的坑有点多,加密都没有什么问题调用方法就行

好了就这样了!!!