首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

Windows 8 加密解密進階:非對稱演算法 (RSA) 與資料轉換輔助類

     編輯:關於.NET

前言

Windows 8 提供完整的密碼編譯 API (Windows.Security.Cryptography),支援非對稱加密 (RSA)、數位簽章驗證,以及 CryptographicBuffer 這個強大的資料格式轉換輔助類。本文透過完整範例示範 RSA 加密/解密、簽名/驗證、以及字串、十六進位、Base64、二進位資料間的相互轉換。

一、非對稱演算法 (RSA) 加密與解密

RSA 適用於加密少量資料 (如對稱金鑰)。支援多種填充模式:RSA_PKCS1、RSA_OAEP_SHA1、RSA_OAEP_SHA256、RSA_OAEP_SHA384、RSA_OAEP_SHA512。原文長度受限於金鑰長度與填充模式 (例如 2048-bit 金鑰下 OAEP SHA256 最大原文 256 位元組)。

程式碼範例

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 Asymmetric : Page
    {
        public Asymmetric()
        {
            this.InitializeComponent();
        }

        private void btnDemo_Click(object sender, RoutedEventArgs e)
        {
            string plainText = "i am webabcd";
            uint keySize = 2048;
            lblMsg.Text = "原文: " + plainText;
            lblMsg.Text += Environment.NewLine;
            lblMsg.Text += "keySize: " + keySize / 8;
            lblMsg.Text += Environment.NewLine;
            lblMsg.Text += Environment.NewLine;

            string[] algorithmNames = { "RSA_PKCS1", "RSA_OAEP_SHA1", "RSA_OAEP_SHA256", "RSA_OAEP_SHA384", "RSA_OAEP_SHA512" };
            foreach (var algorithmName in algorithmNames)
            {
                IBuffer buffer = CryptographicBuffer.ConvertStringToBinary(plainText, BinaryStringEncoding.Utf8);
                IBuffer encrypted;
                IBuffer decrypted;
                IBuffer blobPublicKey;
                IBuffer blobKeyPair;
                CryptographicKey keyPair;

                AsymmetricKeyAlgorithmProvider asymmetricAlgorithm = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(algorithmName);
                try
                {
                    keyPair = asymmetricAlgorithm.CreateKeyPair(keySize);
                }
                catch (Exception ex)
                {
                    lblMsg.Text += ex.ToString();
                    lblMsg.Text += Environment.NewLine;
                    return;
                }

                encrypted = CryptographicEngine.Encrypt(keyPair, buffer, null);
                lblMsg.Text += algorithmName + " encrypted: " + CryptographicBuffer.EncodeToHexString(encrypted) + " (" + encrypted.Length + ")";
                lblMsg.Text += Environment.NewLine;

                blobPublicKey = keyPair.ExportPublicKey();
                blobKeyPair = keyPair.Export();
                CryptographicKey publicKey = asymmetricAlgorithm.ImportPublicKey(blobPublicKey);
                CryptographicKey keyPair2 = asymmetricAlgorithm.ImportKeyPair(blobKeyPair);

                decrypted = CryptographicEngine.Decrypt(keyPair2, encrypted, null);
                lblMsg.Text += algorithmName + " decrypted: " + CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, decrypted);
                lblMsg.Text += Environment.NewLine;
                lblMsg.Text += Environment.NewLine;
            }
        }
    }
}

二、RSA 數位簽名與驗證簽名

使用私鑰對資料簽名,公開金鑰驗證簽名。支援 RSASIGN_PKCS1_SHA1/256/384/512 與 RSASIGN_PSS_SHA1/256/384/512 共 8 種演算法。流程:建立金鑰對 -> CryptographicEngine.Sign 簽名 -> 匯出公鑰 -> CryptographicEngine.VerifySignature 驗證。

