Kvarz

Untitled

Apr 13th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 KB | None | 0 0
  1.   public static byte[] encryptWithPublicKey(String text) throws Exception {
  2.         String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCf61/PMOVIIt2Z0xQ0NBe1rZxd8lrOKxzAyQWSpElV39TY279w1NRBMzbVvyiU5XfFjbA07JtcUkNxu1uyysKioB6yQHIXIDXAorvuinrpNMTYQDl5cyZTDNA9F3UJyhfhFEPdSWXY53ObXW61lICCmZlZRHFGPLfJQjjEzu7dvwIDAQAB";
  3.         PublicKey apiPublicKey= getRSAPublicKeyFromString(publicKey);
  4.         Cipher rsaCipher = Cipher.getInstance("RSA/None/PKCS1Padding", "BC");
  5.         rsaCipher.init(Cipher.ENCRYPT_MODE, apiPublicKey);
  6.         return rsaCipher.doFinal(text.getBytes("ISO-8859-1"));
  7.     }
  8.  
  9.     private static PublicKey getRSAPublicKeyFromString(String publicKey) throws Exception{
  10.         KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
  11.         byte[] publicKeyBytes = Base64.decode(publicKey.getBytes("ISO-8859-1"), Base64.DEFAULT);
  12.         X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKeyBytes);
  13.         return keyFactory.generatePublic(x509KeySpec);
  14.     }
  15.  
  16.  Base64.encodeToString(EncryptionSHA.encryptWithPublicKey("qwerty1234"), Base64.NO_WRAP)
Add Comment
Please, Sign In to add comment