Guest User

Untitled

a guest
Jun 14th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. private static byte [] encrypt(String plaintext) throws Exception {
  2.  
  3. KeyStore keyStore = getKeyStore();
  4. Certificate[] certs = keyStore.getCertificateChain("alias");
  5. Cipher cipher = Cipher.getInstance("RSA","BC");
  6. cipher.init(Cipher.ENCRYPT_MODE, certs[0].getPublicKey());
  7. return cipher.doFinal(plaintext.getBytes());
  8. }
  9.  
  10. KeyStore keyStore = getKeyStore();
  11. PrivateKey privateKey = (PrivateKey) keyStore.getKey("alias",
  12. "password".toCharArray());
  13. Cipher cipher = Cipher.getInstance("RSA","BC");
  14. cipher.init(Cipher.DECRYPT_MODE, privateKey);
  15. byte[] cipherbyte=cipher.doFinal(ciphertext);
  16. return new String(cipherbyte);
  17. }
  18.  
  19. Caused by: javax.crypto.BadPaddingException: Given final block not properly padded
  20. at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:966)
  21. at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:824)
  22. at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:436)
  23. at javax.crypto.Cipher.doFinal(Cipher.java:2165)
  24. at
Add Comment
Please, Sign In to add comment