当前位置:网站首页>使用RSA与base64对字符串进行加密解密
使用RSA与base64对字符串进行加密解密
2022-06-11 10:31:00 【差点资深程序员】
1.使用RSA公钥加密
/**
* RSA与base64加密字符串
* @param json 待加密数据
* @param publicKey 公钥
* @return 加密后字符串
*/
String rsaKeyEncrypt(String json, String publicKey){
String outStr = null;
try {
//base64编码的公钥
byte[] decoded = org.apache.commons.codec.binary.Base64.decodeBase64(publicKey);
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").
generatePublic(new X509EncodedKeySpec(decoded));
//RSA加密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
//当长度过长的时候,需要分割后加密 117个字节
byte[] resultBytes = getMaxResultEncrypt(json, cipher);
outStr = Base64.encodeBase64String(resultBytes);
} catch (Exception e){
e.printStackTrace();
}
return outStr;
}
//RSA与base64加密字符串
private static byte[] getMaxResultEncrypt(String str, Cipher cipher) throws IllegalBlockSizeException, BadPaddingException {
byte[] inputArray = str.getBytes();
int inputLength = inputArray.length;
// 最大加密字节数,超出最大字节数需要分组加密
int MAX_ENCRYPT_BLOCK = 117;
// 标识
int offSet = 0;
byte[] resultBytes = {};
byte[] cache = {};
while (inputLength - offSet > 0) {
if (inputLength - offSet > MAX_ENCRYPT_BLOCK) {
cache = cipher.doFinal(inputArray, offSet, MAX_ENCRYPT_BLOCK);
offSet += MAX_ENCRYPT_BLOCK;
} else {
cache = cipher.doFinal(inputArray, offSet, inputLength - offSet);
offSet = inputLength;
}
resultBytes = Arrays.copyOf(resultBytes, resultBytes.length + cache.length);
System.arraycopy(cache, 0, resultBytes, resultBytes.length - cache.length, cache.length);
}
return resultBytes;
}
2.使用RSA私钥解密
/**
* RSA私钥解密
*
* @param str 加密字符串
* @param privateKey 私钥
* @return 解密后的字符串
*/
public static String privateKeyDecrypt(String str, String privateKey) throws Exception {
//64位解码加密后的字符串
byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8"));
//base64编码的私钥
byte[] decoded = Base64.decodeBase64(privateKey);
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA")
.generatePrivate(new PKCS8EncodedKeySpec(decoded));
//RSA解密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, priKey);
// String outStr = new String(cipher.doFinal(inputByte));
//当长度过长的时候,需要分割后解密 128个字节
String outStr = new String(getMaxResultDecrypt(str, cipher));
return outStr;
}
/**
* RSA私钥解密
*/
private static byte[] getMaxResultDecrypt(String str, Cipher cipher) {
byte[] resultBytes = {};
try {
byte[] inputArray = Base64.decodeBase64(str.getBytes("UTF-8"));
int inputLength = inputArray.length;
// 最大解密字节数,超出最大字节数需要分组加密
int MAX_ENCRYPT_BLOCK = 128;
// 标识
int offSet = 0;
byte[] cache = {};
while (inputLength - offSet > 0) {
if (inputLength - offSet > MAX_ENCRYPT_BLOCK) {
cache = cipher.doFinal(inputArray, offSet, MAX_ENCRYPT_BLOCK);
offSet += MAX_ENCRYPT_BLOCK;
} else {
cache = cipher.doFinal(inputArray, offSet, inputLength - offSet);
offSet = inputLength;
}
resultBytes = Arrays.copyOf(resultBytes, resultBytes.length + cache.length);
System.arraycopy(cache, 0, resultBytes, resultBytes.length - cache.length, cache.length);
}
} catch (Exception e) {
e.printStackTrace();
}
return resultBytes;
}边栏推荐
猜你喜欢

详解2.5G/5G/10G Base-T以太网接口物理层一致性测试!

What are the ABAP keywords and syntax that cannot be used in the ABAP cloud environment?

iPhone 15 被迫用上 Type-C 接口

Ugui picture wall

Fix the problem that uicollectionview does not reach the bottom security zone

安装MySQL ,出现由于找不到 MSVCR120.dll,无法继续执行代码解决方法”

Pyramidtnt: TNT with characteristic pyramid structure

Arbitrum infrastructure: a quick start

Tiktok encounters cultural conflict in the UK, and many employees leave in a short time

修复UICollectionView 没有到达底部安全区的问题
随机推荐
[Objective-C] dynamically create controls
NewOJ Week 2---BCD
VMware install win7 virtual machine
Windows 安装MySQL5.7 以上的版本(压缩包形式安装)
电子设备辐射EMC整改案例
RSA signature issues
MySQL 权限管理和备份
【K-Means】K-Means学习实例
Ugui picture wall
Streaming computing knowledge
使用bat向文件的第一行中写入内容
Practical process of selecting DC-DC switching power supply controller
Batch add noise to data and generate new named annotation files
Ngui, map zoom in and out
Sys in kingbasees_ Checksums bad block detection function
Common techniques for handling dates
Wechat applet ordering system with source code
Excellent test engineer must have 4 safety test methods!
利用PHP开发的一款万能、表白墙系统部分代码片段
Start jar