我正在尝试使用RNCryptor库的 AES256 实现来使图像加密/解密工作。
到目前为止,这是我的代码:
//Encrypt file
/**
*
var encryptedData = RNCryptor.encrypt(data: data as Data, withPassword: hashKey.description)
try encryptedData.write(to: fileURL)
* */
fun encryptFile( inputFile : File ) : File {
val size = inputFile.length().toInt()
val fileBytes = ByteArray(size)
val aeS256JNCryptor = AES256JNCryptor()
val file = File(Environment.getExternalStorageDirectory().toString() + "/Encrypted_" + inputFile.name)
try {
val buf = BufferedInputStream(FileInputStream(inputFile))
buf.read(fileBytes, 0, fileBytes.size)
val encryptedFileBytes = aeS256JNCryptor.encryptData(fileBytes, "master".toCharArray())
val bufOut = BufferedOutputStream(FileOutputStream(file))
bufOut.write(encryptedFileBytes)
buf.close()
bufOut.close()
} catch (e: FileNotFoundException) {
// TODO Auto-generated catch block
e.printStackTrace()
} catch (e: IOException) {
// TODO Auto-generated catch block
e.printStackTrace()
}
return file
}
//Decrypt file
/**
*
let encryptedData = fileManager.contents(atPath: filePathNormal)
let decryptedData = try RNCryptor.decrypt(data: encryptedData!, withPassword: hashKey.description)
let selected_image = UIImage.sd_image(with: decryptedData)
* */
fun decryptFile( inputFile : File ) : File {
val size = inputFile.length().toInt()
val fileBytes = ByteArray(size)
val aeS256JNCryptor = AES256JNCryptor()
val file = File(Environment.getExternalStorageDirectory().toString() + "/Decrypted_" + inputFile.name)
try {
val buf = BufferedInputStream(FileInputStream(inputFile))
buf.read(fileBytes, 0, fileBytes.size)
val decryptedFileBytes = aeS256JNCryptor.decryptData(fileBytes, "master".toCharArray())
val bufOut = BufferedOutputStream(file.outputStream())
bufOut.write(decryptedFileBytes)
buf.close()
bufOut.close()
} catch (e: FileNotFoundException) {
// TODO Auto-generated catch block
e.printStackTrace()
} catch (e: IOException) {
// TODO Auto-generated catch block
e.printStackTrace()
}
return file
}
解密后我无法加载/查看图像。我已经发布了评论中使用的相关 iOS 代码。如果我在某个地方出错,请告诉我。
以下是我已经尝试过但没有成功的事情:
fun decryptFile( inputFile : File ) : File {
val size = inputFile.length().toInt()
val fileBytes = ByteArray(size)
val aeS256JNCryptor = AES256JNCryptor()
val file = File(Environment.getExternalStorageDirectory().toString() + "/Decrypted_" + inputFile.name)
try {
val buf = BufferedInputStream(FileInputStream(inputFile))
buf.read(fileBytes, 0, fileBytes.size)
val decryptedFileBytes = aeS256JNCryptor.decryptData(fileBytes, "master".toCharArray())
if( file.exists() ) file.delete()
//val bufOut = BufferedOutputStream(file.outputStream())
//bufOut.write(decryptedFileBytes)
val fileOutputStream = FileOutputStream( file.absolutePath )
fileOutputStream.write(decryptedFileBytes)
buf.close()
fileOutputStream.close()
//bufOut.close()
} catch (e: FileNotFoundException) {
// TODO Auto-generated catch block
e.printStackTrace()
} catch (e: IOException) {
// TODO Auto-generated catch block
e.printStackTrace()
}
return file
}
保存位图的另一种方法:
val fileOutputStream = FileOutputStream( file.absolutePath )
//fileOutputStream.write(decryptedFileBytes)
val bitmap = BitmapFactory.decodeByteArray(decryptedFileBytes, 0, decryptedFileBytes.size)
if( bitmap != null )
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream)
buf.close()
fileOutputStream.close()
我可以看到解密后的文件,大小与原始文件大小匹配,尝试调试字节数组转换以确保字节与原始文件相同。
在图像视图中加载文件时,我无法在图库/应用程序中打开文件。