在c#中以加密形式将数据存储到文件windows 10 通用应用程序开发
Store data to a file in encrypted form in c# windows 10 universal app development
在这里,我尝试将一些文本以加密的方式写入本地文件 form.But 当我在本地看到文件时,数据不是加密形式。
有人可以纠正我吗
//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;
您可以使用CryptographicEngine
class来实际执行加密和解密。
但是@Neil's上面的评论-你在哪里存储解密密钥?如果密钥作为应用程序的一部分存储,它并不能真正保护任何东西。如果它来自用户输入(例如,他们每次输入的密码 + 可选的加盐),那就更好了。
如果您想在代码中保密,可以使用混淆。然而,这也可能被破坏,但至少它会使破坏安全性变得更加困难,因此尝试和破解它的动机也会减少。最后,所需的保护量取决于您要隐藏的数据的重要性。
加密或解密存储文件 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;
}
在这里,我尝试将一些文本以加密的方式写入本地文件 form.But 当我在本地看到文件时,数据不是加密形式。 有人可以纠正我吗
//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;
您可以使用CryptographicEngine
class来实际执行加密和解密。
但是@Neil's上面的评论-你在哪里存储解密密钥?如果密钥作为应用程序的一部分存储,它并不能真正保护任何东西。如果它来自用户输入(例如,他们每次输入的密码 + 可选的加盐),那就更好了。
如果您想在代码中保密,可以使用混淆。然而,这也可能被破坏,但至少它会使破坏安全性变得更加困难,因此尝试和破解它的动机也会减少。最后,所需的保护量取决于您要隐藏的数据的重要性。
加密或解密存储文件 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;
}