SRA解密报错:Data must start with zero
项目背景:要对打印地址进行加密,用公钥加密后会乱码需要base64 decode一下,但是在解密时报错:javax.crypto.BadPaddingException: Data must start with zero
解决办法:
1.加解密时KeyFactory keyFactory = KeyFactory.getInstance("RSA");
2.将加解密的Cipher cipher = Cipher.getInstance(“RSA”)改为Cipher cipher = Cipher.getInstance(“RSA/ECB/NoPadding”)
困扰了两天的问题解决了,代码如下,希望有次问题的同学不必再走此弯路。
-
package resources.util.encryption;
-
-
import java.io.FileInputStream;
-
import java.io.FileOutputStream;
-
import java.io.ObjectInputStream;
-
import java.io.ObjectOutputStream;
-
import java.security.Key;
-
import java.security.KeyFactory;
-
import java.security.KeyPair;
-
import java.security.KeyPairGenerator;
-
import java.security.PrivateKey;
-
import java.security.PublicKey;
-
import java.security.spec.PKCS8EncodedKeySpec;
-
import java.security.spec.X509EncodedKeySpec;
-
-
import javax.crypto.Cipher;
-
-
import org.junit.Test;
-
-
public class EncryptionUtil {
-
private static final String RSA = "RSA";
-
private static final String RSANOPADDING = "RSA/ECB/NoPadding";
-
private static final String PUBLIC_KEY_PATH = "public.key";
-
private static final String PRIVATE_KEY_PATH = "private.key";
-
private static final String path = Thread.currentThread().getContextClassLoader().getResource("/").getPath();
-
// private static final String path = "";
-
@Test
-
public void generateKey() throws Exception {
-
//KeyPairGenerator引擎类用于产生密钥对,JDK(7)默认支持的算法有,DiffieHellman、DSA、RSA、EC
-
KeyPairGenerator generator = KeyPairGenerator.getInstance(RSA);
-
generator.initialize(512);
-
//产生密钥对
-
KeyPair keyPair = generator.generateKeyPair();
-
//获取公钥
-
PublicKey publicKey = keyPair.getPublic();
-
//获取私钥
-
PrivateKey privateKey = keyPair.getPrivate();
-
-
//将公钥与私钥写入文件,以备后用
-
writeKey(PUBLIC_KEY_PATH, publicKey);
-
writeKey(PRIVATE_KEY_PATH, privateKey);
-
}
-
-
//公钥加密
-
public byte[] SRAEncrypt(String src) throws Exception {
-
PublicKey publicKey= (PublicKey)readKey(path + PUBLIC_KEY_PATH);
-
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded());
-
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
-
publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
-
Cipher cipher = Cipher.getInstance(RSANOPADDING);
-
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
-
byte[] data = src.getBytes();
-
int blockSize = 53;//根据异常提示设的53
-
//根据块大小分块,不足一块的部分为一块
-
int blocksNum = (int)Math.ceil((1.0*data.length)/blockSize);
-
//加密
-
for (int i = 0; i < blocksNum; i++) {
-
if (i < blocksNum - 1) {
-
cipher.doFinal(data, i * blockSize, blockSize);
-
} else {
-
cipher.doFinal(data, i * blockSize, data.length - i * blockSize);
-
}
-
}
-
return data;
-
}
-
-
//私钥解密
-
public String SRADecrypt(byte[] data) throws Exception{
-
PrivateKey privateKey= (PrivateKey)readKey(path + PRIVATE_KEY_PATH);
-
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded());
-
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
-
privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
-
Cipher cipher = Cipher.getInstance(RSANOPADDING);
-
cipher.init(Cipher.DECRYPT_MODE, privateKey);
-
int blockSize = 64;//根据异常提示设的64
-
//根据块大小分块,不足一块的部分为一块
-
int blocksNum = (int)Math.ceil((1.0*data.length)/blockSize);
-
//解密
-
for (int i = 0; i < blocksNum; i++) {
-
if (i < blocksNum - 1) {
-
cipher.doFinal(data, i * blockSize, blockSize);
-
} else {
-
cipher.doFinal(data, i * blockSize, data.length - i * blockSize);
-
}
-
}
-
return new String(data);
-
}
-
-
public void writeKey(String path, Key key) throws Exception {
-
FileOutputStream fos = new FileOutputStream(path);
-
ObjectOutputStream oos = new ObjectOutputStream(fos);
-
oos.writeObject(key);
-
oos.close();
-
}
-
-
public Key readKey(String path) throws Exception {
-
FileInputStream fis = new FileInputStream(path);
-
ObjectInputStream bis = new ObjectInputStream(fis);
-
Object object = bis.readObject();
-
bis.close();
-
return (Key)object;
-
}
-
-
@Test
-
public void testEncryptAndDecrypt() throws Exception {
-
Cipher cipher = Cipher.getInstance(RSA);
-
//读取公钥,进行加密
-
PublicKey publicKey= (PublicKey) readKey("component/"+PUBLIC_KEY_PATH);
-
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
-
//加密
-
String sendInfo = "我的明文";
-
byte[] results = cipher.doFinal(sendInfo.getBytes());
-
-
//读取私钥,进行解密
-
PrivateKey privateKey = (PrivateKey) readKey("component/"+PRIVATE_KEY_PATH);
-
cipher.init(Cipher.DECRYPT_MODE, privateKey);
-
//解密
-
byte[] deciphered = cipher.doFinal(results);
-
//得到明文
-
String recvInfo = new String(deciphered);
-
System.out.println(recvInfo);
-
}
-
-
@Test
-
public void testSRA() throws Exception{
-
PublicKey publicKey= (PublicKey)readKey("component/" + PUBLIC_KEY_PATH);
-
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded());
-
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
-
publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
-
Cipher cipher = Cipher.getInstance(RSA);
-
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
-
byte[] result = cipher.doFinal("yuanyuan".getBytes());
-
-
PrivateKey privateKey= (PrivateKey)readKey("component/" + PRIVATE_KEY_PATH);
-
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded());
-
privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
-
cipher = Cipher.getInstance(RSANOPADDING);
-
cipher.init(Cipher.DECRYPT_MODE, privateKey);
-
result = cipher.doFinal(result);
-
System.out.println(new String(result));
-
}
-
}
SRA解密报错:Data must start with zero的更多相关文章
- JAVA实现AES 解密报错Input length must be multiple of 16 when decrypting with padded cipher
加密代码 /**解密 * @param content 待解密内容 * @param password 解密密钥 * @return */ public static byte[] decrypt(b ...
- 微信第三方平台解密报错:Illegal key size
今天在交接别人代码的时候遇到的,微信第三方平台解密报的错误,原因: 如果密钥大于128, 会抛出java.security.InvalidKeyException: Illegal key size ...
- RSA解密报错java.security.spec.InvalidKeySpecException的解决办法
java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException : algid p ...
- 安装mysql_cluster报错: Data::Dumper丢失
步骤 安装包:mysql-cluster-gpl-7.3.5-linux-glibc2.5-x86_64.tar.gz 下载解压到/usr/local/mysql mkdir /usr/local/m ...
- Echart..js插件渲染报错 data.length<1?
问题 getJSON提交 返回数据正常,在传入参数进行序列化,渲染报表时报错 option.data.length < 1. 分析 1.可能情况一: . 可自己明明是getJSON()把渲染放 ...
- dubbo报错Data length too large: 10710120处理,及服务提供者协议配置详细说明
工作中遇到以下报错信息 cause: java.io.IOException: Data length too large: 10710120, max payload: 8388608, chann ...
- PHP执行insert语句报错“Data too long for column”解决办法
PHP执行mysql 插入语句, insert语句在Navicat for mysql(或任意的mysql管理工具) 中可以正确执行,但是用mysql_query()函数执行却报错. 错误 : “Da ...
- 【翻译】详解HTML5 自定义 Data 属性
原标题:HTML5 Custom Data Attributes (data-*) 你是否曾经使用 class 或 rel 来保存任意的元数据,只为了使你的JavaScript更简单?如果你回答是的, ...
- OC学习笔记之属性详解和易错点
属性的概念在OC1.0中就存在,格式是定义实例变量,然后定义setter和getter方法,用点操作符操作属性 举例,类的接口部分 @interface Father : NSObject { NSI ...
随机推荐
- Java 大数
How Many Fibs? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- 【Oracle错误集锦】:PLSQL无法直连64位Oracle11g数据库
背景:Oracle数据库装在本机上,使用PLSQL连接. 今天安装完Oracle 11g数据库后.用plsql连接数据库死活都连接不上.而且plsql客户端登录窗体的Database下拉框还为空.见下 ...
- php的数据类型和变量的作用域
1)php支持例如以下所看到的的基本数据类型: Integer(整数).Float(浮点数).String(字符串).Boolean(布尔值).Array(数组).Object(对象),此外还有两个特 ...
- vue 星星评分组件
显示评分和打分组件,可现实半颗星星效果 效果图: 参数名 类型 说明 score Number 分数 ,默认0,保留一位小数 disabled Boolean 是否只读,默认false,鼠标点击可以打 ...
- JS学习笔记 - fgm练习 - 限制输入框的字符类型 正则 和 || 或运算符的运用 i++和++i
<script> window.onload = function(){ var aInp = document.getElementsByTagName('input'); var oS ...
- [Angular] Using the Argon 2 Hashing Function In Our Sign Up Backend Service
Which hash algorithom to choose for new application: https://www.owasp.org/index.php/Password_Storag ...
- Windows 7 下快速挂载和分离VHD文件的小脚本
1.保存以下代码为VDM.vbs,放在Windows\system32下 Dim ArgsSet Args = WScript.ArgumentsTranArgs = " "For ...
- BAT面试常的问题和最佳答案
原标题:BAT面试常的问题和最佳答案 技术面试 1.servlet执行流程 客户端发出http请求,web服务器将请求转发到servlet容器,servlet容器解析url并根据web.xml找到相对 ...
- sum()函数——MATLAB
a=sum(A) %列求和 b=sum(A,2) %行求和 c=sum(A(:)) %矩阵求和 假定A为一个矩阵: sum(A)以矩阵A的每一列为对象,对一列内的数字求和. sum(A,2)以矩阵A ...
- 7、linux系统2440开发板域名解析问题
如果在linux系统中ping某一台电脑的ip地址可以ping 通: ~ >: ping 192.168.1.3PING 192.168.1.3 (192.168.1.3): 56 data b ...