Skip to content

Instantly share code, notes, and snippets.

@Oshan-Madushanka
Oshan-Madushanka / Utils.kt
Last active June 26, 2019 08:43
Encrypt a file
@Throws(Exception::class)
fun encrypt(yourKey: SecretKey, fileData: ByteArray): ByteArray {
val data = yourKey.getEncoded()
val skeySpec = SecretKeySpec(data, 0, data.size, "AES")
val cipher = Cipher.getInstance("AES", "BC")
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, IvParameterSpec(ByteArray(cipher.getBlockSize())))
return cipher.doFinal(fileData)
}