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
@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) | |
} | |
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
@Throws(Exception::class) | |
fun generateSecretKey(): SecretKey? { | |
val secureRandom = SecureRandom() | |
val keyGenerator = KeyGenerator.getInstance("AES") | |
//generate a key with secure random | |
keyGenerator?.init(128, secureRandom) | |
return keyGenerator?.generateKey() | |
} |
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 saveSecretKey(sharedPref: SharedPreferences, secretKey: SecretKey): String { | |
val encodedKey = Base64.encodeToString(secretKey.encoded, Base64.NO_WRAP) | |
sharedPref.edit().putString(AppConstants.secretKeyPref, encodedKey).apply() | |
return encodedKey | |
} |
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
@Throws(Exception::class) | |
fun readFile(filePath: String): ByteArray { | |
val file = File(filePath) | |
val fileContents = file.readBytes() | |
val inputBuffer = BufferedInputStream( | |
FileInputStream(file) | |
) | |
inputBuffer.read(fileContents) | |
inputBuffer.close() |
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
@Throws(Exception::class) | |
fun saveFile(fileData: ByteArray, path: String) { | |
val file = File(path) | |
val bos = BufferedOutputStream(FileOutputStream(file, false)) | |
bos.write(fileData) | |
bos.flush() | |
bos.close() | |
} |
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 encryptDownloadedFile(filePath: String) { | |
val fileData = readFile(filePath) | |
val secretKey = getSecretKey(sharedPref) | |
val encodedData = encode(secretKey, fileData) | |
saveFile(encodedData, filePath) | |
} |
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 encryptDownloadedFile() { | |
try { | |
val filePath = filesDir.absolutePath + "/filename" | |
val fileData = readFile(filePath) | |
//get secret key | |
val secretKey = getSecretKey(sharedPref) | |
//encrypt file | |
val encodedData = encrypt(secretKey, fileData) |
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 getSecretKey(sharedPref: SharedPreferences): SecretKey { | |
val key = sharedPref.getString(AppConstants.secretKeyPref, null) | |
if (key == null) { | |
//generate secure random | |
val secretKey = generateSecreKey() | |
saveSecretKey(sharedPref, secretKey!!) | |
return secretKey | |
} |
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
@Throws(Exception::class) | |
fun decrypt(yourKey: SecretKey, fileData: ByteArray): ByteArray { | |
val decrypted: ByteArray | |
val cipher = Cipher.getInstance("AES", "BC") | |
cipher.init(Cipher.DECRYPT_MODE, yourKey, IvParameterSpec(ByteArray(cipher.blockSize))) | |
decrypted = cipher.doFinal(fileData) | |
return decrypted | |
} |
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
private fun decryptEncryptedFile(): ByteArray { | |
val filePath = filesDir.absolutePath + "/filename" | |
val fileData = readFile(filePath) | |
val secretKey = getSecretKey(sharedPref) | |
return decode(secretKey, fileData) | |
} |
OlderNewer