gpt4 book ai didi

c# - Xamarin.android-如何更快地加密/解密图像

转载 作者:行者123 更新时间:2023-11-30 00:10:00 27 4
gpt4 key购买 nike

我希望用户从图库中选择图片/视频,并在我的应用中保护他们的图片。为此,我加密了这些图像。图像加密工作正常(我认为是这样!)。 8MB 图像需要 1.5 到 2 秒。但是视频呢?视频可能以 GB 为单位。所以会花很多时间。即使在加密/解密中,我也必须对每个图像执行操作,这可能会导致内存问题。 This链接帮助我实现了这一目标。

如你所见,ES 文件浏览器还提供了对图片/视频的加密和解密。并在几秒钟内完成 GB 的操作。那么我能知道这些人使用的是哪种技术/算法吗?

或者即使我使用自己的方式,有什么技巧可以让它更快吗?还是有其他方法可以使用户无法访问文件?更改 MIME 类型会起作用吗?

即使我更改扩展名或通过添加 .在文件名之前,用户仍然可以在某些文件资源管理器中查看图像。

实际上对于xamarin,我没有找到任何与加密解密文件相关的帖子/博客。他们提供的只是字符串解决方案。

如果有人指导我解决这个问题,我将不胜感激。

编辑

您好,@Joe Lv,正如我所说,我尝试了您的加密速度较慢但解密速度非常快的方法。所以我实现了你用来加密东西的相同解密技术。它有效!但我想知道这是否有效。

现在我的加密方法是这样的:

public void encrypt(string filename)
{

// Here you read the cleartext.
try
{
File extStore = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryMovies);
startTime = System.DateTime.Now.Millisecond;
Android.Util.Log.Error("Encryption Started", extStore + "/" + filename);

// This stream write the encrypted text. This stream will be wrapped by
// another stream.
// createFile(filename, extStore);
// System.IO.FileStream fs=System.IO.File.OpenRead(extStore + "/" + filename);
// FileOutputStream fos = new FileOutputStream(extStore + "/" + filename + ".aes", false);

FileInputStream fis = new FileInputStream(filepath);


FileOutputStream fos = new FileOutputStream(filepath, false);
System.IO.FileStream fs = System.IO.File.OpenWrite(filepath + filename);
// Create cipher

// Length is 16 byte
Cipher cipher = Cipher.GetInstance("AES/CBC/PKCS5Padding");
byte[] raw = System.Text.Encoding.Default.GetBytes(sKey);
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
IvParameterSpec iv = new IvParameterSpec(System.Text.Encoding.Default.GetBytes(ivParameter));//
cipher.Init(CipherMode.EncryptMode, skeySpec, iv);

// Wrap the output stream
// CipherInputStream cis = new CipherInputStream(fs, cipher);
CipherOutputStream cos = new CipherOutputStream(fs, cipher);

// Write bytes
int b;
byte[] d = new byte[512 * 1024];
while ((b = fis.Read(d)) != -1)
{
cos.Write(d, 0, b);
}
// Flush and close streams.
fos.Flush();
fos.Close();
cos.Close();
fis.Close();

stopTime = System.DateTime.Now.Millisecond;
Android.Util.Log.Error("Encryption Ended", extStore + "/5mbtest/" + filename + ".aes");
Android.Util.Log.Error("Time Elapsed", ((stopTime - startTime) / 1000.0) + "");
}
catch (Exception e)
{
Android.Util.Log.Error("lv",e.Message);
}

}

最佳答案

I didn't find any post/blog related to encrypting decrypting file

可以使用CipherOutputStreamCipherInputStream来实现。

这是我的测试演示,你可以试试,你需要将一个名为videoplayback.mp4的视频文件推送到手机中,路径为/storage/sdcard/Movies,所以你可以直接测试我的代码。

using Android.App;
using Android.Widget;
using Android.OS;
using Javax.Crypto.Spec;
using Java.Lang;
using Java.IO;
using Javax.Crypto;
using System.Text;

