SQLite 安全 Windows Phone 8.1
SQLite secure Windows Phone 8.1
我有 windows 通用应用程序。在这个应用程序中,我使用 SQLite,我需要保护这个文件。它保存在 LocalFolder 中,用户可以访问他。
我只需要为我的应用程序设置访问权限或为此数据库或其他任何设置密码。
请问,您知道可以帮助我的扩展程序吗?
谢谢
在 Windows Store API 中,您会找到一些名称空间,我认为您可以将其用于您的目的:Windows.Security.Cryptography, Windows.Security.Cryptography.Core and Windows.Security.Cryptography.DataProtection.
在 Maarten Bodewes 评论后编辑 - 添加了随机初始化向量。
加密某些数据的一个非常简单的示例如下所示:
/// <summary>
/// Method encrypting data in source file and saving to target file
/// </summary>
/// <param name="backupKey">secret key</param>
/// <param name="sourceFile">source file with data</param>
/// <param name="targetFile">encrypted file</param>
public static async Task EncryptFile(string backupKey, StorageFile sourceFile, StorageFile targetFile)
{
SymmetricKeyAlgorithmProvider algorithm = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);
IBuffer keymaterial = CryptographicBuffer.ConvertStringToBinary(backupKey, BinaryStringEncoding.Utf8);
IBuffer initVector = CryptographicBuffer.GenerateRandom(32);
CryptographicKey key = algorithm.CreateSymmetricKey(keymaterial);
IBuffer output = CryptographicEngine.Encrypt(key, await FileIO.ReadBufferAsync(sourceFile), initVector);
await Windows.Storage.FileIO.WriteTextAsync(targetFile, CryptographicBuffer.EncodeToBase64String(initVector) + CryptographicBuffer.EncodeToBase64String(output));
}
/// <summary>
/// Method decrypting a file
/// </summary>
/// <param name="backupKey">secret key</param>
/// <param name="encryptedFile">source file with encrypted data</param>
/// <returns>buffer with devrypted data</returns>
public static async Task<IBuffer> DecryptFile(string backupKey, StorageFile encryptedFile)
{
string entry = await Windows.Storage.FileIO.ReadTextAsync(encryptedFile);
IBuffer initVector = CryptographicBuffer.DecodeFromBase64String(entry.Substring(0, 44));
IBuffer input = CryptographicBuffer.DecodeFromBase64String(entry.Substring(44));
SymmetricKeyAlgorithmProvider algorithm = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);
IBuffer keymaterial = CryptographicBuffer.ConvertStringToBinary(backupKey, BinaryStringEncoding.Utf8);
CryptographicKey key = algorithm.CreateSymmetricKey(keymaterial);
IBuffer inputDecrypted = CryptographicEngine.Decrypt(key, input, initVector);
Debug.WriteLine("Encrypted message: {0}", CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, inputDecrypted));
return inputDecrypted;
}
我是这样测试的:
private const string mySuperSecretKey = @"s3cr3tsadjfjlksdfj@^&^$)(ojfaapsojowejiwfpkwfvz";
private async void firstBtn_Click(object sender, RoutedEventArgs e)
{
var sourceFile = await Package.Current.InstalledLocation.GetFileAsync("TestMessage.txt");
var targetFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("EncryptedMessage.txt", CreationCollisionOption.ReplaceExisting);
await EncryptFile(mySuperSecretKey, sourceFile, targetFile);
}
private async void secondBtn_Click(object sender, RoutedEventArgs e)
{
var sourceFile = await ApplicationData.Current.LocalFolder.GetFileAsync("EncryptedMessage.txt");
var dataDecrypted = await DecryptFile(mySuperSecretKey, sourceFile);
}
上面的代码当然很简单,应该改进,但也许会帮助你开始。另外请记住保护好你的密钥,反编译包并不难。
我有 windows 通用应用程序。在这个应用程序中,我使用 SQLite,我需要保护这个文件。它保存在 LocalFolder 中,用户可以访问他。
我只需要为我的应用程序设置访问权限或为此数据库或其他任何设置密码。 请问,您知道可以帮助我的扩展程序吗?
谢谢
在 Windows Store API 中,您会找到一些名称空间,我认为您可以将其用于您的目的:Windows.Security.Cryptography, Windows.Security.Cryptography.Core and Windows.Security.Cryptography.DataProtection.
在 Maarten Bodewes 评论后编辑 - 添加了随机初始化向量。
加密某些数据的一个非常简单的示例如下所示:
/// <summary>
/// Method encrypting data in source file and saving to target file
/// </summary>
/// <param name="backupKey">secret key</param>
/// <param name="sourceFile">source file with data</param>
/// <param name="targetFile">encrypted file</param>
public static async Task EncryptFile(string backupKey, StorageFile sourceFile, StorageFile targetFile)
{
SymmetricKeyAlgorithmProvider algorithm = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);
IBuffer keymaterial = CryptographicBuffer.ConvertStringToBinary(backupKey, BinaryStringEncoding.Utf8);
IBuffer initVector = CryptographicBuffer.GenerateRandom(32);
CryptographicKey key = algorithm.CreateSymmetricKey(keymaterial);
IBuffer output = CryptographicEngine.Encrypt(key, await FileIO.ReadBufferAsync(sourceFile), initVector);
await Windows.Storage.FileIO.WriteTextAsync(targetFile, CryptographicBuffer.EncodeToBase64String(initVector) + CryptographicBuffer.EncodeToBase64String(output));
}
/// <summary>
/// Method decrypting a file
/// </summary>
/// <param name="backupKey">secret key</param>
/// <param name="encryptedFile">source file with encrypted data</param>
/// <returns>buffer with devrypted data</returns>
public static async Task<IBuffer> DecryptFile(string backupKey, StorageFile encryptedFile)
{
string entry = await Windows.Storage.FileIO.ReadTextAsync(encryptedFile);
IBuffer initVector = CryptographicBuffer.DecodeFromBase64String(entry.Substring(0, 44));
IBuffer input = CryptographicBuffer.DecodeFromBase64String(entry.Substring(44));
SymmetricKeyAlgorithmProvider algorithm = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);
IBuffer keymaterial = CryptographicBuffer.ConvertStringToBinary(backupKey, BinaryStringEncoding.Utf8);
CryptographicKey key = algorithm.CreateSymmetricKey(keymaterial);
IBuffer inputDecrypted = CryptographicEngine.Decrypt(key, input, initVector);
Debug.WriteLine("Encrypted message: {0}", CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, inputDecrypted));
return inputDecrypted;
}
我是这样测试的:
private const string mySuperSecretKey = @"s3cr3tsadjfjlksdfj@^&^$)(ojfaapsojowejiwfpkwfvz";
private async void firstBtn_Click(object sender, RoutedEventArgs e)
{
var sourceFile = await Package.Current.InstalledLocation.GetFileAsync("TestMessage.txt");
var targetFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("EncryptedMessage.txt", CreationCollisionOption.ReplaceExisting);
await EncryptFile(mySuperSecretKey, sourceFile, targetFile);
}
private async void secondBtn_Click(object sender, RoutedEventArgs e)
{
var sourceFile = await ApplicationData.Current.LocalFolder.GetFileAsync("EncryptedMessage.txt");
var dataDecrypted = await DecryptFile(mySuperSecretKey, sourceFile);
}
上面的代码当然很简单,应该改进,但也许会帮助你开始。另外请记住保护好你的密钥,反编译包并不难。