Welcome

首页 / 软件开发 / .NET编程技术 / Windows 8 Store Apps学习(31) 加密解密: 哈希算法, 对称算法

Windows 8 Store Apps学习(31) 加密解密: 哈希算法, 对称算法2013-12-06 cnblogs webabcd介绍

重新想象 Windows 8 Store Apps 之 加密解密

hash 算法(MD5, SHA1, SHA256, SHA384, SHA512)

hmac 算法(MD5, SHA1, SHA256, SHA384, SHA512)

本地数据的加密解密

对 称算法(AES, DES, 3DES, RC2, RC4)

示例

1、演示如何使用 hash 算法(MD5, SHA1, SHA256, SHA384, SHA512)

Crypto/Hash.xaml.cs

/* * 演示如何使用 hash 算法(MD5, SHA1, SHA256, SHA384, SHA512) */using System;using Windows.Security.Cryptography;using Windows.Security.Cryptography.Core;using Windows.Storage.Streams;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;namespace XamlDemo.Crypto{public sealed partial class Hash : Page{public Hash(){this.InitializeComponent();}private void btnDemo_Click(object sender, RoutedEventArgs e){string plainText = "i am webabcd";lblMsg.Text = "原文: " + plainText;lblMsg.Text += Environment.NewLine;lblMsg.Text += Environment.NewLine;string[] algorithmNames = { "MD5", "SHA1", "SHA256", "SHA384", "SHA512" };foreach (var algorithmName in algorithmNames){// 根据算法名称实例化一个哈希算法提供程序HashAlgorithmProvider hashAlgorithm = HashAlgorithmProvider.OpenAlgorithm(algorithmName);// hashAlgorithm.HashLength - 哈希后的值的长度,单位:字节// 原文的二进制数据IBuffer vector = CryptographicBuffer.ConvertStringToBinary(plainText, BinaryStringEncoding.Utf8);// 哈希二进制数据IBuffer digest = hashAlgorithm.HashData(vector);lblMsg.Text += algorithmName + ": " + CryptographicBuffer.EncodeToHexString(digest);lblMsg.Text += Environment.NewLine;// 创建一个可重用的 CryptographicHash 对象CryptographicHash reusableHash = hashAlgorithm.CreateHash();reusableHash.Append(CryptographicBuffer.ConvertStringToBinary("i ", BinaryStringEncoding.Utf8)); // 向 CryptographicHash 中追加需要哈希的二进制数据reusableHash.Append(CryptographicBuffer.ConvertStringToBinary("am ", BinaryStringEncoding.Utf8)); // 向 CryptographicHash 中追加需要哈希的二进制数据reusableHash.Append(CryptographicBuffer.ConvertStringToBinary("webabcd", BinaryStringEncoding.Utf8)); // 向 CryptographicHash 中追加需要哈希的二进制数据// 获取哈希后的数据,然后清空 CryptographicHash 中的数据digest = reusableHash.GetValueAndReset();lblMsg.Text += algorithmName + ": " + CryptographicBuffer.EncodeToHexString(digest);lblMsg.Text += Environment.NewLine;lblMsg.Text += Environment.NewLine;}}}}
2、演示如何使用 hmac 算法(HMAC_MD5, HMAC_SHA1, HMAC_SHA256, HMAC_SHA384, HMAC_SHA512 )

Crypto/Hmac.xaml.cs

/* * 演示如何使用 hmac 算法(HMAC_MD5, HMAC_SHA1, HMAC_SHA256, HMAC_SHA384, HMAC_SHA512) ** 注:hmac 相当于带密钥的 hash,可以理解为将信息用密钥加密后再哈希 */using System;using Windows.Security.Cryptography;using Windows.Security.Cryptography.Core;using Windows.Storage.Streams;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;namespace XamlDemo.Crypto{public sealed partial class Hmac : Page{public Hmac(){this.InitializeComponent();}private void btnDemo_Click(object sender, RoutedEventArgs e){string plainText = "i am webabcd";lblMsg.Text = "原文: " + plainText;lblMsg.Text += Environment.NewLine;lblMsg.Text += Environment.NewLine;string[] algorithmNames = { "HMAC_MD5", "HMAC_SHA1", "HMAC_SHA256", "HMAC_SHA384", "HMAC_SHA512" };foreach (var algorithmName in algorithmNames){// 根据算法名称实例化一个 hmac 算法提供程序MacAlgorithmProvider hmacAlgorithm = MacAlgorithmProvider.OpenAlgorithm(algorithmName);// hmacAlgorithm.MacLength - hmac 后的值的长度,单位:字节// 创建一个用于 hmac 算法的随机的 keyIBuffer key = CryptographicBuffer.GenerateRandom(hmacAlgorithm.MacLength);// 根据 key 生成 CryptographicKey 对象CryptographicKey hmacKey = hmacAlgorithm.CreateKey(key);// 根据 hmacKey 签名指定的内容IBuffer signature = CryptographicEngine.Sign(hmacKey, // 签名时所用的 keyCryptographicBuffer.ConvertStringToBinary(plainText, BinaryStringEncoding.Utf8) // 需要签名的内容);lblMsg.Text += algorithmName + ": " + CryptographicBuffer.EncodeToHexString(signature) + " (key: " + CryptographicBuffer.EncodeToHexString(key) + ")";lblMsg.Text += Environment.NewLine;// 验证签名bool isAuthenticated = CryptographicEngine.VerifySignature(hmacKey, // 签名时所用的 keyCryptographicBuffer.ConvertStringToBinary(plainText, BinaryStringEncoding.Utf8), // 需要签名的内容signature // 签名后的值);lblMsg.Text += "isAuthenticated: " + isAuthenticated;lblMsg.Text += Environment.NewLine;lblMsg.Text += Environment.NewLine;}}}}