namespace EncryTest
{
[Activity(Label = "EncryTest", MainLauncher = true)]
public class MainActivity : Activity
{
long stopTime, startTime;
private string sKey = "0123456789abcdef";//key,
private string ivParameter = "1020304050607080";
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);

// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);

encrypt("videoplayback.mp4");
decrypt("videoplayback.mp4");
}

public void encrypt(string filename)
{

// Here you read the cleartext.
try
{
File extStore = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryMovies);
startTime = System.DateTime.Now.Millisecond;
Android.Util.Log.Error("Encryption Started", extStore + "/" + filename);

// This stream write the encrypted text. This stream will be wrapped by
// another stream.
createFile(filename, extStore);
System.IO.FileStream fs=System.IO.File.OpenRead(extStore + "/" + filename);
FileOutputStream fos = new FileOutputStream(extStore + "/" + filename + ".aes", false);

// Length is 16 byte
Cipher cipher = Cipher.GetInstance("AES/CBC/PKCS5Padding");
byte[] raw = System.Text.Encoding.Default.GetBytes(sKey);
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
IvParameterSpec iv = new IvParameterSpec(System.Text.Encoding.Default.GetBytes(ivParameter));//
cipher.Init(CipherMode.EncryptMode, skeySpec, iv);

// Wrap the output stream
CipherInputStream cis = new CipherInputStream(fs, cipher);
// Write bytes
int b;
byte[] d = new byte[1024 * 1024];
while ((b = cis.Read(d)) != -1)
{
fos.Write(d, 0, b);
}
// Flush and close streams.
fos.Flush();
fos.Close();
cis.Close();
stopTime = System.DateTime.Now.Millisecond;
Android.Util.Log.Error("Encryption Ended", extStore + "/5mbtest/" + filename + ".aes");
Android.Util.Log.Error("Time Elapsed", ((stopTime - startTime) / 1000.0) + "");
}
catch (Exception e)
{
Android.Util.Log.Error("lv",e.Message);
}

}


private void createFile(string filename, File extStore)
{
File file = new File(extStore + "/" + filename + ".aes");

if (filename.IndexOf(".") != -1)
{
try
{
file.CreateNewFile();
}
catch (IOException e)
{
// TODO Auto-generated catch block
Android.Util.Log.Error("lv",e.Message);
}
Android.Util.Log.Error("lv","file created");
}
else
{
file.Mkdir();
Android.Util.Log.Error("lv","folder created");
}

file.Mkdirs();
}
public void decrypt(string filename)
{
try
{

File extStore = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryMovies);
Android.Util.Log.Error("Decryption Started", extStore + "");
FileInputStream fis = new FileInputStream(extStore + "/" + filename + ".aes");

createFile(filename, extStore);
FileOutputStream fos = new FileOutputStream(extStore + "/" + "decrypted" + filename, false);
System.IO.FileStream fs = System.IO.File.OpenWrite(extStore + "/" + "decrypted" + filename);
// Create cipher

Cipher cipher = Cipher.GetInstance("AES/CBC/PKCS5Padding");
byte[] raw = System.Text.Encoding.Default.GetBytes(sKey);
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
IvParameterSpec iv = new IvParameterSpec(System.Text.Encoding.Default.GetBytes(ivParameter));//
cipher.Init(CipherMode.DecryptMode, skeySpec, iv);

startTime = System.DateTime.Now.Millisecond;
CipherOutputStream cos = new CipherOutputStream(fs, cipher);
int b;
byte[] d = new byte[1024 * 1024];
while ((b = fis.Read(d)) != -1)
{
cos.Write(d, 0, b);
}

stopTime = System.DateTime.Now.Millisecond;

Android.Util.Log.Error("Decryption Ended", extStore + "/" + "decrypted" + filename);
Android.Util.Log.Error("Time Elapsed", ((stopTime - startTime) / 1000.0) + "");

cos.Flush();
cos.Close();
fis.Close();
}
catch (Exception e)
{
Android.Util.Log.Error("lv", e.Message);
}
}
}
}

视频只是为了测试,你可以从其他路径获取视频文件,稍微改变一下就可以了。

关于路径/storage/sdcard/Movies,我觉得一张图会更好理解enter image description here

关于c# - Xamarin.android-如何更快地加密/解密图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48306345/

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