Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. private const val TRANSFORMATION = "RSA/ECB/PKCS1Padding"
  2.  
  3. internal fun rsaEncrypt(data: String, keyPair: KeyPair): String {
  4. val cipher = Cipher.getInstance(TRANSFORMATION)
  5. cipher.init(Cipher.ENCRYPT_MODE, keyPair.private)
  6. val bytes = cipher.doFinal(data.toByteArray(Charsets.UTF_8))
  7. return Base64.encodeToString(bytes, 0)
  8. }
  9.  
  10. internal fun rsaDecrypt(data: String, keyPair: KeyPair): String {
  11. val cipher = Cipher.getInstance(TRANSFORMATION)
  12. cipher.init(Cipher.DECRYPT_MODE, keyPair.public)
  13. val bytes = Base64.decode(data, 0)
  14. val decrypted = cipher.doFinal(bytes)
  15. return String(decrypted, Charsets.UTF_8)
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement