66package com.mirrorfly_rn
77
88
9+ import android.os.Build
10+ import android.util.Base64
911import androidx.annotation.Keep
1012import java.io.UnsupportedEncodingException
1113import java.nio.charset.StandardCharsets
@@ -130,6 +132,91 @@ class FileCryptLib {
130132 return _cx // Return encrypted/decrypted cipher instance
131133 }
132134
135+ /* *
136+ * @param inputByteArray Text to be encrypted or decrypted
137+ * @param encryptionKey Encryption key to used for encryption / decryption
138+ * @param mode specify the mode encryption / decryption
139+ * @param initVector Initialization vector
140+ * @return encrypted or decrypted string based on the mode
141+ * @throws UnsupportedEncodingException
142+ * @throws InvalidKeyException
143+ * @throws InvalidAlgorithmParameterException
144+ * @throws IllegalBlockSizeException
145+ * @throws BadPaddingException
146+ */
147+ @Throws(
148+ UnsupportedEncodingException ::class ,
149+ InvalidKeyException ::class ,
150+ InvalidAlgorithmParameterException ::class ,
151+ IllegalBlockSizeException ::class ,
152+ BadPaddingException ::class
153+ )
154+ private fun encryptDecryptFile (
155+ inputByteArray : ByteArray ,
156+ encryptionKey : String ,
157+ mode : EncryptMode ,
158+ initVector : String
159+ ): ByteArray {
160+
161+ var outPutByteArray = inputByteArray // Output byte array
162+
163+ /* *
164+ * _encryptionKey = md5(_encryptionKey);
165+ * length of the key provided
166+ */
167+ var len = encryptionKey.toByteArray(StandardCharsets .UTF_8 ).size
168+ if (encryptionKey.toByteArray(StandardCharsets .UTF_8 ).size > _key .size)
169+ len = _key .size
170+
171+ var ivlen = initVector.toByteArray(StandardCharsets .UTF_8 ).size
172+ if (initVector.toByteArray(StandardCharsets .UTF_8 ).size > _iv .size)
173+ ivlen = _iv .size
174+
175+ System .arraycopy(encryptionKey.toByteArray(StandardCharsets .UTF_8 ), 0 , _key , 0 , len)
176+ System .arraycopy(initVector.toByteArray(StandardCharsets .UTF_8 ), 0 , _iv , 0 , ivlen)
177+
178+ /* *
179+ * Create a new SecretKeySpec
180+ */
181+ val keySpec = SecretKeySpec (_key , " AES" )
182+
183+ /* *
184+ * Create a new IvParameterSpec instance with the bytes from the specified buffer iv
185+ * used as initialization vector.
186+ */
187+ val ivSpec = IvParameterSpec (_iv )
188+
189+ if (mode == EncryptMode .ENCRYPT ) { // Encryption
190+ /* *
191+ * Read
192+ * https://android-developers.blogspot.com/2013/08/some-securerandom-thoughts.html
193+ * for more info.
194+ */
195+ _cx .init (Cipher .ENCRYPT_MODE , keySpec, ivSpec) // Initialize this cipher instance.
196+ val results = _cx .doFinal(inputByteArray)
197+
198+ val outputBytes = if (Build .VERSION .SDK_INT >= 26 ) {
199+ java.util.Base64 .getEncoder().encode(results)
200+ } else Base64 .encode(results, 0 )
201+
202+ outPutByteArray = outputBytes // Multi-part, transformation and (encryption) Ciphertext
203+ }
204+
205+ if (mode == EncryptMode .DECRYPT ) { // Decryption
206+ _cx .init (Cipher .DECRYPT_MODE , keySpec, ivSpec) // Initialize this cipher instance
207+
208+ val outputBytes = if (Build .VERSION .SDK_INT >= 26 ) {
209+ java.util.Base64 .getDecoder().decode(inputByteArray)
210+ } else Base64 .decode(inputByteArray, 0 )
211+
212+ val decryptedVal = _cx .doFinal(outputBytes) // Finish
213+
214+ outPutByteArray = decryptedVal // Multi-part transformation(decryption)
215+ }
216+
217+ return outPutByteArray // Return encrypted/decrypted string
218+ }
219+
133220
134221 @SuppressWarnings(" kotlin:S1133" ," kotlin:S107" ," kotlin:S1874" )
135222 @Throws(
@@ -152,6 +239,10 @@ class FileCryptLib {
152239 return _cx // Return decrypted cipher instance
153240 }
154241
242+ fun decryptFile (_encryptedText : ByteArray , _key : String , _iv : String ): ByteArray {
243+ return encryptDecryptFile(_encryptedText , _key , EncryptMode .DECRYPT , _iv )
244+ }
245+
155246 /* **
156247 * This function computes the SHA256 hash of input string
157248 *
0 commit comments