Skip to content

Instantly share code, notes, and snippets.

@yakivmospan
Created December 6, 2017 18:08
Show Gist options
  • Save yakivmospan/4aedbf9bd31bde8f45e7341e9512f00c to your computer and use it in GitHub Desktop.
Save yakivmospan/4aedbf9bd31bde8f45e7341e9512f00c to your computer and use it in GitHub Desktop.
fun encrypt(data: String, key: Key?, useInitializationVector: Boolean = false): String {
cipher.init(Cipher.ENCRYPT_MODE, key)
var result = ""
if (useInitializationVector) {
val iv = cipher.iv
val ivString = Base64.encodeToString(iv, Base64.DEFAULT)
result = ivString + IV_SEPARATOR
}
val bytes = cipher.doFinal(data.toByteArray())
result += Base64.encodeToString(bytes, Base64.DEFAULT)
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment