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 ...
随机推荐
- 如何使用maven 打包源代码呢?
如何使用maven 打包源代码呢? http://hw1287789687.iteye.com/blog/1943157
- HDU 4927 Series 1(推理+大数)
HDU 4927 Series 1 题目链接 题意:给定一个序列,要求不断求差值序列.直到剩一个,输出这个数字 思路:因为有高精度一步.所以要推理一下公式,事实上纸上模拟一下非常easy推出公式就是一 ...
- 14.ZooKeeper Java API 使用样例
转自:http://www.aboutyun.com/thread-7332-1-1.html package com.taobao.taokeeper.research.sample; import ...
- mysql 获取自增id的值的方法
原生jdbc方式: Statement.getGeneratedKeys() 示例: Statement stmt = null; ResultSet rs = null; try { // // C ...
- 认识一下JavaScript
1.什么是JavaScript? JavaScript简称“JS”,应用于HTML和WEB,广泛应用于服务器.PC.笔记本等设备. JavaScript 是 Web 的编程语言. 所有现代的 HTML ...
- Python产生随机数组,测试用
import numpy as np if __name__ == '__main__': a=np.random.randint(0,10,size=[3,3]) print(a) 输出: [ ...
- Vue 使用use、prototype自定义自己的全局组件
使用Vue.use()写一个自己的全局组件. 目录如下: 然后在Loading.vue里面定义自己的组件模板 <template> <div v-if="loadFlag& ...
- 为什么出现ORM
ORM(Object Relational Mapping)对象关系映射,是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术 . 为什么出现ORM? 面向对象的特征:我们通常使用的开发语言J ...
- python基础-合并列表
1.append() 向列表尾部追加一个新元素,列表只占一个索引位,在原有列表上增加 2.extend() 向列表尾部追加一个列表,将列表中的每个元素都追加进来,在原有列表上增加 3.+ 直接用+ ...
- Android(Lollipop/5.0) Material Design(一) 简单介绍
Material Design系列 Android(Lollipop/5.0)Material Design(一) 简单介绍 Android(Lollipop/5.0)Material Design( ...