[.NET]產生AES的Key與IV
前言        
之前一篇文章:[ASP.NET]AES加解密,裡面提到了如何使用RijndaelManaged來做AES的加解密。       
        
這篇文章則是memo,如何產生AES加解密用的Key與IV值。        
    
說明
- 當new RijndaelManaged()的時候,該類別就會自動產生一組Key與IV在其Key屬性與IV屬性。
 - Key屬性與IV屬性為byte[],Key為byte[32], IV為byte[16]。而要呈現或記錄時,通常會將byte[]轉成Base64的字串。
 
Sample Code
    /// 產生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; }
    }
畫面
  
結論
          
只是個Memo,以免自己忘記,也提供給各位在使用.NET的RijndaelManaged類別時,當個參考。
blog 與課程更新內容,請前往新站位置:http://tdd.best/
