How to encrypt or decrypt a file in Kotlin
How to encrypt or decrypt a file in Kotlin.
Here's a step-by-step tutorial on how to encrypt or decrypt a file in Kotlin.
Step 1: Setting Up the Project
- Create a new Kotlin project in your preferred IDE.
- Add the following dependency to your project's
build.gradlefile:
implementation 'org.bouncycastle:bcprov-jdk15on:1.68'
This will enable the use of the Bouncy Castle library for encryption and decryption.
Step 2: Encrypting a File
- Create a function named
encryptFileto perform the encryption:
import org.bouncycastle.jce.provider.BouncyCastleProvider
import java.io.File
import java.io.FileOutputStream
import java.security.Security
import javax.crypto.Cipher
import javax.crypto.CipherOutputStream
import javax.crypto.SecretKey
import javax.crypto.SecretKeyFactory
import javax.crypto.spec.PBEKeySpec
import javax.crypto.spec.PBEParameterSpec
fun encryptFile(inputFile: File, outputFile: File, password: String) {
Security.addProvider(BouncyCastleProvider())
val salt = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
val iterationCount = 1000
val keySpec = PBEKeySpec(password.toCharArray())
val secretKeyFactory = SecretKeyFactory.getInstance("PBEWithSHA256And256BitAES-CBC-BC")
val secretKey: SecretKey = secretKeyFactory.generateSecret(keySpec)
val cipher = Cipher.getInstance("PBEWithSHA256And256BitAES-CBC-BC")
val pbeParamSpec = PBEParameterSpec(salt, iterationCount)
cipher.init(Cipher.ENCRYPT_MODE, secretKey, pbeParamSpec)
val inputStream = inputFile.inputStream()
val outputStream = CipherOutputStream(FileOutputStream(outputFile), cipher)
inputStream.copyTo(outputStream)
outputStream.flush()
outputStream.close()
inputStream.close()
}
- In the
mainfunction, call theencryptFilefunction with the appropriate file paths and password:
fun main() {
val inputFile = File("path/to/input/file.txt")
val outputFile = File("path/to/output/encrypted_file.txt")
val password = "mysecretpassword"
encryptFile(inputFile, outputFile, password)
}
Step 3: Decrypting a File
- Create a function named
decryptFileto perform the decryption:
fun decryptFile(inputFile: File, outputFile: File, password: String) {
Security.addProvider(BouncyCastleProvider())
val salt = byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08)
val iterationCount = 1000
val keySpec = PBEKeySpec(password.toCharArray())
val secretKeyFactory = SecretKeyFactory.getInstance("PBEWithSHA256And256BitAES-CBC-BC")
val secretKey: SecretKey = secretKeyFactory.generateSecret(keySpec)
val cipher = Cipher.getInstance("PBEWithSHA256And256BitAES-CBC-BC")
val pbeParamSpec = PBEParameterSpec(salt, iterationCount)
cipher.init(Cipher.DECRYPT_MODE, secretKey, pbeParamSpec)
val inputStream = CipherInputStream(inputFile.inputStream(), cipher)
val outputStream = FileOutputStream(outputFile)
inputStream.copyTo(outputStream)
outputStream.flush()
outputStream.close()
inputStream.close()
}
- In the
mainfunction, call thedecryptFilefunction with the appropriate file paths and password:
fun main() {
val inputFile = File("path/to/input/encrypted_file.txt")
val outputFile = File("path/to/output/decrypted_file.txt")
val password = "mysecretpassword"
decryptFile(inputFile, outputFile, password)
}
That's it! You now have the necessary code to encrypt and decrypt a file in Kotlin using the Bouncy Castle library. Remember to replace the file paths with the actual paths to your input and output files, and choose a strong and secure password for encryption.
This tutorial used the PBEWithSHA256And256BitAES-CBC-BC algorithm for encryption and decryption. Feel free to explore other algorithms supported by Bouncy Castle based on your specific requirements.