zoukankan      html  css  js  c++  java
  • C# DES加密

    C#的DES加密,一个demo记录一下。

    DES加密管理类

        /// <summary>
        /// DES加密管理类
        /// </summary>
        public class DESEncryptHelper
        {
            private static string DES_Key = "****************";
            /// <summary>
            /// DES加密
            /// </summary>
            /// <param name="str">需要加密的</param>
            /// <returns></returns>
            public static string Encrypt(string str)
            {
                if (string.IsNullOrEmpty(str))
                {
                    return "加密处理失败,加密字符串为空";
                }
                //加密秘钥补位处理
                string encryptKeyall = Convert.ToString(DES_Key);    //定义密钥  
                if (encryptKeyall.Length < 9)
                {
                    for (; ; )
                    {
                        if (encryptKeyall.Length < 9)
                            encryptKeyall += encryptKeyall;
                        else
                            break;
                    }
                }
                string encryptKey = encryptKeyall.Substring(0, 8);
                DES_Key = encryptKey;
    
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Encoding.Default.GetBytes(str);
                des.Key = ASCIIEncoding.UTF8.GetBytes(DES_Key); // 密匙
                des.IV = ASCIIEncoding.UTF8.GetBytes(DES_Key);  // 向量
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                var result = Convert.ToBase64String(ms.ToArray());
                return result;
            }
    
            /// <summary>
            /// DES解密
            /// </summary>
            /// <param name="str">需要解密的</param>
            /// <returns></returns>
            public static string Decrypt(string str)
            {
                if (string.IsNullOrEmpty(str))
                {
                    return "解密处理失败,解密字符串为空";
                }
                //解密秘钥补位处理
                string encryptKeyall = Convert.ToString(DES_Key);
                if (encryptKeyall.Length < 9)
                {
                    for (; ; )
                    {
                        if (encryptKeyall.Length < 9)
                            encryptKeyall += encryptKeyall;
                        else
                            break;
                    }
                }
                string encryptKey = encryptKeyall.Substring(0, 8);
                DES_Key = encryptKey;
                //解密处理
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Convert.FromBase64String(str);
                des.Key = ASCIIEncoding.UTF8.GetBytes(DES_Key);  //秘钥---加密解密秘钥需要一致
                des.IV = ASCIIEncoding.UTF8.GetBytes(DES_Key);   //向量
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                return System.Text.Encoding.Default.GetString(ms.ToArray());
            }
        }

    方法调用:

    string resultStr = DESEncryptHelper.Encrypt(“123456”);
    string resultStr = DESEncryptHelper.Decrypt(“123456”);
    

    学习:

    DES算法原理:https://blog.csdn.net/qq_27570955/article/details/52442092 

    欢迎相互交流学习!

  • 相关阅读:
    Codeforce821E Okabe and El Psy Kongroo
    hihocoder1497 Queen Attack
    hihocoder 1523数据重排
    codeforce 780C Andryusha and Colored Balloons
    codeforce 768B Code For 1
    hihoCoder1270 建造基地 完全背包
    UVA10054 The Necklace 欧拉回路+并查集
    Codeforces Round #480 (Div. 2) C 贪心 D 数字、思维 E 树上倍增
    ZOJ 4029 Now Loading!!! 思维
    西安电子科技大学第16届程序设计竞赛网络同步赛 E dp G 找规律
  • 原文地址:https://www.cnblogs.com/jiayan1578/p/11903789.html
Copyright © 2011-2022 走看看