我试图使用AES256实现RNCryptor库来实现图像加密/解密。
到目前为止,这是我的代码:
//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()我能够看到解密的文件,大小与原始文件相匹配,尝试调试字节数组转换,以确保字节与原始文件相同。
当将文件加载到图像视图中时,我无法在图片库/应用程序中打开文件。
发布于 2018-02-05 07:04:39
它是通过改变密码加密和使用的方式解决的。
以下是起作用的原因:
//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的随机字母数字字符串。一旦生成,就需要在加密和解密文件时重用它。
https://stackoverflow.com/questions/48419188
复制相似问题