【问题标题】:sqlite encryption in windows phone 8.1windows phone 8.1中的sqlite加密
【发布时间】:2015-10-02 14:27:09
【问题描述】:

我正在使用 Visual Studio 2014 更新 4 为 Windows Phone 8.1 开发应用程序我还使用来自 nuget 源的 sqlite 数据库来存储数据。我想对我的数据库文件进行密码加密,我得到了诸如 open ssl 和 sqlenrypt 之类的库,但它们支付了我负担不起的费用。任何人都可以通过免费图书馆帮助我。 我还检查了 sqlcipher(开源库之一),但我没有将它与我的项目合并的任何要点。如果其他库也在那里,请为我提供与项目集成的步骤。

【问题讨论】:

    标签: sqlite windows-phone-8.1


    【解决方案1】:

    您可以使用 Windows Cryptography Encryption 技术来加密和解密文件。

    对于加密:

    private async Task<string> EncryptFileHelper(string source, string destination)
        {
            StorageFolder appFolder = ApplicationData.Current.LocalFolder;
                    try
                    {
                        StorageFile file = await appFolder.GetFileAsync(source);
                        IBuffer fileStreamBuffer = await FileIO.ReadBufferAsync(file);
                        string protectionDescriptor = "LOCAL=user";
                        Windows.Security.Cryptography.DataProtection.DataProtectionProvider provider = String.IsNullOrEmpty(protectionDescriptor) ? new Windows.Security.Cryptography.DataProtection.DataProtectionProvider() : new Windows.Security.Cryptography.DataProtection.DataProtectionProvider(protectionDescriptor);
                        IBuffer encryptedBuffer = await provider.ProtectAsync(fileStreamBuffer);
    
                        StorageFile encryptedFile = await appFolder.CreateFileAsync(destination, CreationCollisionOption.ReplaceExisting);
                        await FileIO.WriteBufferAsync(encryptedFile, encryptedBuffer);
                    }
                    catch (Exception)
                    {
                        return "fail";
                    }
                return "success";
        }
    

    source 中,只需传递您要加密的文件的路径,在 destination 中,传递您要存储加密文件的路径。目的地可能是相同的路径。喜欢:

    string source = ApplicationData.Current.LocalFolder.Path + "\\YourFolder\\YourFileName.sqlite";
    

    用于解密:

    private async Task<string> DecryptFileHelper(string source, string destination)
        {
            StorageFolder appFolder = ApplicationData.Current.LocalFolder;
             try
                    {
                        StorageFile file = await appFolder.GetFileAsync(source);
                        IBuffer fileStreamBuffer = await FileIO.ReadBufferAsync(file);
                        string protectionDescriptor = "LOCAL=user";
                        Windows.Security.Cryptography.DataProtection.DataProtectionProvider provider = String.IsNullOrEmpty(protectionDescriptor) ? new Windows.Security.Cryptography.DataProtection.DataProtectionProvider() : new Windows.Security.Cryptography.DataProtection.DataProtectionProvider(protectionDescriptor);
                        IBuffer encryptedBuffer = await provider.UnprotectAsync(fileStreamBuffer);
    
                        StorageFile encryptedFile = await appFolder.CreateFileAsync(destination, CreationCollisionOption.ReplaceExisting);
                        await FileIO.WriteBufferAsync(encryptedFile, encryptedBuffer);
                    }
                    catch (Exception)
                    {
                        return "fail";
                    }
                return "success";
        }
    

    希望对您有所帮助!谢谢!

    【讨论】:

    • 谢谢先生。但是对于每个查询,我都需要加密和解密数据库文件,这会降低性能。我希望数据库之类的东西只能通过使用密码来访问。
    • 哦,我以为你只需要加密 sqlite 文件。是的,每次加密和解密文件都会降低性能。
    • 如果文件大于 40 mb,则会抛出可用内存不足的异常。(512 mb RAM)。
    猜你喜欢
    • 2014-09-21
    • 1970-01-01
    • 2014-08-27
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多