在c#windows 10 universal app development中以加密形式将数据存储到文件中

在c#windows 10 universal app development中以加密形式将数据存储到文件中,c#,file,security,encryption,windows-10,C#,File,Security,Encryption,Windows 10,在这里,我试图将一些文本以加密的形式写入本地文件,但当我在本地看到文件时,数据不是加密的。 有人能纠正我吗 //Writing to file StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder; // textBlock.Text = folder.Path; StorageFile sampleFile = await folder.CreateFileAsync

在这里,我试图将一些文本以加密的形式写入本地文件,但当我在本地看到文件时,数据不是加密的。 有人能纠正我吗

    //Writing to file
    StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder;
    // textBlock.Text = folder.Path;
    StorageFile sampleFile = await folder.CreateFileAsync("sample.txt", CreationCollisionOption.ReplaceExisting);
    var buffer = Windows.Security.Cryptography.CryptographicBuffer.ConvertStringToBinary(
                "Text input into sample.txt file", Windows.Security.Cryptography.BinaryStringEncoding.Utf8);
    await Windows.Storage.FileIO.WriteBufferAsync(sampleFile, buffer);

    //Reading from file
    StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
    sampleFile = await storageFolder.GetFileAsync("sample.txt");
    string text = await Windows.Storage.FileIO.ReadTextAsync(sampleFile);
    // textBlock.Text = text;
您可以使用来实际执行加密和解密


但是上面的评论是,你把解密密钥存储在哪里?如果密钥作为应用程序的一部分存储,那么它实际上并不能保护任何东西。如果它来自用户输入(例如,他们每次输入的密码+可选的salt),则更好。

如果您想在代码中保守秘密,可以使用模糊处理。然而,这也可能会被破坏,但至少会使破坏安全性变得更加困难,从而减少尝试和破解它的动机。最后,所需的保护量取决于要隐藏的数据的重要性。

加密或解密storagefile UWP

 public static async Task<StorageFile> EncryptStorageFileLocalUserAsync(this StorageFile FileForEncryption)
    {
        //"LOCAL = user"
        IBuffer data = await FileIO.ReadBufferAsync(FileForEncryption);
        IBuffer SecuredData = await DataProtectionStream("LOCAL = user", data);
        var  EncryptedFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(
          "EncryptedFile" + FileForEncryption.FileType, CreationCollisionOption.ReplaceExisting);
        await FileIO.WriteBufferAsync(EncryptedFile, SecuredData);
        return EncryptedFile;
       // Reporting.DisplayMessage( "File encryption successfull. File stored at " + EncryptedFile.Path + "\n\n");

    }
    public static async Task<StorageFile> DecryptStorageFileLocalUserAsync(this StorageFile EncryptedFile)
    {

        IBuffer data = await FileIO.ReadBufferAsync(EncryptedFile);
        IBuffer UnSecuredData = await DataUnprotectStream(data);

         var DecryptedFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(
          "DecryptedFile" + EncryptedFile.FileType, CreationCollisionOption.ReplaceExisting);
        await FileIO.WriteBufferAsync(DecryptedFile, UnSecuredData);
       // Reporting.DisplayMessage("File decryption successfull. File stored at " + DecryptedFile.Path + "\n\n");
        return DecryptedFile;

    }
公共静态异步任务EncryptStorageFileLocalUserAsync(此StorageFile FileForEncryption)
{
//“本地=用户”
IBuffer data=wait FileIO.ReadBufferAsync(FileForEncryption);
IBuffer SecuredData=等待数据保护流(“本地=用户”,数据);
var EncryptedFile=wait ApplicationData.Current.TemporaryFolder.CreateFileAsync(
“EncryptedFile”+FileForEncryption.FileType,CreationCollisionOption.ReplaceExisting);
wait FileIO.WriteBufferAsync(EncryptedFile,SecuredData);
返回加密文件;
//Reporting.DisplayMessage(“文件加密成功。文件存储在“+EncryptedFile.Path+”\n\n”);
}
公共静态异步任务DecryptStorageFileLocalUserAsync(此StorageFile EncryptedFile)
{
IBuffer data=await FileIO.ReadBufferAsync(EncryptedFile);
IBuffer UnSecuredData=等待数据取消保护流(数据);
var DecryptedFile=wait ApplicationData.Current.TemporaryFolder.CreateFileAsync(
“DecryptedFile”+EncryptedFile.FileType,CreationCollisionOption.ReplaceExisting);
wait FileIO.WriteBufferAsync(解密文件,未恢复数据);
//Reporting.DisplayMessage(“文件解密成功。文件存储在“+DecryptedFile.Path+”\n\n”);
返回解密文件;
}

Utf8不是加密,而
ConvertStringToBinary
不是执行加密的方法。不确定您的示例是否与您的问题相关…我是应用程序开发新手,我通过以下方式尝试了这一点:,请提供一些教程来加密文件中的数据并再次解密。请告诉我们您计划如何首先保护加密密钥…我需要以加密形式在本地存储字符串,并在我的应用程序中检索字符串时解密。使用Windows.Security.Credentials有什么问题吗?或者将用户凭据存储在凭据存储柜中是一种糟糕的做法?将凭据存储在存储柜中非常好。问题是“他们首先来自哪里”