当前位置:网站首页>Encrypt and decrypt strings using RSA and Base64
Encrypt and decrypt strings using RSA and Base64
2022-06-11 10:38:00 【Almost a senior programmer】
1. Use RSA Public key encryption
/**
* RSA And base64 Encrypted string
* @param json Data to be encrypted
* @param publicKey Public key
* @return Encrypted string
*/
String rsaKeyEncrypt(String json, String publicKey){
String outStr = null;
try {
//base64 Encoded public key
byte[] decoded = org.apache.commons.codec.binary.Base64.decodeBase64(publicKey);
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").
generatePublic(new X509EncodedKeySpec(decoded));
//RSA encryption
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
// When the length is too long , Encryption after splitting is required 117 Bytes
byte[] resultBytes = getMaxResultEncrypt(json, cipher);
outStr = Base64.encodeBase64String(resultBytes);
} catch (Exception e){
e.printStackTrace();
}
return outStr;
}
//RSA And base64 Encrypted string
private static byte[] getMaxResultEncrypt(String str, Cipher cipher) throws IllegalBlockSizeException, BadPaddingException {
byte[] inputArray = str.getBytes();
int inputLength = inputArray.length;
// Maximum encrypted bytes , Exceeding the maximum number of bytes requires packet encryption
int MAX_ENCRYPT_BLOCK = 117;
// identification
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. Use RSA Private key decryption
/**
* RSA Private key decryption
*
* @param str Encrypted string
* @param privateKey Private key
* @return Decrypted string
*/
public static String privateKeyDecrypt(String str, String privateKey) throws Exception {
//64 Bit decode encrypted string
byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8"));
//base64 Encoded private key
byte[] decoded = Base64.decodeBase64(privateKey);
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA")
.generatePrivate(new PKCS8EncodedKeySpec(decoded));
//RSA Decrypt
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, priKey);
// String outStr = new String(cipher.doFinal(inputByte));
// When the length is too long , Decryption after splitting is required 128 Bytes
String outStr = new String(getMaxResultDecrypt(str, cipher));
return outStr;
}
/**
* RSA Private key decryption
*/
private static byte[] getMaxResultDecrypt(String str, Cipher cipher) {
byte[] resultBytes = {};
try {
byte[] inputArray = Base64.decodeBase64(str.getBytes("UTF-8"));
int inputLength = inputArray.length;
// Maximum decryption bytes , Exceeding the maximum number of bytes requires packet encryption
int MAX_ENCRYPT_BLOCK = 128;
// identification
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;
}边栏推荐
- Internet of things security in the era of remote work
- Practical process of selecting DC-DC switching power supply controller
- 云开发mbti人格类型测试助手微信小程序源码
- Leetcode 1952. 三除数
- What is digital twin? A real-time and virtual representation
- 基于位置服务(LBS)的SSM的框架实现的兴趣社交软件平台设计与实现
- NFT 2.0: 下一代的NFT将是精简且值得信赖的NFT
- C语言课程设计题目
- Some code fragments of a universal and confession wall system developed by PHP
- When installing mysql, an error occurred because msvcr120 could not be found DLL, unable to continue code resolution "
猜你喜欢
随机推荐
【Objective-C】‘NSAutoreleasePool‘ is unavailable: not available in automatic reference counting mode
NFT 2.0: the next generation of NFT will be lean and trustworthy
Beginning an excellent emlog theme
数字藏品app系统源码
使用 Ribbon 实现客户端负载均衡
Jedislock redis distributed lock implementation
Arbitrum 基础架构:快速入门
GameFi:您需要了解的关于“即玩即赚”游戏经济的一切
国际多语言出海商城返佣产品自动匹配订单源码
Install MySQL version 5.7 or above on windows (install in compressed package)
Some understanding of inductive bias
Série de démarrage C # (XI) - - tableaux multidimensionnels
NGUI,飘血
Waiting event enq: Ko - some feasible processing methods for fast object checkpoint
電子設備輻射EMC整改案例
Streaming computing knowledge
Ngui, chat scroll box, UI textlist
1. system in Library
C语言校园导游咨询系统
IPhone 15 forced to use type-C interface









