Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static byte[] encryptWithPublicKey(String text) throws Exception {
- String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCf61/PMOVIIt2Z0xQ0NBe1rZxd8lrOKxzAyQWSpElV39TY279w1NRBMzbVvyiU5XfFjbA07JtcUkNxu1uyysKioB6yQHIXIDXAorvuinrpNMTYQDl5cyZTDNA9F3UJyhfhFEPdSWXY53ObXW61lICCmZlZRHFGPLfJQjjEzu7dvwIDAQAB";
- PublicKey apiPublicKey= getRSAPublicKeyFromString(publicKey);
- Cipher rsaCipher = Cipher.getInstance("RSA/None/PKCS1Padding", "BC");
- rsaCipher.init(Cipher.ENCRYPT_MODE, apiPublicKey);
- return rsaCipher.doFinal(text.getBytes("ISO-8859-1"));
- }
- private static PublicKey getRSAPublicKeyFromString(String publicKey) throws Exception{
- KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
- byte[] publicKeyBytes = Base64.decode(publicKey.getBytes("ISO-8859-1"), Base64.DEFAULT);
- X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKeyBytes);
- return keyFactory.generatePublic(x509KeySpec);
- }
- Base64.encodeToString(EncryptionSHA.encryptWithPublicKey("qwerty1234"), Base64.NO_WRAP)
Add Comment
Please, Sign In to add comment