RSA解密时javax.crypto.BadPaddingException: Data must start with zero
来源:互联网 发布:在淘宝怎么才能买到烟 编辑:程序博客网 时间:2024/06/05 05:42
解决方法:要在加密后产生的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!
然后再根据空格分隔转换回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!
0 0
- RSA解密时javax.crypto.BadPaddingException: Data must start with zero
- 利用java api 实现 RSA算法 解决(javax.crypto.BadPaddingException: Data must start with zero)
- 关于解决RSA非对称加密时,将密文存入数据库取出后,再对密文解密的时候抛出javax.crypto.BadPaddingException: Data must start with zero
- RSA Data must start with zero问题解决方法
- SRA解密报错:Data must start with zero
- Data must start with zero
- RSA 加密解密 关于javax.crypto.BadPaddingException: Blocktype错误的几种解决方法
- javax.crypto.BadPaddingException: Blocktype mismatch: 0 RSA加密,Android到服务器解密报错
- RSA javax.crypto.BadPaddingException: Blocktype mismatch: 0 错误
- RSA解密时BadPaddingException解决方法
- AES加解密异常:javax.crypto.BadPaddingException: Invalid pad value!
- javax.crypto.BadPaddingException: error:0407106B:rsa routines:RSA_padding_check_PKCS1_type_2:block t
- AES加解密的错误提示 javax.crypto.BadPaddingException: pad block corrupted
- javax.crypto.BadPaddingException: Given final block not
- Linux下运行java DES解密失败,报javax.crypto.BadPaddingException:Given final block not properly padded
- Linux下运行java DES解密失败,报javax.crypto.BadPaddingException:Given final block not properly padded
- Linux下运行java DES解密失败,报javax.crypto.BadPaddingException:Given final block not properly padded
- Linux下运行java DES解密失败,报javax.crypto.BadPaddingException:Given final block not properly padded
- UI 第六天 UISegmentControl UISlider UIImageView
- 递归调用--汉诺塔
- Android dalvik GC相关的属性详解
- 学习AngularJS的最强大资源集合
- Excel怎么将ABC字母表示的列号改成数字列号?
- RSA解密时javax.crypto.BadPaddingException: Data must start with zero
- 语音识别学习计划1.0
- 动态内存分配的一个小Demo
- CSS中的clear属性
- cocos2d-x学习笔记(4)精灵和菜单
- tcpdump 命令解析
- R语言学习之<代码规范>
- ElasticSearch入门 —— 集群搭建
- css(三)布局模型