• c#对输入的字符串加密


     #region ========MD5加密========
        public  string  MD5Encryption(string userPassword)
        {
            string MD5EncrypPassword = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(userPassword, "MD5");//202CB962AC59075B964B07152D234B70
            return MD5EncrypPassword;

        }
        #endregion
        #region ========SHA1加密========
        public string SHA1Encryption(string userPassword)
        {
            string SHA1EncrypPassword = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(userPassword, "SHA1");


             return SHA1EncrypPassword;
        }
        #endregion
        #region ========DES加密========
        public string DESEncryption(string userPassword,byte[] Key,byte[] Iv)
        {
            Byte[] password = Encoding.ASCII.GetBytes(userPassword);
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            MemoryStream ms = new MemoryStream();
            ICryptoTransform iCryrt = des.CreateEncryptor(Key, Iv);
            CryptoStream cs = new CryptoStream(ms, iCryrt, CryptoStreamMode.Write);
            cs.Write(password, 0, userPassword.Length);
            cs.FlushFinalBlock();
            return Convert.ToBase64String(ms.ToArray());
        }
        #endregion
        #region ========TripleDES加密========
        public string TripleEncryption(string userPassword, string  Key)
        {
            byte[] bytes = Encoding.Unicode.GetBytes(userPassword);
            byte[] keys = ASCIIEncoding.ASCII.GetBytes(Key);
            TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
            tripleDES.Key = keys;  //长度必须为16位或则是24位
            tripleDES.Mode = CipherMode.ECB; //设置运算模式
            ICryptoTransform transform= tripleDES.CreateEncryptor();
            return Convert.ToBase64String(transform.TransformFinalBlock (bytes, 0, bytes.Length));
        }

        #endregion



    static void Main(string[] args)
            {
                string password = "123";
                Byte[] key={12,23,34,45,56,20,35,12};
                Byte[] iv={120,230,110,50,30,11,12,13};
                string  tt = "1234567890123456";
                Encryptions ss = new Encryptions();
                //string dnPword = ss.DESEncryption(password, key, iv);
                string ds = ss.TripleEncryption(password, tt);
            }

  • 相关阅读:
    少壮不努力,老大徒伤悲
    吾日三省吾身
    记录生活
    为人处世
    时间不等人
    博客两年记忆
    抬起头吧
    下一步计划
    寻找遗失的平静
    暑假第二十六测
  • 原文地址:https://www.cnblogs.com/umlzhang/p/1560014.html
Copyright © 2020-2023  润新知