Skip to content

Commit 0bf7244

Browse files
committed
fix(android): file encryption
1 parent 108c394 commit 0bf7244

File tree

3 files changed

+48
-44
lines changed

3 files changed

+48
-44
lines changed

packages/native_crypto_android/android/src/main/kotlin/fr/pointcheval/native_crypto_android/NativeCrypto.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class NativeCrypto(private val context: Context) : NativeCryptoAPI {
111111
): Boolean {
112112
// For now, only AES is supported
113113
val aes = AES()
114-
val params = FileParameters(context, plainTextPath, cipherTextPath)
114+
val params = FileParameters(context, cipherTextPath, plainTextPath)
115115

116116
return aes.decryptFile(params, key)
117117
}

packages/native_crypto_android/android/src/main/kotlin/fr/pointcheval/native_crypto_android/ciphers/AES.kt

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package fr.pointcheval.native_crypto_android.ciphers
22

33
import fr.pointcheval.native_crypto_android.interfaces.Cipher
44
import fr.pointcheval.native_crypto_android.utils.FileParameters
5+
import java.io.BufferedInputStream
6+
import java.io.BufferedOutputStream
7+
import javax.crypto.CipherInputStream
58
import javax.crypto.CipherOutputStream
69
import javax.crypto.spec.GCMParameterSpec
710
import javax.crypto.spec.SecretKeySpec
@@ -60,28 +63,32 @@ class AES : Cipher {
6063
cipherInstance!!.init(javax.crypto.Cipher.ENCRYPT_MODE, sk)
6164
}
6265

63-
var len: Int?
64-
val buffer = ByteArray(8192)
65-
val inputFile = fileParameters.getFileInputStream()
66-
val outputFile = fileParameters.getFileOutputStream()
67-
val iv: ByteArray? = cipherInstance!!.iv
68-
69-
outputFile?.write(iv)
70-
outputFile?.flush()
71-
72-
val encryptedStream = CipherOutputStream(outputFile!!, cipherInstance)
73-
while(true) {
74-
len = inputFile?.read(buffer)
75-
if (len != null && len > 0) {
76-
encryptedStream.write(buffer,0,len)
77-
} else {
78-
break
79-
}
80-
}
81-
encryptedStream.flush()
82-
encryptedStream.close()
83-
inputFile?.close()
84-
outputFile.close()
66+
val input = BufferedInputStream(fileParameters.getFileInputStream())
67+
68+
var outputBuffered = BufferedOutputStream(fileParameters.getFileOutputStream(false))
69+
val iv: ByteArray = cipherInstance!!.iv
70+
71+
// Prepend the IV to the cipherText file
72+
outputBuffered.write(iv)
73+
outputBuffered.flush()
74+
outputBuffered.close()
75+
76+
// Reopen the file and append the cipherText
77+
outputBuffered = BufferedOutputStream(fileParameters.getFileOutputStream(true))
78+
val output = CipherOutputStream(outputBuffered, cipherInstance)
79+
80+
var i: Int
81+
do {
82+
i = input.read()
83+
if (i != -1) output.write(i)
84+
} while (i != -1)
85+
86+
output.flush()
87+
output.close()
88+
89+
input.close()
90+
outputBuffered.close()
91+
8592

8693
return fileParameters.outputExists()
8794
}
@@ -110,9 +117,7 @@ class AES : Cipher {
110117
val inputFile = fileParameters.getFileInputStream() ?: throw Exception("Error while reading IV")
111118

112119
// Read the first 12 bytes from the file
113-
for (i in 0 until 12) {
114-
iv[i] = inputFile.read().toByte()
115-
}
120+
inputFile.read(iv)
116121

117122
// Initialize secret key spec
118123
val sk = SecretKeySpec(key, "AES")
@@ -125,21 +130,19 @@ class AES : Cipher {
125130

126131
cipherInstance!!.init(javax.crypto.Cipher.DECRYPT_MODE, sk, gcmParameterSpec)
127132

128-
var len: Int?
129-
val buffer = ByteArray(8192)
130-
val outputFile = fileParameters.getFileOutputStream()
131-
val decryptedStream = CipherOutputStream(outputFile!!, cipherInstance)
132-
while (true) {
133-
len = inputFile.read(buffer)
134-
if(len > 0){
135-
decryptedStream.write(buffer,0, len)
136-
} else {
137-
break
138-
}
139-
}
140-
decryptedStream.flush()
141-
decryptedStream.close()
142-
inputFile.close()
133+
val input = CipherInputStream(BufferedInputStream(inputFile), cipherInstance)
134+
val output = BufferedOutputStream(fileParameters.getFileOutputStream(false))
135+
136+
var i: Int
137+
do {
138+
i = input.read()
139+
if (i != -1) output.write(i)
140+
} while (i != -1)
141+
142+
output.flush()
143+
output.close()
144+
145+
input.close()
143146

144147
return fileParameters.outputExists()
145148
}

packages/native_crypto_android/android/src/main/kotlin/fr/pointcheval/native_crypto_android/utils/FileParameters.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,15 @@ class FileParameters(ctx: Context, input: String, output: String) {
4343
}
4444
}
4545

46-
fun getFileOutputStream(): OutputStream? {
46+
fun getFileOutputStream(append: Boolean): OutputStream? {
4747
val path = outputPath
4848
return try{
49-
FileOutputStream(path)
49+
FileOutputStream(path, append)
5050
} catch(e: IOException){
5151
val documentFile: DocumentFile = this.getDocumentFileByPath(path)
5252
val documentUri = documentFile.uri
53-
context.contentResolver.openOutputStream(documentUri)
53+
val mode = if(append) "wa" else "w"
54+
context.contentResolver.openOutputStream(documentUri, mode)
5455
}
5556
}
5657

0 commit comments

Comments
 (0)