-1
私は暗号化された文字列を持っています。暗号化はJavaコードを使用して行われます。次のJavaコードを使用して暗号化された文字列を復号化します。秘密鍵を使用したPython復号
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を使用する必要があります。私は秘密鍵を持っています。私は次のコード
key = load_pem_private_key(keydata, password=None, backend=default_backend())
を使用して暗号化パッケージを使用する場合にはValueError: Could not unserialize key data.
をスロー誰も私がここで行方不明です何を助けることができますか?
テキストブックRSAを使用しないでください。パディングを使用しないか、またはパディング不良を使用することは非常に安全です。現在、デフォルトのPKCS#1 v1.5パディングの代わりにOAEPを使用する必要があります。だからおそらく 'Cipher.getInstance(" RSA/ECB/OAEPWithSHA-256AndMGF1Padding ");' –