我正在尝试使用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()
我能够看到解密文件,大小与原始文件的大小匹配,尝试调试字节数组转换以确保字节与原始字节相同。
在图片视图中加载时,我无法在Gallery / in app中打开文件。
答案 0 :(得分:1)
通过更改加密和使用密码的方式解决了这个问题。
这是有效的:
//Encrypt file
/**
*
var encryptedData = RNCryptor.encrypt(data: data as Data, withPassword: hashKey.description)
try encryptedData.write(to: fileURL)
fileBytes - 3,1,-54,106
encrypted - 3,1,71,68
* */
fun encryptFile( inputFile : File, privateKey : CharArray ) : 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, privateKey)
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)
encrypted - 3,1,71,68
decrypted Bytes - 3,1,-54,106
* */
fun decryptFile( inputFile : File, privateKey: CharArray ) : 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, privateKey)
if( file.exists() ) file.delete()
val fileOutputStream = FileOutputStream( file.absolutePath )
fileOutputStream.write(decryptedFileBytes)
buf.close()
fileOutputStream.close()
} catch (e: FileNotFoundException) {
// TODO Auto-generated catch block
e.printStackTrace()
} catch (e: IOException) {
// TODO Auto-generated catch block
e.printStackTrace()
}
return file
}
privateKey是一个长度为16的随机字母数字字符串。一旦生成,它就会出现 需要在加密和解密文件时重复使用。