• C# 加解密算法


     

    算法介绍

    3DES又称Triple DES,是DES加密算法的一种模式,它使用3条56位的密钥
    数据进行三次加密。数据加密标准(DES)是美国的一种由来已久的加密标准,它使用对称密钥加密法,并于1981年被ANSI组织规范为ANSI X.3.92。DES使用56位密钥和密码块的方法,而在密码块的方法中,文本被分成64位大小的文本块然后再进行加密。比起最初的DES,3DES更为安全。
     
    3DES(即Triple DES)是DES向AES过渡的加密算法(1999年,NIST将3-DES指定为过渡的加密标准),加密算法,其具体实现如下:设Ek()和Dk()代表DES算法的加密和解密过程,K代表DES算法使用的密钥,P代表明文,C代表密文,这样:
     
    3DES加密过程为:C=Ek3(Dk2(Ek1(P)))
    3DES解密过程为:P=Dk1(EK2(Dk3(C)))    
                                                                                                                               

    DES加解密

    key 是八位长的字符串

     #region Des 加解密
    
            public static string DesEncrypt(string encryptStr, string key)
            {
                try
                {
                    var inputArry = Encoding.Default.GetBytes(encryptStr);
                    var byKey = Encoding.ASCII.GetBytes(key);
                    var byIv = Encoding.ASCII.GetBytes(key);
                    var ms = new MemoryStream();
                    using (var cryptoProvider = new DESCryptoServiceProvider())
                    {
                        using (var cs = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIv), CryptoStreamMode.Write))
                        {
                            cs.Write(inputArry, 0, inputArry.Length);
                            cs.FlushFinalBlock();
                            cs.Close();
                        }
                    }
    
                    var str = Convert.ToBase64String(ms.ToArray());
                    ms.Close();
                    return str;
                }
                catch (Exception)
                {
                    return encryptStr;
                }
    
            }
    
            public static string DesDecrypt(string decryptStr, string key)
            {
                try
                {
                    var inputArry = Convert.FromBase64String(decryptStr);
                    var byKey = Encoding.Default.GetBytes(key);
                    var byIv = Encoding.Default.GetBytes(key);
                    var ms = new MemoryStream();
                    using (var cryptProvider = new DESCryptoServiceProvider())
                    {
                        using (var cs = new CryptoStream(ms, cryptProvider.CreateDecryptor(byKey, byIv), CryptoStreamMode.Write))
                        {
                            cs.Write(inputArry, 0, inputArry.Length);
                            cs.FlushFinalBlock();
                            cs.Close();
                        }
                    }
    
                    var str = Encoding.Default.GetString(ms.ToArray());
                    ms.Close();
                    return str;
                }
                catch (Exception)
                {
                    return decryptStr;
                }
    
            }
    
            #endregion

    3Des加解密

    #region 3DES 加解密
    
            public static string Encrypt3Des(string encryStr, string key)
            {
                try
                {
                    var inputArry = Encoding.Default.GetBytes(encryStr);
                    var hashmd5 = new MD5CryptoServiceProvider();
                    var byKey = hashmd5.ComputeHash(Encoding.Default.GetBytes(key));
                    var byIv = byKey;
                    var ms = new MemoryStream();
                    using (var tDescryptProvider = new TripleDESCryptoServiceProvider())
                    {
                        tDescryptProvider.Mode = CipherMode.ECB;
                        using (var cs = new CryptoStream(ms, tDescryptProvider.CreateEncryptor(byKey, byIv), CryptoStreamMode.Write))
                        {
                            cs.Write(inputArry, 0, inputArry.Length);
                            cs.FlushFinalBlock();
                            cs.Close();
                        }
                    }
    
                    var str = Convert.ToBase64String(ms.ToArray());
                    ms.Close();
                    return str;
                }
                catch (Exception)
                {
                    return encryStr;
                }
    
            }
    
            public static string Decrypt3Des(string decryStr, string key)
            {
                try
                {
                    var inputArry = Convert.FromBase64String(decryStr);
                    var hashmd5 = new MD5CryptoServiceProvider();
                    var byKey = hashmd5.ComputeHash(Encoding.Default.GetBytes(key));
                    var byIv = byKey;
                    var ms = new MemoryStream();
                    using (var tDescryptProvider = new TripleDESCryptoServiceProvider())
                    {
                        tDescryptProvider.Mode = CipherMode.ECB;
                        using (var cs = new CryptoStream(ms, tDescryptProvider.CreateDecryptor(byKey, byIv), CryptoStreamMode.Write))
                        {
                            cs.Write(inputArry, 0, inputArry.Length);
                            cs.FlushFinalBlock();
                            cs.Close();
                        }
                    }
    
                    var str = Encoding.Default.GetString(ms.ToArray());
                    ms.Close();
                    return str;
                }
                catch (Exception)
                {
                    return decryStr;
                }
            }
    
    
            #endregion
  • 相关阅读:
    openVolumeMesh example 程序学习
    使用字符串创建java 对象
    HDU-1501-Zipper
    UVA-10285-Longest Run on a Snowboard
    HDU-2182-Frog
    HDU-2044-一只小蜜蜂
    POJ-1163-The Triangle
    HDU-1159-Common Subsequence
    HDU-2069-Coin Change
    HDU-4864-Task
  • 原文地址:https://www.cnblogs.com/qiufengke/p/5292359.html
Copyright © 2020-2023  润新知