RSA Data must start with zero问题解决方法
2017-03-17 10:10
1021 查看
RSA工具使用公钥加密之后生成byte[] ,直接解密是没有问题的,但将byte[]通过Base64.encode(byte[])之后得到的字符串重新getBytes()去解密是会出现RSA Data must start with zero的问题的。
解决方法:
/**
* 将加密后的字节数组转换为对象
*
* @MethodName: bytesToString
* @Description:
* @param encrytpByte
* @return
* @throws
*/
public static String bytesToString(byte[] encrytpByte) {
String result = "";
for (Byte bytes : encrytpByte) {
result += bytes.toString() + " ";
}
return result;
}
/**
* 公钥加密
*
* @MethodName: encrypt
* @Description:
* @param publicKey
* @param obj
* @return
* @throws
*/
public static byte[] encrypt(RSAPublicKey publicKey, byte[] obj) {
if (publicKey != null) {
try {
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(obj);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* 私钥解密
*
* @MethodName: decrypt
* @Description:
* @param privateKey
* @param obj
* @return
* @throws
*/
public static byte[] decrypt(RSAPrivateKey privateKey, byte[] obj) {
if (privateKey != null) {
try {
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(obj);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}测试方法:
@Test
public void testRSA() throws Exception{
String publicKey=EhcacheUtil.get(StaticProperty.WMS_OPEN_API_RSA_PUBLIC_KEY).toString();
String privateKey=EhcacheUtil.get(StaticProperty.WMS_OPEN_API_RSA_PRIVATE_KEY).toString();
String platformCode="1234",tenantCode="10000";
byte [] bytes=SAASTokenManager.generateBytesToken(publicKey, platformCode, tenantCode);
System.out.println("RSA bites 加密:"+new String(bytes));
System.out.println("RSA bites 解密:"+new String(RSAUtils.decryptByPrivateKey(bytes, privateKey)));
// 将bytes转换为String
String bytesStr = RSAUtils.bytesToString(bytes);
String[] strArr = bytesStr.split(" ");
int len = strArr.length;
// 转回bytes
byte[] clone = new byte[len];
for (int i = 0; i < len; i++) {
clone[i] = Byte.parseByte(strArr[i]);
}
System.out.println("RSA bites 解密:"+new String(RSAUtils.decryptByPrivateKey(clone, privateKey)));
}
解决方法:
/**
* 将加密后的字节数组转换为对象
*
* @MethodName: bytesToString
* @Description:
* @param encrytpByte
* @return
* @throws
*/
public static String bytesToString(byte[] encrytpByte) {
String result = "";
for (Byte bytes : encrytpByte) {
result += bytes.toString() + " ";
}
return result;
}
/**
* 公钥加密
*
* @MethodName: encrypt
* @Description:
* @param publicKey
* @param obj
* @return
* @throws
*/
public static byte[] encrypt(RSAPublicKey publicKey, byte[] obj) {
if (publicKey != null) {
try {
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(obj);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* 私钥解密
*
* @MethodName: decrypt
* @Description:
* @param privateKey
* @param obj
* @return
* @throws
*/
public static byte[] decrypt(RSAPrivateKey privateKey, byte[] obj) {
if (privateKey != null) {
try {
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(obj);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}测试方法:
@Test
public void testRSA() throws Exception{
String publicKey=EhcacheUtil.get(StaticProperty.WMS_OPEN_API_RSA_PUBLIC_KEY).toString();
String privateKey=EhcacheUtil.get(StaticProperty.WMS_OPEN_API_RSA_PRIVATE_KEY).toString();
String platformCode="1234",tenantCode="10000";
byte [] bytes=SAASTokenManager.generateBytesToken(publicKey, platformCode, tenantCode);
System.out.println("RSA bites 加密:"+new String(bytes));
System.out.println("RSA bites 解密:"+new String(RSAUtils.decryptByPrivateKey(bytes, privateKey)));
// 将bytes转换为String
String bytesStr = RSAUtils.bytesToString(bytes);
String[] strArr = bytesStr.split(" ");
int len = strArr.length;
// 转回bytes
byte[] clone = new byte[len];
for (int i = 0; i < len; i++) {
clone[i] = Byte.parseByte(strArr[i]);
}
System.out.println("RSA bites 解密:"+new String(RSAUtils.decryptByPrivateKey(clone, privateKey)));
}
相关文章推荐
- 关于解决RSA非对称加密时,将密文存入数据库取出后,再对密文解密的时候抛出javax.crypto.BadPaddingException: Data must start with zero
- RSA解密时javax.crypto.BadPaddingException: Data must start with zero
- 利用java api 实现 RSA算法 解决(javax.crypto.BadPaddingException: Data must start with zero)
- A context path must either be an empty string or start with a '/' and do not end with a '/' 问题解决方法
- “AndroidStudio2.1.3\sdk\build-tools\24.0.0\aapt.exe''finished with non-zero exit value 1问题解决方法
- Data must start with zero
- NIOSII Slave with readdatavalid signal must support at least 1 pending read 解决方法。
- SRA解密报错:Data must start with zero
- 关于MultipleActiveResultSets属性导致的There is already an open DataReader associated with this Command which must be closed first的解决方法
- 用va_start() va_end()函数解决不定参数问题(即,fun(...)实现方法)
- Teradata Expression 12 在Windows 2003上Connection Reset 问题的解决方法
- no data exchange control with ID的解决方法
- ubunt 系统上安装 deb 出错解决方法 version number does not start with digit
- Path does not start with a "/" character 问题的解决(2)
- 遇到动态调用Parameters .AddWithValue()参数类型问题的解决方法
- startActivityForResult方法解决Activity之间数据的保存问题
- 问题解决: Attribute value request.getParameter("id") is quoted with " which must be escaped when used wi
- org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [org.zero.space.entity.TblJd#38]解决方法
- Can not issue data manipulation statements with executeQuery().问题的解决
- 解决WebService[WebMethod]方法返回一个SqlDataReader出错问题