【发布时间】:2018-06-26 15:26:07
【问题描述】:
我希望用户从图库中选择图像/视频并在我的应用中保护他们的图像。为此,我加密了这些图像。对图像的加密工作正常(我想是的!)。 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);
}
}
【问题讨论】:
-
图片为什么不用
ThreadPoolExecutor加密呢? -
你可以使用
CipherOutputStream来解密文件。 -
我认为 ES 文件浏览器可能只是打开一个线程来加密/解密并在 UI 线程中显示结果,所以实际上工作线程很慢,这只是一个猜测。
-
这是使用 android keystore 解释的一种方法:appliedcodelog.com/2021/07/…
标签: c# android encryption xamarin.android