Created
December 6, 2017 18:08
-
-
Save yakivmospan/12959c855b68be5dc7f0c40a5d55b0a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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