-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion03.kt
35 lines (27 loc) · 964 Bytes
/
question03.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.io.FileInputStream
import java.io.FileOutputStream
import javax.crypto.Cipher
fun main(args: Array<String>){
decryptFile(args[0], args[1], args[2])
}
fun decryptFile(encryptedFile: String, decryptedFile: String, key: String, bufferSize: Int = 1024) {
try {
val key = generateSecretKey(key)
val cipher = Cipher.getInstance("AES")
cipher.init(Cipher.DECRYPT_MODE, key)
val input = FileInputStream(encryptedFile)
val output = FileOutputStream(decryptedFile)
val buffer = ByteArray(bufferSize)
var size : Int
while (input.read(buffer).also { size = it } != -1) {
val decryptedText = cipher.update(buffer, 0, size)
output.write(decryptedText)
}
val finalTextDecrypted = cipher.doFinal()
output.write(finalTextDecrypted)
input.close()
output.close()
} catch (e: Exception) {
println(e.message)
}
}