I'm trying to implement RSA Encryption in both Java and PHP, but I can't seem to get PHP to output the same Encrypted Result with Java.
Java Code
public static String sign(byte[] data,String privateKey)throws Exception{
byte[] keyBytes = Base64.decodeBase64(privateKey);
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes);
//KEY_ALGORTHM="RSA";
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORTHM);
PrivateKey privateKey2 = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
//SIGNATURE_ALGORITHM="SHA1WithRSA"
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initSign(privateKey2);
signature.update(data);
return Base64.encodeBase64String(signature.sign());
}
PHP Code
public function sign( string $data,string $privateKey){
openssl_private_encrypt($data,$encrypted,$privateKey);
return base64_encode($encrypted);
}
Can someone help me to translate it to PHP Code ?
Any help would be greatly appreciated o.O
$paddingarg? It defaults tooOPENSSL_PKCS1_PADDINGbut you may wantOPENSSL_PKCS1_OAEP_PADDINGinstead