【发布时间】:2016-08-24 19:31:13
【问题描述】:
我有一个加密字符串。加密是使用 java 代码完成的。我使用以下 java 代码解密加密字符串
InputStream fileInputStream = getClass().getResourceAsStream(
"/private.txt");
byte[] bytes = IOUtils.toByteArray(fileInputStream);
private String decrypt(String inputString, byte[] keyBytes) {
String resultStr = null;
PrivateKey privateKey = null;
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(keyBytes);
privateKey = keyFactory.generatePrivate(privateKeySpec);
} catch (Exception e) {
System.out.println("Exception privateKey::::::::::::::::: "
+ e.getMessage());
e.printStackTrace();
}
byte[] decodedBytes = null;
try {
Cipher c = Cipher.getInstance("RSA/ECB/NoPadding");
c.init(Cipher.DECRYPT_MODE, privateKey);
decodedBytes = c.doFinal(Base64.decodeBase64(inputString));
} catch (Exception e) {
System.out
.println("Exception while using the cypher::::::::::::::::: "
+ e.getMessage());
e.printStackTrace();
}
if (decodedBytes != null) {
resultStr = new String(decodedBytes);
resultStr = resultStr.split("MNSadm")[0];
// System.out.println("resultStr:::" + resultStr + ":::::");
// resultStr = resultStr.replace(salt, "");
}
return resultStr;
}
现在我必须使用 Python 来解密加密的字符串。我有私钥。当我使用以下代码使用 Cryptography 包时
key = load_pem_private_key(keydata, password=None, backend=default_backend())
它抛出ValueError: Could not unserialize key data.
谁能帮助我在这里缺少什么?
【问题讨论】:
-
永远不要使用教科书 RSA。不使用填充或错误的填充是非常不安全的。现在,您应该使用 OAEP 而不是默认的 PKCS#1 v1.5 填充。所以你可能应该使用
Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
标签: python cryptography