zoukankan      html  css  js  c++  java
  • Des对称可逆加密

        /// <summary>

        /// DES AES Blowfish

        ///  对称加密算法的优点是速度快,

        ///  缺点是密钥管理不方便,要求共享密钥。

        /// 可逆对称加密  密钥长度8

        /// </summary>

    using System.Security.Cryptography;

      public class DesEncrypt

        {

            //8位长度

            private static string KEY = "ruanmou1";

            private static byte[] rgbKey = ASCIIEncoding.ASCII.GetBytes(KEY.Substring(0, 8));

            private static byte[] rgbIV = ASCIIEncoding.ASCII.GetBytes(KEY.Insert(0, "w").Substring(0, 8));

            /// <summary>

            /// DES 加密

            /// </summary>

            /// <param name="strValue"></param>

            /// <returns></returns>

            public static string Encrypt(string strValue)

            {

                DESCryptoServiceProvider dsp = new DESCryptoServiceProvider();

                using (MemoryStream memStream = new MemoryStream())

                {

                    CryptoStream crypStream = new CryptoStream(memStream, dsp.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);

                    StreamWriter sWriter = new StreamWriter(crypStream);

                    sWriter.Write(strValue);

                    sWriter.Flush();

                    crypStream.FlushFinalBlock();

                    memStream.Flush();

                    return Convert.ToBase64String(memStream.GetBuffer(), 0, (int)memStream.Length);

                }

            }

            /// <summary>

            /// DES解密

            /// </summary>

            /// <param name="EncValue"></param>

            /// <returns></returns>

            public static string Decrypt(string EncValue)

            {

                DESCryptoServiceProvider dsp = new DESCryptoServiceProvider();

                byte[] buffer = Convert.FromBase64String(EncValue);

                using (MemoryStream memStream = new MemoryStream())

                {

                    CryptoStream crypStream = new CryptoStream(memStream, dsp.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);

                    crypStream.Write(buffer, 0, buffer.Length);

                    crypStream.FlushFinalBlock();

                    return ASCIIEncoding.UTF8.GetString(memStream.ToArray());

                }

            }

        }

    string desEn1 = DesEncrypt.Encrypt("张三李四");

     string desDe1 = DesEncrypt.Decrypt(desEn1);

  • 相关阅读:
    ZOJ3861 Valid Pattern Lock
    ZOJ 3866 Cylinder Candy
    hdu 1729 Stone Game SG函数
    hdu 2546 饭卡 01背包
    hdu 2084 数塔
    中国科学院大学生创新实践训练计划-
    中国科技论文在线期刊模板出现了格式问题,怎么解决?
    1015. 德才论 (25)
    1014. 福尔摩斯的约会 (20)
    Ubuntu 14.0的安装及联网
  • 原文地址:https://www.cnblogs.com/anyihen/p/12773184.html
Copyright © 2011-2022 走看看