Android(安卓)RSA 公钥加密、解密
16lz
2021-12-04
package com.linekong.sdk.utils;
import java.security.KeyFactory;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import android.util.Base64;
/**
* @author changcsw 2015-11-11
*
*/
public class RSAUtils {
private static RSAPublicKey publicKey = null;
/**************************** RSA 公钥加密解密**************************************/
/**
* 从字符串中加载公钥,从服务端获取
*
* @param publicKeyStr
* 公钥数据字符串
* @throws Exception
* 加载公钥时产生的异常
*/
public static void loadPublicKey(String pubKey) {
try {
byte[] buffer = Base64.decode(pubKey, Base64.DEFAULT);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
publicKey = (RSAPublicKey) keyFactory.generatePublic(keySpec);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 公钥加密过程
*
* @param publicKey
* 公钥
* @param plainData
* 明文数据
* @return
* @throws Exception
* 加密过程中的异常信息
*/
public static String encryptWithRSA(String plainData) throws Exception {
if (publicKey == null) {
throw new NullPointerException("encrypt PublicKey is null !");
}
Cipher cipher = null;
cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");// 此处如果写成"RSA"加密出来的信息JAVA服务器无法解析
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] output = cipher.doFinal(plainData.getBytes("utf-8"));
// 必须先encode成 byte[],再转成encodeToString,否则服务器解密会失败
byte[] encode = Base64.encode(output, Base64.DEFAULT);
return Base64.encodeToString(encode, Base64.DEFAULT);
}
/**
* 公钥解密过程
*
* @param publicKey
* 公钥
* @param encryedData
* 明文数据
* @return
* @throws Exception
* 加密过程中的异常信息
*/
public static String decryptWithRSA(String encryedData) throws Exception {
if (publicKey == null) {
throw new NullPointerException("decrypt PublicKey is null !");
}
Cipher cipher = null;
cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");// 此处如果写成"RSA"解析的数据前多出来些乱码
cipher.init(Cipher.DECRYPT_MODE, publicKey);
byte[] output = cipher.doFinal(Base64.decode(encryedData, Base64.DEFAULT));
return new String(output);
}
/**************************** RSA 公钥加密解密**************************************/
}
import java.security.KeyFactory;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import android.util.Base64;
/**
* @author changcsw 2015-11-11
*
*/
public class RSAUtils {
private static RSAPublicKey publicKey = null;
/**************************** RSA 公钥加密解密**************************************/
/**
* 从字符串中加载公钥,从服务端获取
*
* @param publicKeyStr
* 公钥数据字符串
* @throws Exception
* 加载公钥时产生的异常
*/
public static void loadPublicKey(String pubKey) {
try {
byte[] buffer = Base64.decode(pubKey, Base64.DEFAULT);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
publicKey = (RSAPublicKey) keyFactory.generatePublic(keySpec);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 公钥加密过程
*
* @param publicKey
* 公钥
* @param plainData
* 明文数据
* @return
* @throws Exception
* 加密过程中的异常信息
*/
public static String encryptWithRSA(String plainData) throws Exception {
if (publicKey == null) {
throw new NullPointerException("encrypt PublicKey is null !");
}
Cipher cipher = null;
cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");// 此处如果写成"RSA"加密出来的信息JAVA服务器无法解析
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] output = cipher.doFinal(plainData.getBytes("utf-8"));
// 必须先encode成 byte[],再转成encodeToString,否则服务器解密会失败
byte[] encode = Base64.encode(output, Base64.DEFAULT);
return Base64.encodeToString(encode, Base64.DEFAULT);
}
/**
* 公钥解密过程
*
* @param publicKey
* 公钥
* @param encryedData
* 明文数据
* @return
* @throws Exception
* 加密过程中的异常信息
*/
public static String decryptWithRSA(String encryedData) throws Exception {
if (publicKey == null) {
throw new NullPointerException("decrypt PublicKey is null !");
}
Cipher cipher = null;
cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");// 此处如果写成"RSA"解析的数据前多出来些乱码
cipher.init(Cipher.DECRYPT_MODE, publicKey);
byte[] output = cipher.doFinal(Base64.decode(encryedData, Base64.DEFAULT));
return new String(output);
}
/**************************** RSA 公钥加密解密**************************************/
}
更多相关文章
- Android(安卓)SharedPreferences本地缓存
- 数据存储之——Android文件存储系统及文件系统(Android(安卓)Q)
- 每日一道Android(安卓)面试题,面试途中不败题
- 数据存储之——Android内、外存储分区&常用存储目录详解(Android(
- Android基础笔记(二)-数据存储和界面展现
- 2011Android技术面试整理附有详细答案(包括百度、新浪、中科软等
- 对Android及移动互联网的大局观看法!
- 对Android及移动互联网的大局观看法!
- Android串口通信:抱歉,学会它真的可以为所欲为