Securing Windows Store App Data (SWSA)
Khái niệm
Là kỹ thuật mã hóa/giải mã dữ liệu trong việc bảo mật dữ liệu ứng dụng Windows Store.Hướng dẫn
Tạo project mới có tên EncryptDecrytDemo:Mở MainPage.xaml ra, code theo hình:
Copy/paste:
<Page
x:Class="EncryptDecrytDemo.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:EncryptDecrytDemo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock FontSize="40" Text="Encrypt Decrypt Demo" Margin="450,60,475,650" Height="60" Width="450"/>
<TextBlock FontSize="20" Text="Plain Text" Margin="395,225,825,505"/>
<TextBlock FontSize="20" Text="Encrypted Value" Margin="396,315,825,414" />
<TextBlock x:Name ="txtEncrypt" FontSize="20" Margin="567,315,199,399" Width="600"/>
<TextBlock FontSize="20" Text="Decrypted Value" Margin="396,403,825,329"/>
<TextBlock x:Name ="txtDecrypted" FontSize="20" Margin="567,402,199,329" Width="600"/>
<TextBox x:Name="txtText" FontSize="20" Margin="603,221,386,505" Width="377"/>
<Button Content="Submit" Margin="600,475,0,255" Click="Button_Click"/>
</Grid>
</Page>
Hãy đảm bảo rằng khi code xong design nó sẽ trông ntn :)
Mở MainPage.xaml.cs để code xử lý:
Copy/paste:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace EncryptDecrytDemo
{
///
/// An empty page that can be used on its own or navigated to within a Frame.
///
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
public static byte[] Encrypt(string plainText, string pw, string sult)
{
IBuffer pwBuffer = CryptographicBuffer.ConvertStringToBinary(pw, BinaryStringEncoding.Utf8);
IBuffer sultBuffer = CryptographicBuffer.ConvertStringToBinary(sult, BinaryStringEncoding.Utf16LE);
IBuffer plainBuffer = CryptographicBuffer.ConvertStringToBinary(plainText, BinaryStringEncoding.Utf16LE);
KeyDerivationAlgorithmProvider key = KeyDerivationAlgorithmProvider.OpenAlgorithm("PBKDF2_SHA1");
KeyDerivationParameters parm = KeyDerivationParameters.BuildForPbkdf2(sultBuffer, 1000);
CryptographicKey ckey = key.CreateKey(pwBuffer);
IBuffer keyMaterial = CryptographicEngine.DeriveKeyMaterial(ckey, parm, 32);
CryptographicKey dkey = key.CreateKey(pwBuffer);
IBuffer sulltMaterial = CryptographicEngine.DeriveKeyMaterial(dkey, parm, 16);
SymmetricKeyAlgorithmProvider sp = SymmetricKeyAlgorithmProvider.OpenAlgorithm("AES_CBC_PKCS7");
CryptographicKey symKey = sp.CreateSymmetricKey(keyMaterial);
IBuffer resultBuffer = CryptographicEngine.Encrypt(symKey, plainBuffer, sulltMaterial);
byte[] result;
CryptographicBuffer.CopyToByteArray(resultBuffer, out result);
return result;
}
public static string Decrypt(byte[] encryptval, string pw, string salt)
{
IBuffer pwBuffer = CryptographicBuffer.ConvertStringToBinary(pw, BinaryStringEncoding.Utf8);
IBuffer saltBuffer = CryptographicBuffer.ConvertStringToBinary(salt, BinaryStringEncoding.Utf16LE);
IBuffer cipherBuffer = CryptographicBuffer.CreateFromByteArray(encryptval);
KeyDerivationAlgorithmProvider key = KeyDerivationAlgorithmProvider.OpenAlgorithm("PBKDF2_SHA1");
KeyDerivationParameters parm = KeyDerivationParameters.BuildForPbkdf2(saltBuffer, 1000);
CryptographicKey ckey = key.CreateKey(pwBuffer);
IBuffer keyMaterial = CryptographicEngine.DeriveKeyMaterial(ckey, parm, 32);
CryptographicKey dkey = key.CreateKey(pwBuffer);
IBuffer saltMaterial = CryptographicEngine.DeriveKeyMaterial(ckey, parm, 16);
SymmetricKeyAlgorithmProvider sp = SymmetricKeyAlgorithmProvider.OpenAlgorithm("AES_CBC_PKCS7");
CryptographicKey symmKey = sp.CreateSymmetricKey(keyMaterial);
IBuffer resultBuffer = CryptographicEngine.Decrypt(symmKey, cipherBuffer, saltMaterial);
string result = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf16LE, resultBuffer);
return result;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
byte[] encrypt;
encrypt = Encrypt(txtText.Text, "pw", "salt");
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
txtEncrypt.Text = encoding.GetString(encrypt, 0, encrypt.Count());
txtDecrypted.Text = Decrypt(encrypt, "pw", "salt");
}
}
}
Test app vừa tạo nào :)
Tổng kết
- Qua ví dụ trên, chúng ta đã được biết cách mã hóa/giải mã kí tự đơn giản bằng Encoding Utf-8 và Utf16LE.Nội dung tham khảo
- Mã hoá chuỗi trong C#- Encrypting and Decrypting trên MSDN
Không có nhận xét nào :
Đăng nhận xét