'Porting python PKCS1_v1_5 encryption code to java but java code not work
I have a backend,which receives codes and returns true or false.
I send it codes using my python encryption,it returns true(the result varible):
from Cryptodome.Cipher import PKCS1_v1_5
from Cryptodome.PublicKey import RSA
import base64
prsa = RSA.importKey('''-----BEGIN RSA PUBLIC KEY-----
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-----END RSA PUBLIC KEY-----''')
pcipher = PKCS1_v1_5.new(prsa)
result=base64.b64encode(pcipher.encrypt(bytes))
But my java encryption always return false:
public static String encrypt(byte[] bytes, String publicKeyFilePath)
throws Exception {
byte[] keyBytes = Files.readAllBytes(Paths.get(publicKeyFilePath));
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
Key key = keyFactory.generatePublic(keySpec);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");
cipher.init(1, key);
byte[] result = cipher.doFinal(bytes);
return java.util.Base64.getEncoder().encodeToString(result);
}
Both java and python codes can be well decrypted by my java decryption codes(I have private key).
I have tryed different kinds of cipher,such as:
Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding");
And trying to set provider:
Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding",new org.bouncycastle.jce.provider.BouncyCastleProvider());
But all return false.I am confused about the "PKCS1_v1_5",for don't know which kind of padding it is,and what is the right code porting to java?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
