推薦這個blog:

Award


(ASP.NET 2010、2011、2012年)

其他資源

簡體中文blog

最新回應

[.NET]產生AES的Key與IV

前言
之前一篇文章:
[ASP.NET]AES加解密,裡面提到了如何使用RijndaelManaged來做AES的加解密。

這篇文章則是memo,如何產生AES加解密用的Key與IV值。

說明

  1. 當new RijndaelManaged()的時候,該類別就會自動產生一組Key與IV在其Key屬性與IV屬性。
  2. Key屬性與IV屬性為byte[],Key為byte[32], IV為byte[16]。而要呈現或記錄時,通常會將byte[]轉成Base64的字串。

 

Sample Code

    /// <summary>
    /// 產生AES需要的Key與IV
    /// </summary>
    public static class KeyEntityGenerator
    {
        /// <summary>
        /// 透過RijndaelManaged產生AES的Key與IV,並經過Base64轉換成字串
        /// </summary>
        /// <returns>帶有Key與IV的KeyEntity</returns>
        public static KeyEntity GetKeyEntity()
        {
            var generator = new RijndaelManaged();
            var key = Convert.ToBase64String(generator.Key);
            var iv = Convert.ToBase64String(generator.IV);

            var result = new KeyEntity(key, iv);
            return result;
        }
    }

    public class KeyEntity
    {
        public KeyEntity(string key, string iv);

        public string KeyIV { get; }
        public string KeyValue { get; }
    }

 

畫面

KeyAndIV


結論
只是個Memo,以免自己忘記,也提供給各位在使用.NET的RijndaelManaged類別時,當個參考。


點部落-In Joey

↑ Grab this Headline Animator


關連文章

[ASP.NET]用Linq取CheckBoxList選取項目的值

[.NET]仿Unix Time的TimeStamp

[jQuery]將marquee plugin封裝成User Control

[Powershell]符合條件的檔案備份,並將內容清空

回應

目前沒有回應.

登入後使用進階評論

Please add 2 and 6 and type the answer here: