目前在使用rsa加密时遇到了一点点小麻烦
就是不管是服务端还是Android端自己加密自己解密都没有问题,一旦是 服务器加密 安卓解密 或是 安卓加密 服务器解密 就各出各的错
服务端报的错
Android端出现乱码
解决的问题是出现在了Cipher这个类上了,Cipher服务端与Android调用内在不一致
只需要让Android的
Cipher cipher = Cipher.getInstance("RSA"); 换成 Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
以下是完整代码以及各个方法的解释
需要引入的依赖
服务端
<dependency>
<groupId>commons-codec</groupId> <!--apache commons codec-->
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
Android端
implementation 'commons-codec:commons-codec:1.15'
或
implementation("commons-codec:commons-codec:1.15")
读取文件与写入文件的代码
/**
* io流写入
*/
private static void writeFile(String pubPath,String content){
OutputStreamWriter writer = null;
try {
writer = new OutputStreamWriter(new FileOutputStream(pubPath), "UTF-8");
writer.write(content);
} catch (IOException e) {
e.printStackTrace();
}finally {
if (writer != null){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* io读取流
*/
private static String readFile(String pubPath){
// String filePath = "path/to/your/file.txt"; // 替换为你的文件路径
StringBuffer s = new StringBuffer();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(pubPath), "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
// System.out.println(line);
s.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return s.toString();
}
生成公钥与私钥的代码
/**
* 生成密钥对并保存在本地文件中
*
* @param algorithm : 算法
* @param pubPath : 公钥保存路径
* @param priPath : 私钥保存路径
* @throws Exception
*/
private static void generateKeyToFile(String algorithm, String pubPath, String priPath) throws Exception {
// 获取密钥对生成器
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm);
keyPairGenerator.initialize(KEY_LENGTH); // 秘钥字节数
// 获取密钥对
KeyPair keyPair = keyPairGenerator.generateKeyPair();
// 获取公钥
PublicKey publicKey = keyPair.getPublic();
// 获取私钥
PrivateKey privateKey = keyPair.getPrivate();
// 获取byte数组
byte[] publicKeyEncoded = publicKey.getEncoded();
byte[] privateKeyEncoded = privateKey.getEncoded();
// 进行Base64编码
String publicKeyString = new String(Base64.encodeBase64(publicKeyEncoded));
String privateKeyString = new String(Base64.encodeBase64(privateKeyEncoded));
// 保存文件
writeFile(pubPath,publicKeyString);
writeFile(priPath,privateKeyString);
// FileUtils.writeStringToFile(new File(pubPath), publicKeyString, Charset.forName("UTF-8"));
// FileUtils.writeStringToFile(new File(priPath), privateKeyString, Charset.forName("UTF-8"));
}
Android端加密也就是公钥加密:
/**
* 公钥加密
*
* @param data
* @param publicKey
* @return
* @throws InvalidKeySpecException
*/
public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Exception {
// 得到公钥
byte[] keyBytes = Base64.decodeBase64(publicKey.getBytes());
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
Key key = keyFactory.generatePublic(x509EncodedKeySpec);
// 加密数据,分段加密
// Cipher cipher = Cipher.getInstance(ALGORITHM);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
int inputLength = data.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offset = 0;
byte[] cache;
int i = 0;
while (inputLength - offset > 0) {
if (inputLength - offset > MAX_ENCRYPT_BLOCK) {
cache = cipher.doFinal(data, offset, MAX_ENCRYPT_BLOCK);
} else {
cache = cipher.doFinal(data, offset, inputLength - offset);
}
out.write(cache, 0, cache.length);
i++;
offset = i * MAX_ENCRYPT_BLOCK;
}
byte[] encryptedData = out.toByteArray();
out.close();
return encryptedData;
}
服务端解密也就是私钥解密:
/**
* 私钥解密
*
* @param data
* @param privateKey
* @return
* @throws Exception
*/
public static byte[] decryptByPrivateKey(byte[] data, String privateKey) throws Exception {
// 得到私钥
byte[] keyBytes = Base64.decodeBase64(privateKey.getBytes());
PKCS8EncodedKeySpec pKCS8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
Key key = keyFactory.generatePrivate(pKCS8EncodedKeySpec);
// 解密数据,分段解密
// Cipher cipher = Cipher.getInstance(ALGORITHM);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
int inputLength = data.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offset = 0;
byte[] cache;
int i = 0;
byte[] tmp;
while (inputLength - offset > 0) {
if (inputLength - offset > MAX_DECRYPT_BLOCK) {
cache = cipher.doFinal(data, offset, MAX_DECRYPT_BLOCK);
} else {
cache = cipher.doFinal(data, offset, inputLength - offset);
}
// out.write(cache, 0, cache.length);
out.write(cache);
i++;
offset = i * MAX_DECRYPT_BLOCK;
}
byte[] decryptedData = out.toByteArray();
out.close();
return decryptedData;
}
最后上完整代码
package com.example.demo4.rsa;
import java.io.*;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64;
public class Demo {
private static final String ALGORITHM = "RSA";
// private static final String PUBLICK_EY = "PUBLICK_EY";
// private static final String PRIVATE_KEY = "PRIVATE_KEY";
/**
// * 加密算法
// */
// private static final String CIPHER_DE = "RSA";
// /**
// * 解密算法
// */
// private static final String CIPHER_EN = "RSA";
/**
* 密钥长度
*/
private static final Integer KEY_LENGTH = 1024;
/**
* RSA最大加密明文大小
*/
private static final int MAX_ENCRYPT_BLOCK = 117;
/**
* RSA最大解密密文大小
*/
private static final int MAX_DECRYPT_BLOCK = 128;
// private static final int MAX_DECRYPT_BLOCK = 256;
/**
* 公钥加密
*
* @param data
* @param publicKey
* @return
* @throws InvalidKeySpecException
*/
public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Exception {
// 得到公钥
byte[] keyBytes = Base64.decodeBase64(publicKey.getBytes());
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
Key key = keyFactory.generatePublic(x509EncodedKeySpec);
// 加密数据,分段加密
// Cipher cipher = Cipher.getInstance(ALGORITHM);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
int inputLength = data.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offset = 0;
byte[] cache;
int i = 0;
while (inputLength - offset > 0) {
if (inputLength - offset > MAX_ENCRYPT_BLOCK) {
cache = cipher.doFinal(data, offset, MAX_ENCRYPT_BLOCK);
} else {
cache = cipher.doFinal(data, offset, inputLength - offset);
}
out.write(cache, 0, cache.length);
i++;
offset = i * MAX_ENCRYPT_BLOCK;
}
byte[] encryptedData = out.toByteArray();
out.close();
return encryptedData;
}
/**
* 私钥解密
*
* @param data
* @param privateKey
* @return
* @throws Exception
*/
public static byte[] decryptByPrivateKey(byte[] data, String privateKey) throws Exception {
// 得到私钥
byte[] keyBytes = Base64.decodeBase64(privateKey.getBytes());
PKCS8EncodedKeySpec pKCS8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
Key key = keyFactory.generatePrivate(pKCS8EncodedKeySpec);
// 解密数据,分段解密
// Cipher cipher = Cipher.getInstance(ALGORITHM);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
int inputLength = data.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offset = 0;
byte[] cache;
int i = 0;
byte[] tmp;
while (inputLength - offset > 0) {
if (inputLength - offset > MAX_DECRYPT_BLOCK) {
cache = cipher.doFinal(data, offset, MAX_DECRYPT_BLOCK);
} else {
cache = cipher.doFinal(data, offset, inputLength - offset);
}
// out.write(cache, 0, cache.length);
out.write(cache);
i++;
offset = i * MAX_DECRYPT_BLOCK;
}
byte[] decryptedData = out.toByteArray();
out.close();
return decryptedData;
}
/**
* io流写入
*/
private static void writeFile(String pubPath,String content){
OutputStreamWriter writer = null;
try {
writer = new OutputStreamWriter(new FileOutputStream(pubPath), "UTF-8");
writer.write(content);
} catch (IOException e) {
e.printStackTrace();
}finally {
if (writer != null){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* io读取流
*/
private static String readFile(String pubPath){
// String filePath = "path/to/your/file.txt"; // 替换为你的文件路径
StringBuffer s = new StringBuffer();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(pubPath), "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
// System.out.println(line);
s.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return s.toString();
}
/**
* 生成密钥对并保存在本地文件中
*
* @param algorithm : 算法
* @param pubPath : 公钥保存路径
* @param priPath : 私钥保存路径
* @throws Exception
*/
private static void generateKeyToFile(String algorithm, String pubPath, String priPath) throws Exception {
// 获取密钥对生成器
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm);
keyPairGenerator.initialize(KEY_LENGTH); // 秘钥字节数
// 获取密钥对
KeyPair keyPair = keyPairGenerator.generateKeyPair();
// 获取公钥
PublicKey publicKey = keyPair.getPublic();
// 获取私钥
PrivateKey privateKey = keyPair.getPrivate();
// 获取byte数组
byte[] publicKeyEncoded = publicKey.getEncoded();
byte[] privateKeyEncoded = privateKey.getEncoded();
// 进行Base64编码
String publicKeyString = new String(Base64.encodeBase64(publicKeyEncoded));
String privateKeyString = new String(Base64.encodeBase64(privateKeyEncoded));
// 保存文件
writeFile(pubPath,publicKeyString);
writeFile(priPath,privateKeyString);
// FileUtils.writeStringToFile(new File(pubPath), publicKeyString, Charset.forName("UTF-8"));
// FileUtils.writeStringToFile(new File(priPath), privateKeyString, Charset.forName("UTF-8"));
}
public static void main(String[] args) throws Exception {
// 加密算法
// String algorithm = "RSA";
// generateKeyToFile(ALGORITHM, "pubObj.pem", "priObj.pem");
// 读取私钥
// 从文件中读取私钥字符串
// String privateKey = readFile("priObj.pem");
// String publicKey = readFile("pubObj.pem");
System.err.println("privateKeyString "+privateKeyString);
// System.err.println("publicKeyString "+publicKeyString);
// PrivateKey privateKeyObj = readPrivateKeyFromFile(algorithm, "priObj.pem");
//
读取公钥
// PublicKey publicKeyObj = readPublicKeyFromFile(algorithm, "pubObj.pem");
// String publicKey = new String(Base64.encodeBase64(privateKeyString.getBytes()));
// String privateKey = new String(Base64.encodeBase64(publicKeyString.getBytes()));
// System.err.println("publicKey "+publicKey);
// System.err.println("privateKey "+privateKey);
//
// genKeyPair();
// Map<String, Object> keyMap = Demo.genKeyPair();
// String publicKey = Demo.getPublicKey(keyMap);
// String privateKey = Demo.getPrivateKey(keyMap);
// System.out.println("公钥:" + publicKey);
// System.out.println("私钥:" + privateKey);
// String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDFIn1hFiiyO4GQU9yq9jSi41jxQEDw8GrQXMk9fgrsECiYspNAQeHAd0ujAbxexJLXINRIo25Y4d0W+q7tv/vcvYybIlnQhTk96azV4WhdGW21Xgpmp04EZBRybXmimahHT0rPbXR+Jm8M71V2AfwZeFFQZv2+ldAUcd1ab3owQwIDAQAB";
// String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAMUifWEWKLI7gZBT3Kr2NKLjWPFAQPDwatBcyT1+CuwQKJiyk0BB4cB3S6MBvF7Ektcg1Eijbljh3Rb6ru2/+9y9jJsiWdCFOT3prNXhaF0ZbbVeCmanTgRkFHJteaKZqEdPSs9tdH4mbwzvVXYB/Bl4UVBm/b6V0BRx3VpvejBDAgMBAAECgYAHrGMC/3DQFz7g1lLVmMXNvVm98klU15bgy2vN5/UW4T4jS3gt3SKjckFQdAXYDoDEvPNmbG4kKY6IkXNzr7BnAr2oRtR4ER4tpE4xlmUaLsG1/dHQeS/7tkHNsUH+PgTQTVESCibiVBb2pZbLu93WJMd7kR/uKpW78WsWWC5uqQJBAOkIUHwuRFhMqY37PRUWbxfbfPP+lJHGe2Epqd4UgxeOuMCU/2H0VHU/3cwal/APCfDHQQ9Ewrkk4B2jtLfUdwUCQQDYkG66KnQ7jRFZZvHz4+KUf5R8IRDnn1QzowlIE2K+Tk9sJsYwIxZtUQeLDPlMrO8ncFW6uI2fdqq64hAqLBynAkAGC3UrA3g6YiKz0CSRe3d+Qu9G1P+9mExzBMWnQRj1b3pTZIoLXG8ka3exZ68xRvKXEFQbtUjnzJETs79oVVaRAkBcGThtxaY+uSR6r9QP04dFfehIzcshpdmqSBinZU8l/am7JdY9HH7qKH6bvP28hQMD87sRuHDKMy4yYfdb5/ybAkEAhYqv5kXt6XsXSFEll9YfLlUOLgFd8k7kqu5uG2hQfa0cF5jeKeOzedBl5Z/AXM3QX7bD530h38z3UYiyCRTpVw==";
// 从文件中读取私钥字符串
String privateKey = readFile("priObj.pem");
String publicKey = readFile("pubObj.pem");
System.err.println("publicKey "+publicKey);
System.err.println("privateKey "+privateKey);
// 公钥加密
String sourceStr = "dsfasdfsdfsdsfsdgsdg";
System.out.println("加密前:" + sourceStr);
byte[] encryptStrByte = Demo.encryptByPublicKey(sourceStr.getBytes(), publicKey);
byte[] btt = Base64.encodeBase64(encryptStrByte);
System.err.println("btt "+btt);
String encryptStr = new String(btt);
System.out.println("加密后:" + encryptStr);
System.out.println("长度:" + encryptStr.length());
// 私钥解密
String aaaa = "TRZger22peQ4BIy0Y4D5QYY4aS91vicNU8sawJ1AA2LrQv4XVXkdgK+mmZAbHo8QUhmN88z5iGweuU8nIG3ac5a4wOTX7yy7waRotzXhtU2eMj1wIpBjV7XjDGcNx3qSe7m3XHkTPq/C+bYOJ3SiyReZrPPxsRCsmXDEolY5KEE=";
//
byte[] bytes = encryptStr.getBytes();
System.err.println("bytes "+bytes);
byte[] decryptStrByte = Demo.decryptByPrivateKey(Base64.decodeBase64(bytes), privateKey);
String sourceStr_1 = new String(decryptStrByte);
System.out.println("解密后:" + sourceStr_1);
}
}