RAS加密算法介绍:
RSA(全称:Rivest-Shamir-Adleman)是一种非对称加密算法,在加密/解密过程中使用不同的密钥,即公钥和私钥。公钥和私钥是成对生成的,一般公钥会存放在客户端,客户端可以使用公钥对内容进行加密,服务器端存放私钥,当服务器端收到加密的内容后,可以通过私钥进行解密。。公钥可以公开,但为了安全私钥需要保密。
RSA加签是什么?加签是私钥放在客户端,使用私有对数据进行加签,服务器拿到数据后用公钥进行验签。
在线生成RAS 公钥和私钥地址:https://www.qianbo.com.cn/Tool/RSA/,也可以通过代码生成公钥和私有。
Jmeter中通过beanshell加密/解密RAS内容,如下:
完整代码:
import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.io.ByteArrayOutputStream; import javax.crypto.Cipher; import java.security.*; import java.util.*; import java.util.Map; import java.util.HashMap; 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_STR = "PublicKeyStr"; //获取私钥的key private static final String PRIVATE_KEY_STR = "PrivateKeyStr"; //RSA最大加密明文大小(字节数) private static final int MAX_ENCRYPT_BLOCK = 117; //RSA最大解密密文大小(字节数) private static final int MAX_DECRYPT_BLOCK = 128; /* * @description: 生成公钥私钥 * @date: 2024/1/17 14:47 * @param * @return java.util.Map<java.lang.String,java.lang.String> */ public static Map initKeyPair() throws Exception { //public static Map<String, String> initKeyPair() throws Exception { KeyPairGenerator kpg = KeyPairGenerator.getInstance(KEY_ALGORITHM); kpg.initialize(1024); KeyPair keyPair = kpg.generateKeyPair(); PublicKey publicKey = keyPair.getPublic(); PrivateKey privateKey = keyPair.getPrivate(); String publicKeyStr = encryptBASE64(publicKey.getEncoded()); String privateKeyStr = encryptBASE64(privateKey.getEncoded()); Map keyMap = new HashMap(); keyMap.put(PUBLIC_KEY_STR, publicKeyStr); keyMap.put(PRIVATE_KEY_STR, privateKeyStr); return keyMap; } /* * @description: 公钥加密 * @date: 2024/1/17 14:49 * @param str 待加密字符串 * @param key 公钥 * @return java.lang.String */ public static String encryptByPublicKey(String str, String key) throws Exception { byte[] data = str.getBytes(); PublicKey publicKey = strToPublicKey(key); Cipher cipher = Cipher.getInstance(KEY_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] bytes = dataSegment(data, cipher, MAX_ENCRYPT_BLOCK); return encryptBASE64(bytes); } /* * @description: 私钥解密 * @date: 2024/1/17 14:52 * @param str 待解密字符串 * @param key 私钥 * @return java.lang.String */ public static String decryptByPrivateKey(String str, String key) throws Exception { byte[] data = decryptBASE64(str); PrivateKey privateKey = strToPrivateKey(key); Cipher cipher = Cipher.getInstance(KEY_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] bytes = dataSegment(data, cipher, MAX_DECRYPT_BLOCK); return new String(bytes); } /* * @description: 私钥加密 * @date: 2024/1/17 14:58 * @param str 待加密字符串 * @param key 私钥 * @return java.lang.String */ public static String encryptByPrivateKey(String str, String key) throws Exception { byte[] data = str.getBytes(); PrivateKey privateKey = strToPrivateKey(key); Cipher cipher = Cipher.getInstance(KEY_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, privateKey); byte[] bytes = dataSegment(data, cipher, MAX_ENCRYPT_BLOCK); return encryptBASE64(bytes); } /* * @description: 公钥解密 * @param str 待解密字符串 * @param key 公钥 * @return java.lang.String */ public static String decryptByPublicKey(String str, String key) throws Exception { byte[] data = decryptBASE64(str); PublicKey publicKey = strToPublicKey(key); Cipher cipher = Cipher.getInstance(KEY_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, publicKey); byte[] bytes = dataSegment(data, cipher, MAX_DECRYPT_BLOCK); return new String(bytes); } /* * @description: 使用私钥对数据进行数字签名 * @param str 待加签字符串 * @param privateKey 私钥 * @return java.lang.String */ public static String sign(String str, String privateKey) throws Exception { byte[] data = str.getBytes(); PrivateKey priKey = strToPrivateKey(privateKey); Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); signature.initSign(priKey); signature.update(data); byte[] bytes = signature.sign(); return encryptBASE64(bytes); } /* * @description: 使用公钥对数据验证签名 * @param str 待验签字符串(即要加签的内容) * @param publicKey 公钥 * @param sign 数据签名 * @return boolean */ public static boolean verify(String str, String publicKey, String sign) throws Exception { byte[] data = str.getBytes(); byte[] signBytes = decryptBASE64(sign); PublicKey pubKey = strToPublicKey(publicKey); Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); signature.initVerify(pubKey); signature.update(data); return signature.verify(signBytes); } /* * @description: 对数据进行分段加密或解密 * @date: 2024/1/17 16:11 * @param data * @param cipher * @param maxBlock * @return byte[] */ private static byte[] dataSegment(byte[] data, Cipher cipher, int maxBlock) throws Exception{ byte[] toByteArray; int inputLen = data.length; ByteArrayOutputStream out = new ByteArrayOutputStream(); int offSet = 0; int i = 0; byte[] cache; // 对数据分段加密解密 while (inputLen - offSet > 0) { int var = inputLen - offSet > maxBlock ? maxBlock : inputLen - offSet; cache = cipher.doFinal(data, offSet, var); out.write(cache, 0, cache.length); i++; offSet = i * maxBlock; } toByteArray = out.toByteArray(); out.close(); return toByteArray; } /* * @description: 获取私钥 * @date: 2024/1/17 14:57 * @param str 私钥字符串 * @return java.security.PrivateKey */ private static PrivateKey strToPrivateKey(String str) throws Exception { PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(decryptBASE64(str)); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec); return privateKey; } /* * @description: 获取公钥 * @date: 2024/1/17 14:57 * @param str 公钥字符串 * @return java.security.PrivateKey */ private static PublicKey strToPublicKey(String str) throws Exception { X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(decryptBASE64(str)); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); PublicKey publicKey = keyFactory.generatePublic(x509KeySpec); return publicKey; } /* * @description: 字节数组转字符串 * @date: 2024/1/17 14:59 * @param bytes 字节数组 * @return java.lang.String */ private static String encryptBASE64(byte[] bytes) { return Base64.getEncoder().encodeToString(bytes); } /* * @description: 字符串转字节数组 * @date: 2024/1/17 15:00 * @param str 字符串 * @return byte[] */ private static byte[] decryptBASE64(String str) { return Base64.getDecoder().decode(str); } //// public static void main(String[] args) throws Exception { //// // 获取公钥私钥 //// Map<String, String> keyMap = initKeyPair(); //// String publicKey = keyMap.get(PUBLIC_KEY_STR); //// String privateKey = keyMap.get(PRIVATE_KEY_STR); //// System.out.println("公钥: " + publicKey); //// System.out.println("私钥: " + privateKey); //// //// String data = "123456"; //// System.out.println("源数据: " + data); //// //// // 公钥加密私钥解密 //// String encryptData = encryptByPublicKey(data, publicKey); //// String decryptData = decryptByPrivateKey(encryptData, privateKey); //// System.out.println("公钥加密结果: " + encryptData); //// System.out.println("私钥解密结果: " + decryptData); //// System.out.println(); //// //// // 私钥加密公钥解密 //// String encryptData2 = encryptByPrivateKey(data, privateKey); //// String decryptData2 = decryptByPublicKey(encryptData2, publicKey); //// System.out.println("私钥加密结果: " + encryptData2); //// System.out.println("公钥解密结果: " + decryptData2); //// System.out.println(); //// //// // 私钥加签公钥验签 //// String data2 = "abc123369"; //// String sign = sign(data2, privateKey); //// boolean flag = verify(data2, publicKey, sign); //// System.out.println("私钥加签结果: " + sign); //// System.out.println("公钥验签结果: " + flag); //// } // } RSAUtils ras_obj = new RSAUtils(); String privateKey = "MIICXgIBAAKBgQCTRWS1vzgTStrvOXwO5cffd/wHlTuZYus83PynyAsEqAM0Y9VE" +"f5ucJTzr0OYrs5AiZNCBMn3jiuuUhMpsz9gfxpAoA6tctdQ38LJsKefyxF5vXioP" +"ifBE7RNEJcZBUH6L6rCOxoYGbEdLtr1GTRm0pzFMy5uffwiSfw1ZotrXuwIDAQAB" +"AoGBAIdvc9Soy/sCBiGmbe6NZqiegUq/9RV0oP1SBZCPKYLYuEm/3eumpwaMQGBl" +"tqdN9SmMKSwHsTpHEat+C2LSKeBvkKZIATcJh9MpIvKfifU3TNwtahGa2y0XA1Ns" +"/Q//EUmmk557dv13DBrplocr4xrk6Bk8yHVdfoTr+8rO3PKBAkEA9uPNHsuSlT7X" +"Zb4e++UYqxixyhBgSOlp15yLIsdvm001De4mfyQr7i6H6KTuJ6JRHz36Lsf/kUPt" +"QN4yoFjlGwJBAJi0j93ihUNGGzol/Yk6pzDSDnE35+U73ZBIwydNKCdGy34CEtsf" +"c6i+dknc12SMwlhJ6abegZ5SiZbWCQ2jIeECQQCStYuOYSoZxd35d6CxztF52pLL" +"oC+XJBzhIwULBbgscRS10m7N3pC8WKoqdNeQdcP+7LVeVzpm+BZkFaW/4xNBAkBr" +"NWFH2y/KMl8NLPzaKmaU3PSswh45jxUXpi302rOVF5voJJaOZn9x+f3cD4LdIgGT" +"sJiswcxolfPpBO2oOF6hAkEAp3fRxYGCUi5ifClLvVL5KdW4DV44Slqz+VOzu58S" +"5DZ0K+8FQQXXvdszeRSYtSNe4eT2x1JfUe+dzfqH0w/oJg=="; String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCTRWS1vzgTStrvOXwO5cffd/wH"+ "lTuZYus83PynyAsEqAM0Y9VEf5ucJTzr0OYrs5AiZNCBMn3jiuuUhMpsz9gfxpAo" +"A6tctdQ38LJsKefyxF5vXioPifBE7RNEJcZBUH6L6rCOxoYGbEdLtr1GTRm0pzFM" +"y5uffwiSfw1ZotrXuwIDAQAB"; String text = "test"; Map keyMap = ras_obj.initKeyPair(); String publicKey = keyMap.get("PublicKeyStr"); String privateKey = keyMap.get("PrivateKeyStr"); log.info("公钥:"+publicKey); log.info("私钥:"+privateKey); log.info("源数据:"+text); String encryptData = ras_obj.encryptByPublicKey(text, publicKey); String decryptData = ras_obj.decryptByPrivateKey(encryptData, privateKey); log.info("公钥加密:"+encryptData); log.info("私钥解密:"+decryptData); vars.put("request_value",encryptData); vars.put("response_value",decryptData);
运行结果:
java代码参考于:https://www.cnblogs.com/guliang/p/17975428
标签:公钥,私钥,String,解密,param,RAS,str,return,jmeter From: https://www.cnblogs.com/JcHome/p/18597539