• C#加密解密(AES)


    using System;
    
    namespace Encrypt
    {
        public class AESHelper
        {
            /// <summary>
            /// 默认密钥-密钥的长度必须是32
            /// </summary>
            private const string PublicKey = "1234567890123456";
    
            /// <summary>
            /// 默认向量
            /// </summary>
            private const string Iv = "abcdefghijklmnop";
            /// <summary>  
            /// AES加密  
            /// </summary>  
            /// <param name="str">需要加密字符串</param>  
            /// <returns>加密后字符串</returns>  
            public static String Encrypt(string str)
            {
                return Encrypt(str, PublicKey);
            }
    
            /// <summary>  
            /// AES解密  
            /// </summary>  
            /// <param name="str">需要解密字符串</param>  
            /// <returns>解密后字符串</returns>  
            public static String Decrypt(string str)
            {
                return Decrypt(str, PublicKey);
            }
            /// <summary>
            /// AES加密
            /// </summary>
            /// <param name="str">需要加密的字符串</param>
            /// <param name="key">32位密钥</param>
            /// <returns>加密后的字符串</returns>
            public static string Encrypt(string str, string key)
            {
                Byte[] keyArray = System.Text.Encoding.UTF8.GetBytes(key);
                Byte[] toEncryptArray = System.Text.Encoding.UTF8.GetBytes(str);
                var rijndael = new System.Security.Cryptography.RijndaelManaged();
                rijndael.Key = keyArray;
                rijndael.Mode = System.Security.Cryptography.CipherMode.ECB;
                rijndael.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
                rijndael.IV = System.Text.Encoding.UTF8.GetBytes(Iv);
                System.Security.Cryptography.ICryptoTransform cTransform = rijndael.CreateEncryptor();
                Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                return Convert.ToBase64String(resultArray, 0, resultArray.Length);
            }
            /// <summary>
            /// AES解密
            /// </summary>
            /// <param name="str">需要解密的字符串</param>
            /// <param name="key">32位密钥</param>
            /// <returns>解密后的字符串</returns>
            public static string Decrypt(string str, string key)
            {
                Byte[] keyArray = System.Text.Encoding.UTF8.GetBytes(key);
                Byte[] toEncryptArray = Convert.FromBase64String(str);
                var rijndael = new System.Security.Cryptography.RijndaelManaged();
                rijndael.Key = keyArray;
                rijndael.Mode = System.Security.Cryptography.CipherMode.ECB;
                rijndael.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
                rijndael.IV = System.Text.Encoding.UTF8.GetBytes(Iv);
                System.Security.Cryptography.ICryptoTransform cTransform = rijndael.CreateDecryptor();
                Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                return System.Text.Encoding.UTF8.GetString(resultArray);
            }
        }
    }
  • 相关阅读:
    Get Date information with a datetime var
    深入浅出Eclipse Modeling Framework (EMF)
    [转]How to Create a Thumbnail Picture Library View in SharePoint 2007
    Moss2007 view 过滤 当前用户 Custom List View (Filter by User [Me])
    moss 2007 自定义NewForm.aspx 注意事项 报错Unable to display this Web Part
    [转]Hiding the Search Scopes DropDown in WSSv3/MOSS 2007
    moss2007 新建模板页 搜索框无法使用 SmallSearchInputBox
    [转]SharePoint SearchasYouType with jQuery
    [转]Moss2007 Customize the NewForm.aspx 自定义NewForm EditForm页面
    Moss2007 xslt DataFormWebPart 展示图片库所有图片(文档库所有文档)
  • 原文地址:https://www.cnblogs.com/zhaojingwei/p/10369440.html
Copyright © 2020-2023  润新知