Skip to content

Instantly share code, notes, and snippets.

@yakivmospan
Created December 6, 2017 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yakivmospan/12959c855b68be5dc7f0c40a5d55b0a9 to your computer and use it in GitHub Desktop.
Save yakivmospan/12959c855b68be5dc7f0c40a5d55b0a9 to your computer and use it in GitHub Desktop.
fun decrypt(data: String, key: Key?, useInitializationVector: Boolean = false): String {
var encodedString: String
if (useInitializationVector) {
val split = data.split(IV_SEPARATOR.toRegex())
if (split.size != 2) throw IllegalArgumentException("Passed data is incorrect. There was no IV specified with it.")
val ivString = split[0]
encodedString = split[1]
val ivSpec = IvParameterSpec(Base64.decode(ivString, Base64.DEFAULT))
cipher.init(Cipher.DECRYPT_MODE, key, ivSpec)
} else {
encodedString = data
cipher.init(Cipher.DECRYPT_MODE, key)
}
val encryptedData = Base64.decode(encodedString, Base64.DEFAULT)
val decodedData = cipher.doFinal(encryptedData)
return String(decodedData)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment