C#AES加密

來源:互聯網
上載者:User

標籤:例子   style   指定   image   字串轉換   end   alt   進位   解密   

今天寫個介面,要求如下,需要對應的AES加密(128位),例子php的

C#實現

/// <summary>        ///  AES 加密 輸出hex格式        /// </summary>        /// <param name="str">明文(待加密)</param>        /// <param name="key">密文</param>        /// <returns></returns>        public static string AesEncrypt(string str, string key)        {            if (string.IsNullOrEmpty(str)) return null;            Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);            System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged            {                Key = Encoding.UTF8.GetBytes(key),                Mode = System.Security.Cryptography.CipherMode.ECB,                Padding = System.Security.Cryptography.PaddingMode.Zeros //加密什麼格式對應改為就行,基本的話應該為PKCS7            };            System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);            //return Convert.ToBase64String(resultArray, 0, resultArray.Length);            return ByteArrayToHexString(resultArray);        }    /// <summary>    /// 將一個byte數群組轉換成一個格式化的16進位字串    /// </summary>    /// <param name="data">byte數組</param>    /// <returns>格式化的16進位字串</returns>    public static string ByteArrayToHexString(byte[] data)    {        StringBuilder sb = new StringBuilder(data.Length * 3);        foreach (byte b in data)        {            //16進位數字            sb.Append(Convert.ToString(b, 16).PadLeft(2, ‘0‘));            //16進位數字之間以空格隔開            //sb.Append(Convert.ToString(b, 16).PadLeft(2, ‘0‘).PadRight(3, ‘ ‘));        }        return sb.ToString().ToUpper();    }
     /// <summary>        ///  AES 解密 輸出hex格式        /// </summary>        /// <param name="str">明文(待解密)</param>        /// <param name="key">密文</param>        /// <returns></returns>        public static string AesDecrypt(string str, string key)        {            if (string.IsNullOrEmpty(str)) return null;            //Byte[] toEncryptArray = Convert.FromBase64String(str);            byte[] toEncryptArray = HexStringToByteArray(str);            System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged            {                Key = Encoding.UTF8.GetBytes(key),                Mode = System.Security.Cryptography.CipherMode.ECB,                Padding = System.Security.Cryptography.PaddingMode.Zeros            };            System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateDecryptor();            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);            return Encoding.UTF8.GetString(resultArray);        }                  /// <summary>    /// 將指定的16進位字串轉換為byte數組    /// </summary>    /// <param name="s">16進位字串(如:“7F 2C 4A”或“7F2C4A”都可以)</param>    /// <returns>16進位字串對應的byte數組</returns>    public static byte[] HexStringToByteArray(string s)    {        s = s.Replace(" ", "");        byte[] buffer = new byte[s.Length / 2];        for (int i = 0; i < s.Length; i += 2)            buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);        return buffer;    }

輸出為Base64將我注釋的取消就行,這個是因為需求要的是hex格式

 

C#AES加密

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.