Windows 8 Store Apps學習(31) 加密解密: 雜湊演算法, 對稱演算法

來源:互聯網
上載者:User

介紹

重新想象 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 演算法的隨機的 key                IBuffer key = CryptographicBuffer.GenerateRandom(hmacAlgorithm.MacLength);                    // 根據 key 產生 CryptographicKey 對象                CryptographicKey hmacKey = hmacAlgorithm.CreateKey(key);                    // 根據 hmacKey 簽名指定的內容                IBuffer signature = CryptographicEngine.Sign(                    hmacKey, // 簽名時所用的 key                    CryptographicBuffer.ConvertStringToBinary(plainText, BinaryStringEncoding.Utf8) // 需要簽名的內容                );                    lblMsg.Text += algorithmName + ": " + CryptographicBuffer.EncodeToHexString(signature) + " (key: " + CryptographicBuffer.EncodeToHexString(key) + ")";                lblMsg.Text += Environment.NewLine;                            // 驗證簽名                bool isAuthenticated = CryptographicEngine.VerifySignature(                    hmacKey, // 簽名時所用的 key                    CryptographicBuffer.ConvertStringToBinary(plainText, BinaryStringEncoding.Utf8), // 需要簽名的內容                    signature // 簽名後的值                );                    lblMsg.Text += "isAuthenticated: " + isAuthenticated;                lblMsg.Text += Environment.NewLine;                lblMsg.Text += Environment.NewLine;            }        }    }}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.