gpt4 book ai didi

android - JNCryptor - RNCryptor 图像文件加密/解密

转载 作者:IT老高 更新时间:2023-10-28 13:46:02 26 4
gpt4 key购买 nike

我正在尝试使用 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()

我可以看到解密后的文件,大小和原文件一致,尝试调试字节数组转换,确保字节与原文件一致。

在 ImageView 中加载文件时,我无法在图库/应用程序中打开文件。

最佳答案

通过更改密码的加密和使用方式解决了。

这是有效的:

//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 is a random alphanumeric string of length 16. Once generated, it needs to be reused while encrypting and decrypting file.

关于android - JNCryptor - RNCryptor 图像文件加密/解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48419188/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com