这里我尝试以加密形式将一些文本写入本地文件。但是当我在本地看到文件时,数据不是加密形式。 有人可以纠正我。
//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;
答案 0 :(得分:2)
您可以使用CryptographicEngine
class实际执行加密和解密。
但是上面的@Neil's评论 - 你在哪里存储解密密钥?如果密钥存储为应用程序的一部分,则它并不能真正保护任何内容。如果它来自用户输入(例如,他们每次输入的密码+可选的盐),那就更好了。
答案 1 :(得分:0)
如果你想在代码中保守秘密,可以使用混淆。然而,这也可能被打破,但至少它会使打破安全性变得更加困难,因此尝试和破解它的动力就会减少。最后,所需的保护量取决于您要隐藏的数据的重要性。
答案 2 :(得分:0)
加密或解密存储文件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;
}