Xamarin.android- how to make encryption/decryption of images faster

前端 未结 1 1319
星月不相逢
星月不相逢 2021-01-25 17:36

I want users to select images/videos from gallery and secure their images within my app. for that, I encrypt those images. Encryption over images works correctly(I think so !).

相关标签:
1条回答
  • 2021-01-25 17:50

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

    You can use CipherOutputStream and CipherInputStream to achieve it.

    Here is my test demo, you can try it, and you need push a video file named videoplayback.mp4 into your phone which path is /storage/sdcard/Movies, so you can test my code directly.

    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);
                }
            }
        }
    }
    

    The video just for test, you can get the video file from another path, just a little change will be good.

    About the path /storage/sdcard/Movies, I think a picture will be better to understand it

    0 讨论(0)
提交回复
热议问题