Last active
September 7, 2022 07:25
-
-
Save kevinjam/6ab94ba2345fc56042aa3d9082d5922a to your computer and use it in GitHub Desktop.
RSA Encrypt in PHP and Decrypt in Java or Kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DecryptKey { | |
//before you proceed, you need to ensure your public key is PKCS8 since that is what can be read natively in java. | |
//If your key begins with-----BEGIN CERTIFICATE-----, | |
// the it is ssleay and you need to convert it using the openssl command below | |
//openssl pkcs8 -topk8 -inform pem -in public.key -outform pem -nocrypt -out pkcs8-public-key.pem | |
//TODO You can you phpsecLib lib but it doesn't support some Library | |
//opnssl work fine | |
final private static String RSA_PRIVATE_KEY = | |
-----BEGIN PUBLIC KEY----- | |
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzTrKt2Y7HqQpt8w379Sl | |
79mjXGJ+CF+ridXpnAKoKyN8LH1NkI08UyWEHKbtCBl4iCsy8ayr4RAkWDKzeb8p | |
ZEmdOg3cHKyFT6NdUEDc0FGpgKWY6GycJUrY+1S5O256M/NRSw02mUpyYIpGjq1z | |
2zfu4GWIPg24hyI4YrAVI90mknIyXduerLH30XdwnKTevwhkQ4cP+xeYLVDduodc | |
pcVaSXcwBvIbwVWS7szTxNGX0qyDlmJoK6YtTZ4FJp6BK6iyD+Fpnt9Fld8d62o2 | |
WjAtjpc8w81kIwwykojCDv9cb2qhwryS63qlcPhgpxcpDXINBHxcONHtHSWKEb0t | |
2wIDAQAB | |
-----END PUBLIC KEY-----; | |
public static byte[] decrypt(String key) throws Exception { | |
String privKeyPEM = RSA_PRIVATE_KEY.replace("-----BEGIN PRIVATE KEY-----\n", ""); | |
privKeyPEM = privKeyPEM.replace("-----END PRIVATE KEY-----", ""); | |
byte [] encoded = Base64.decode(privKeyPEM, Base64.DEFAULT); | |
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded); | |
KeyFactory kf = KeyFactory.getInstance("RSA"); | |
PrivateKey privKey = kf.generatePrivate(keySpec); | |
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC"); | |
cipher.init(Cipher.DECRYPT_MODE, privKey); | |
byte[] decodedStr = Base64.decode(key, Base64.DEFAULT); | |
byte[] plainText = cipher.doFinal(decodedStr); | |
return plainText; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open class DecryptKey { | |
//before you proceed, you need to ensure your public key is PKCS8 since that is what can be read natively in java. | |
//If your key begins with-----BEGIN CERTIFICATE-----, | |
// the it is ssleay and you need to convert it using the openssl command below | |
//openssl pkcs8 -topk8 -inform pem -in public.key -outform pem -nocrypt -out pkcs8-public-key.pem | |
//TODO You can you phpsecLib lib but it doesn't support some Library | |
//opnssl work fine | |
private val RSA_PRIVATE_KEY = "-----BEGIN PRIVATE KEY-----\n" + | |
"MI...\n" + | |
"2...\n" + | |
"HPt...\n" + | |
"v3...\n" + | |
"-----END PRIVATE KEY-----" | |
@Throws(Exception::class) | |
fun decrypt(key: String): ByteArray { | |
var privKeyPEM = RSA_PRIVATE_KEY.replace("-----BEGIN PRIVATE KEY-----\n", "") | |
privKeyPEM = privKeyPEM.replace("-----END PRIVATE KEY-----", "") | |
val encoded = Base64.decode(privKeyPEM, Base64.DEFAULT) | |
val keySpec = PKCS8EncodedKeySpec(encoded) | |
val kf = KeyFactory.getInstance("RSA") | |
val privKey = kf.generatePrivate(keySpec) | |
val cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC") | |
cipher.init(Cipher.DECRYPT_MODE, privKey) | |
val decodedStr = Base64.decode(key, Base64.DEFAULT) | |
return cipher.doFinal(decodedStr) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function encrypt($string) | |
{ | |
$key = | |
"-----BEGIN PUBLIC KEY----- | |
MIGfM.... | |
gUn.... | |
7eMl.... | |
yXd.... | |
-----END PUBLIC KEY-----"; | |
openssl_public_encrypt($string, $crypted, $key); | |
return base64_encode($crypted); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public String encryptToString(byte[] data) { | |
Security.addProvider(new BouncyCastleProvider()); | |
byte[] cipherData = null; | |
try { | |
X509Certificate cert = this.getPublicKey(public_key_alias, keystore_pass, keystore_location); | |
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC"); | |
cipher.init(Cipher.ENCRYPT_MODE, cert.getPublicKey()); | |
cipherData = cipher.doFinal(data); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
return Base64.encodeBase64String(cipherData); | |
} | |
04 July 20 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment