RSA解密时javax.crypto.BadPaddingException: Data must start with zero
解决方法:要在加密后产生的byte数组转成string时要在各byte之间加个标识符,我加了个空格,
然后再根据空格分隔转换回byte数组。如果不加标识符,由于byte值可能是一位到三位,无法知道某一个byte是在哪里结束。当然也可以在转成string时补0。或者转成16进制固定为两位长。
code:
public class RSATest {
public static void main(String[] args) {
try {
RSATest encrypt = new RSATest();
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(1024);
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
String str = "Hello World!";
System.out.println("String will be encrypted: " + str);
byte[] e = encrypt.encrypt(publicKey, str.getBytes());
String tmp1 = encrypt.bytesToString(e);
System.out
.println("encrypted String's bytes, use
bytesToString() method convert bytes to string: " + tmp1);
String[] strArr = tmp1.split(" ");
int len = strArr.length;
byte[] clone = new byte[len];
for (int i = 0; i < len; i++) {
clone[i] = Byte.parseByte(strArr[i]);
}
System.out.println("convert to String, then back to bytes
again: " + encrypt.bytesToString(clone));
byte[] d = encrypt.decrypt(privateKey, clone);
System.out.println("decrypted String's bytes, use
bytesToString() method convert bytes to string:"
+ encrypt.bytesToString(d));
System.out.println("construct a string by decrypted
string's bytes: " + new String(d));
} catch (Exception e) {
e.printStackTrace();
}
}
protected String bytesToString(byte[] encrytpByte) {
String result = "";
for (Byte bytes : encrytpByte) {
result += bytes.toString() + " ";
}
return result;
}
protected byte[] encrypt(RSAPublicKey publicKey, byte[] obj) {
if (publicKey != null) {
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(obj);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
protected byte[] decrypt(RSAPrivateKey privateKey, byte[] obj) {
if (privateKey != null) {
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(obj);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
}
运行结果:
String will be encrypted: Hello World!
encrypted String's bytes, use bytesToString() method convert bytes to
string: 79 62 -105 -47 -61 45 64 -11 -8 -120 30 31 37 -111 49 -30 88
-12 93 -77 3 39 -13 -18 68 -104 0 30 85 26 104 15 -126 -39 12 110 -84
68 -43 73 35 121 -20 -69 -84 85 -33 -123 -48 -68 -85 -106 41 -84 20
-17 87 -81 42 -67 -87 122 -2 37 74 27 103 112 58 -125 -87 -32 96 -56
65 -2 -103 -28 70 107 2 28 87 75 -8 -62 54 12 -7 -108 -123 120 -63 -83
13 -89 -21 58 -51 -84 66 25 103 -114 -14 110 80 58 74 95 -57 -73 -78
-46 56 -83 -72 -38 2 43 25 12 56 12 101 15 91 -37
convert to String, then back to bytes again: 79 62 -105 -47 -61 45 64
-11 -8 -120 30 31 37 -111 49 -30 88 -12 93 -77 3 39 -13 -18 68 -104 0
30 85 26 104 15 -126 -39 12 110 -84 68 -43 73 35 121 -20 -69 -84 85
-33 -123 -48 -68 -85 -106 41 -84 20 -17 87 -81 42 -67 -87 122 -2 37 74
27 103 112 58 -125 -87 -32 96 -56 65 -2 -103 -28 70 107 2 28 87 75 -8
-62 54 12 -7 -108 -123 120 -63 -83 13 -89 -21 58 -51 -84 66 25 103
-114 -14 110 80 58 74 95 -57 -73 -78 -46 56 -83 -72 -38 2 43 25 12 56
12 101 15 91 -37
decrypted String's bytes, use bytesToString() method convert bytes to
string:72 101 108 108 111 32 87 111 114 108 100 33
construct a string by decrypted string's bytes: Hello World!
RSA解密时javax.crypto.BadPaddingException: Data must start with zero的更多相关文章
- java rsa 解密报:javax.crypto.BadPaddingException: Decryption error
Exception in thread "main" javax.crypto.BadPaddingException: Decryption error at sun.se ...
- 关于javax.crypto.BadPaddingException: Blocktype错误的几种解决方法
此文章转载自:http://www.myexception.cn/mobile/1259076.html 关于javax.crypto.BadPaddingException: Blocktype异常 ...
- javax.crypto.BadPaddingException: Given final block not properly padded 解决方法
下面的 Des 加密解密代码,在加密时正常,但是在解密是抛出错误: javax.crypto.BadPaddingException: Given final block not properly p ...
- exception javax.crypto.BadPaddingException: Given final block not properly padded
exception javax.crypto.BadPaddingException: Given final block not properly padded CreationTime--20 ...
- javax.crypto.BadPaddingException: Given final block not properly padded
一.报错 写了一个加密方法,在Windows上运行没有问题,在Linux上运行时提示如下错误: javax.crypto.BadPaddingException: Given final block ...
- javax.crypto.BadPaddingException: Given final block not properly padded解决方案
解密的时候报错: javax.crypto.BadPaddingException: Given final block not properly padded 该异常是在解密 ...
- 左右 android AES 所述机器的一部分 javax.crypto.BadPaddingException: pad block corrupted
好多人 android 使用上述 AES 显现 javax.crypto.BadPaddingException: pad block corrupted 下面的代码发布没问题,比较自己.不解释! p ...
- android AES 部分机器javax.crypto.BadPaddingException: pad block corrupted
package com.bbguoxue.poetry.util; import java.security.SecureRandom; import javax.crypto.Cipher; imp ...
- android 上AES解密是报错javax.crypto.BadPaddingException: pad block corrupted
网上看到两种方法: 1.SecretKeySpec skeySpec = new SecretKeySpec(getRawKey(key), "AES"); private sta ...
随机推荐
- Docker 内程序时间设置,很重要
原文:Docker 内程序时间设置,很重要 重要!!!!! 创建容器时候需要修改一个参数,设置tomcat的时区 -e TZ="Asia/Shanghai" -v /etc/loc ...
- spring @configuration使用
http://yaobenzhang.blog.163.com/blog/static/2143951132014811105138824/
- solr/lucence和关系数据库的混合使用
我们知道solr提供了一个DIHandler,提供将关系数据库中的数据导成索引,然后使用solr查询. 对于一个大表中关联数个小表的查询,这非常耗费时间. 我的思路是: 1. 将一个大表做成索引,使用 ...
- Makefile的问题
注意包含路径的变量,可能会带有空格而使得运行失败 直接定义:DEBUG = false后面引用需要使用 $(DEBUG) 或者set DEBUG $(DEBUG)之后就可以直接引用了
- .v 和 .sdf
DC输出的.v(网表?)和.sdf(储存的是延时的信息) 用于后仿真
- MyBatis学习总结(13)——Mybatis查询之resultMap和resultType区别
MyBatis的每一个查询映射的返回类型都是ResultMap,只是当我们提供的返回类型属性是resultType的时候,MyBatis对自动的给我们把对应的值赋给resultType所指定对象的属性 ...
- 代码高亮显示——google-code-prettify
先放着,搭建完HEXO博客再来写这篇. https://code.google.com/archive/p/google-code-prettify/
- python3 登录验证小程序,同一用户输错三次密码,锁定账户
''' 让用户输入用户名密码 认证成功后显示欢迎信息用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态''' # !/usr/bin/env python # -*- coding:u ...
- HDU2438 Turn the corner【三分法】【数学几何】
Turn the corner Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- client产生CLOSE_WAIT状态的解决方式
现象 生产环境和測试环境都发现有个外围应用通过搜索服务调用搜索引擎时.偶尔会出现大量的訪问超时的问题,通过例如以下方式进行分析排查: l 首先是拿到搜索服务的JavaCore.发现其堵在HttpCli ...