public void Crypto()
        {
            var encData = aes.Encrypt("1");
            var str     = aes.Decrypt(encData, mode: CipherMode.ECB);

            Assert.AreEqual("1", str);
        }
Example #2
0
        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="aes"></param>
        /// <param name="str">待加密的字符串</param>
        /// <param name="encoding">待加密的字符串编码格式</param>
        /// <param name="key">加密密钥,为空则使用实体属性Key</param>
        /// <param name="mode">加密模式,为空则使用实体属性Mode</param>
        /// <param name="padding">填充算法,为空则使用实体属性Padding</param>
        /// <returns>base64编码格式加密字符串</returns>
        public static string Encrypt(this System.Security.Cryptography.Aes aes,
                                     string str, Encoding encoding, byte[] key = null, CipherMode?mode = null, PaddingMode?padding = null)
        {
            var data    = encoding.GetBytes(str);
            var encData = aes.Encrypt(data, key, mode, padding);

            return(Convert.ToBase64String(encData));
        }
Example #3
0
 /// <summary>
 /// AES加密
 /// </summary>
 /// <param name="aes"></param>
 /// <param name="str">待加密的字符串(UTF8编码)</param>
 /// <param name="key">加密密钥,为空则使用实体属性Key</param>
 /// <param name="mode">加密模式,为空则使用实体属性Mode</param>
 /// <param name="padding">填充算法,为空则使用实体属性Padding</param>
 /// <returns>base64编码格式加密字符串</returns>
 public static string Encrypt(this System.Security.Cryptography.Aes aes,
                              string str, byte[] key = null, CipherMode?mode = null, PaddingMode?padding = null)
 {
     return(aes.Encrypt(str, Encoding.UTF8, key, mode, padding));
 }