程式碼範例

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 Sign : Page
    {
        public Sign()
        {
            this.InitializeComponent();
        }

        private void btnDemo_Click(object sender, RoutedEventArgs e)
        {
            string plainText = "i am webabcd";
            uint keySize = 2048;
            lblMsg.Text = "原文: " + plainText;
            lblMsg.Text += Environment.NewLine;
            lblMsg.Text += "keySize: " + keySize / 8;
            lblMsg.Text += Environment.NewLine;
            lblMsg.Text += Environment.NewLine;

            string[] algorithmNames = { "RSASIGN_PKCS1_SHA1", "RSASIGN_PKCS1_SHA256", "RSASIGN_PKCS1_SHA384", "RSASIGN_PKCS1_SHA512", "RSASIGN_PSS_SHA1", "RSASIGN_PSS_SHA256", "RSASIGN_PSS_SHA384", "RSASIGN_PSS_SHA512" };
            foreach (var algorithmName in algorithmNames)
            {
                IBuffer buffer = CryptographicBuffer.ConvertStringToBinary(plainText, BinaryStringEncoding.Utf8);
                IBuffer blobPublicKey;
                IBuffer blobKeyPair;
                CryptographicKey keyPair;

                AsymmetricKeyAlgorithmProvider asymmetricAlgorithm = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(algorithmName);
                try
                {
                    keyPair = asymmetricAlgorithm.CreateKeyPair(keySize);
                }
                catch (Exception ex)
                {
                    lblMsg.Text += ex.ToString();
                    lblMsg.Text += Environment.NewLine;
                    return;
                }

                IBuffer signature = CryptographicEngine.Sign(keyPair, buffer);
                lblMsg.Text += algorithmName + " - 原文已被簽名,簽名後的資料: " + CryptographicBuffer.EncodeToHexString(signature);
                lblMsg.Text += Environment.NewLine;

                blobPublicKey = keyPair.ExportPublicKey();
                blobKeyPair = keyPair.Export();
                CryptographicKey publicKey = asymmetricAlgorithm.ImportPublicKey(blobPublicKey);

                bool isAuthenticated = CryptographicEngine.VerifySignature(publicKey, buffer, signature);
                lblMsg.Text += "簽名驗證的結果: " + isAuthenticated;
                lblMsg.Text += Environment.NewLine;
                lblMsg.Text += Environment.NewLine;
            }
        }
    }
}

三、CryptographicBuffer:資料格式轉換瑞士刀

CryptographicBuffer 提供靜態方法實現以下轉換:

  • 字串 ↔ 二進位緩衝區 (ConvertStringToBinary / ConvertBinaryToString),支援 UTF-8、UTF-16LE、UTF-16BE。
  • 二進位緩衝區 ↔ 位元組陣列 (CopyToByteArray / CreateFromByteArray)。
  • Base64 字串 ↔ 二進位緩衝區 (DecodeFromBase64String / EncodeToBase64String)。
  • 十六進位字串 ↔ 二進位緩衝區 (DecodeFromHexString / EncodeToHexString)。
  • 產生隨機數 (GenerateRandomNumber / GenerateRandom)。
  • 比較兩個緩衝區是否相等 (Compare)。

API 速查

// 將 IBuffer 轉為 string
string CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding encoding, IBuffer buffer);
// 將 string 轉為 IBuffer
IBuffer CryptographicBuffer.ConvertStringToBinary(string value, BinaryStringEncoding encoding);
// IBuffer 複製到 byte[]
void CryptographicBuffer.CopyToByteArray(IBuffer buffer, out byte[] value);
// byte[] 建立 IBuffer
IBuffer CryptographicBuffer.CreateFromByteArray(byte[] value);
// Base64 字串解碼為 IBuffer
IBuffer CryptographicBuffer.DecodeFromBase64String(string value);
// Hex 字串解碼為 IBuffer
IBuffer CryptographicBuffer.DecodeFromHexString(string value);
// IBuffer 編碼為 Base64 字串
string CryptographicBuffer.EncodeToBase64String(IBuffer buffer);
// IBuffer 編碼為 Hex 字串
string CryptographicBuffer.EncodeToHexString(IBuffer buffer);
// 產生隨機 uint
uint CryptographicBuffer.GenerateRandomNumber();
// 產生指定長度隨機 IBuffer
IBuffer CryptographicBuffer.GenerateRandom(uint length);
// 比較兩個 IBuffer
bool CryptographicBuffer.Compare(IBuffer object1, IBuffer object2);

總結

  • RSA 非對稱加密適合金鑰交換或少量機密資料,需注意填充模式對原文長度的限制。
  • 數位簽名確保資料完整性與來源不可否認性,PKCS#1 v1.5 與 PSS 兩大族群任選。
  • CryptographicBuffer 是密碼編譯開發中的核心輔助類,掌握其轉換方法能大幅簡化編碼/解碼程式碼。

[原始碼下載]:https://files.cnblogs.com/webabcd/Windows8.rar

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved