-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion02.kt
44 lines (36 loc) · 1.26 KB
/
question02.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
36
37
38
39
40
41
42
43
44
import java.io.FileInputStream
import java.io.FileOutputStream
import java.security.Key
import java.security.MessageDigest
import javax.crypto.Cipher
import javax.crypto.spec.SecretKeySpec
fun main(args: Array<String>){
encryptFile(args[0], args[1], args[3])
}
fun encryptFile(sourceFile: String, encryptedFile: String, key: String, bufferSize: Int = 1024){
try {
val key = generateSecretKey(key)
val cipher = Cipher.getInstance("AES")
cipher.init(Cipher.ENCRYPT_MODE, key)
val input = FileInputStream(sourceFile)
val output = FileOutputStream(encryptedFile)
val buffer = ByteArray(bufferSize)
var size : Int
while (input.read(buffer).also { size = it } != -1) {
val encryptedText = cipher.update(buffer, 0, size)
output.write(encryptedText)
}
val finalTextEncrypted = cipher.doFinal()
output.write(finalTextEncrypted)
input.close()
output.close()
} catch (e: Exception) {
println(e.message)
}
}
fun generateSecretKey(key: String): Key {
val keyBytes = key.toByteArray(charset("UTF-8"))
val sha256 = MessageDigest.getInstance("SHA-256")
val hashKey = sha256.digest(keyBytes)
return SecretKeySpec(hashKey, "AES")
}