天天看點

關于RAS加密解密的一個文章,看着一個就夠了!!!首先要注意一下證書級别建立RAS工具類擷取前端資料處理

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(" ","+");

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

前端這一塊我隻是重點說一下解密 ,因為解密猜的坑有點多,加密都沒有什麼問題調用方法就行

好了就這樣了!!!