【问题标题】:Encrypt & Decrypt Local Images in Windows Store App在 Windows 应用商店应用中加密和解密本地图像
【发布时间】:2015-07-05 23:46:40
【问题描述】:

我正在构建一个包含本地图像文件夹的 Windows 应用商店应用程序。

我想保护所有图像,使其无法从以下位置访问:

C:\Users[username]\AppData\Local\Packages\LocalState\Settings\settings.dat

我知道我应该使用 DataProtectionProvider 类对图像进行加密和解密,但文档仅显示如何加密/解密字符串...

我应该如何将位图图像转换为字节数组?还是应该用Base64 对其进行编码?有没有使用这个过程的教程或示例?

【问题讨论】:

    标签: c# encryption windows-store-apps base64 windows-8.1


    【解决方案1】:
            public async void Protect()
            {
                for (int i = 1; i < 24; i++)
                {
                    string imageFile = ImagePages[i];
                    var fileToEncrypt = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile);
                    var encryptedFile1 = await ApplicationData.Current.LocalFolder.CreateFileAsync("encryptedPage" + i);
    
                    var encryptedFile2 = await EncryptFile(fileToEncrypt, encryptedFile1);
                    IBuffer buffer = await DecryptFile(encryptedFile2);
                    //(2.) It goes here and throw the 'System.ArgumentException' having the encryptedFile's ContentType=""
    
                    var bmp = new BitmapImage();
                    await bmp.SetSourceAsync(buffer.AsStream().AsRandomAccessStream());
    
                    //Fill the List responsible for the Portrait View
                    MyPortrait mp = new MyPortrait();
                    mp.onlyImage = bmp;
                    PImageList.Add(mp);
                }
            }    
    
            public async Task<IStorageFile> EncryptFile(IStorageFile fileToEncrypt, IStorageFile encryptedFile)
            {
                IBuffer buffer = await FileIO.ReadBufferAsync(fileToEncrypt);
                //I have no more exceptions here
    
                DataProtectionProvider dataProtectionProvider = new DataProtectionProvider("LOCAL=user");
    
                IBuffer encryptedBuffer = await dataProtectionProvider.ProtectAsync(buffer);
                //(1.) After arriving here when deploying it goes to (2.)
    
                await FileIO.WriteBufferAsync(encryptedFile, encryptedBuffer);
    
                return encryptedFile;
            }
    
            public async Task<IBuffer> DecryptFile(IStorageFile encryptedFile)
            {
                var protectedBuffer = await FileIO.ReadBufferAsync(encryptedFile);
    
                var dataProtectionProvider = new DataProtectionProvider();
    
                var buffer = await dataProtectionProvider.UnprotectAsync(protectedBuffer);
    
                return buffer;
            }
    

    【讨论】:

    • 我怀疑这是因为您在EncryptFile 中写入后尝试从encryptedFile 中读取。尝试使用GetFileAsync 重新打开加密文件,然后再将其传递给DecryptFile
    • 没错!我从 EncryptFile 方法返回了 encryptedFile!谢谢!你摇滚@peterdn
    【解决方案2】:

    如果要加密的图像从文件中加载并写回文件,这是最简单的。然后你可以这样做:

    async void EncryptFile(IStorageFile fileToEncrypt, IStorageFile encryptedFile)
    {
        IBuffer buffer = await FileIO.ReadBufferAsync(fileToEncrypt);
    
        DataProtectionProvider dataProtectionProvider = 
            new DataProtectionProvider(ENCRYPTION_DESCRIPTOR);
    
        IBuffer encryptedBuffer = 
            await dataProtectionProvider.ProtectAsync(buffer);
    
        await FileIO.WriteBufferAsync(encryptedFile, encryptedBuffer);
    }
    

    DataProtectionProvider.ProtectStreamAsync 是另一种选择,如果您可以从输入和输出中获取流实例。例如,如果您有一个包含图像数据的byte[],那么您可以从中创建一个内存输入流:

    byte[] imageData = ...
    using (var inputMemoryStream = new MemoryStream(imageData).AsInputStream())
    {
        ...
    }
    

    编辑:然后例如解密文件并将其显示在Image 控件中,您可以这样做:

    var encryptedBuffer = await FileIO.ReadBufferAsync(encryptedFile);
    
    var dataProtectionProvider = new DataProtectionProvider();
    
    var buffer = await dataProtectionProvider.UnprotectAsync(encryptedBuffer);
    
    var bmp = new BitmapImage();
    await bmp.SetSourceAsync(buffer.AsStream().AsRandomAccessStream());
    imageControl.Source = bmp;
    

    【讨论】:

    • encryptedFile 怎么样?它应该是一个图像,首先应该是空的......我该如何初始化它?关于DecryptFile() 方法,我是否应该使用encryptedFile、新的decryptedFileUnprotectAsyncWriteBufferAsync 做同样的事情?
    • @LayaleMatta 您可以使用任何文件/打开 API 例如初始化 encryptedFile var encryptedFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename)。是的;我在上面的答案中添加了一个解密示例来说明这一点。
    • 我在 IBuffer buffer = await FileIO.ReadBufferAsync(fileToEncrypt); 的 encryptFile 方法中得到一个 System.IO.FileNotFoundException 我有正确的路径想法,并且图像确实存在于我提到的本地文件夹中。这是 fileToEncrypt 的路径:"C:\\[...]\\Projects\\eBookApp\\eBookApp\\bin\\Debug\\AppX\\\\Assets\\Images\\page1.jpg"
    • AppX 和 Assets 之间有 4 个反斜杠,也许这就是问题所在。否则请仔细检查路径绝对正确和/或尝试在其他地方使用另一个图像。
    • 另外,从资产加载图像的更正确方法是:var fileToEncrypt = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Images/page1.jpg"));。否则,您可能会收到UnauthorizedAccessException
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    • 1970-01-01
    相关资源
    最近更新 更多