• .net 加密与解密


    public class Encrypts
        {
            ///   <summary> 
            ///   构造方法 
            ///   </summary> 
            public Encrypts()
            {
            }
            ///   <summary> 
            ///   使用缺省密钥字符串加密 
            ///   </summary> 
            ///   <param   name= "original "> 明文 </param> 
            ///   <returns> 密文 </returns> 
            public static string Encrypt(string original)
            {
                return Encrypt(original, "haha");
            }
            ///   <summary> 
            ///   使用缺省密钥解密 
            ///   </summary> 
            ///   <param   name= "original "> 密文 </param> 
            ///   <returns> 明文 </returns> 
            public static string Decrypt(string original)
            {
                try
                {
                    return Decrypt(original, "haha", System.Text.Encoding.Default);
                }
                catch
                {
                    return ("");
                }
            }
            ///   <summary> 
            ///   使用给定密钥解密 
            ///   </summary> 
            ///   <param   name= "original "> 密文 </param> 
            ///   <param   name= "key "> 密钥 </param> 
            ///   <returns> 明文 </returns> 
            public static string Decrypt(string original, string key)
            {
                return Decrypt(original, key, System.Text.Encoding.Default);
            }
            ///   <summary> 
            ///   使用缺省密钥解密,返回指定编码方式明文 
            ///   </summary> 
            ///   <param   name= "original "> 密文 </param> 
            ///   <param   name= "encoding "> 编码方式 </param> 
            ///   <returns> 明文 </returns> 
            public static string Decrypt(string original, Encoding encoding)
            {
                return Decrypt(original, "haha", encoding);
            }
            ///   <summary> 
            ///   使用给定密钥加密 
            ///   </summary> 
            ///   <param   name= "original "> 原始文字 </param> 
            ///   <param   name= "key "> 密钥 </param> 
            ///   <param   name= "encoding "> 字符编码方案 </param> 
            ///   <returns> 密文 </returns> 
            public static string Encrypt(string original, string key)
            {
                byte[] buff = System.Text.Encoding.Default.GetBytes(original);
                byte[] kb = System.Text.Encoding.Default.GetBytes(key);
                return Convert.ToBase64String(Encrypt(buff, kb));
            }
    
            ///   <summary> 
            ///   使用给定密钥解密 
            ///   </summary> 
            ///   <param   name= "encrypted "> 密文 </param> 
            ///   <param   name= "key "> 密钥 </param> 
            ///   <param   name= "encoding "> 字符编码方案 </param> 
            ///   <returns> 明文 </returns> 
            public static string Decrypt(string encrypted, string key, Encoding encoding)
            {
                byte[] buff = Convert.FromBase64String(encrypted);
                byte[] kb = System.Text.Encoding.Default.GetBytes(key);
                return encoding.GetString(Decrypt(buff, kb));
            }
            ///   <summary> 
            ///   生成MD5摘要 
            ///   </summary> 
            ///   <param   name= "original "> 数据源 </param> 
            ///   <returns> 摘要 </returns> 
            public static byte[] MakeMD5(byte[] original)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                byte[] keyhash = hashmd5.ComputeHash(original);
                hashmd5 = null;
                return keyhash;
            }
    
            ///   <summary> 
            ///   使用给定密钥加密 
            ///   </summary> 
            ///   <param   name= "original "> 明文 </param> 
            ///   <param   name= "key "> 密钥 </param> 
            ///   <returns> 密文 </returns> 
            public static byte[] Encrypt(byte[] original, byte[] key)
            {
                TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
                des.Key = MakeMD5(key);
                des.Mode = CipherMode.ECB;
    
                return des.CreateEncryptor().TransformFinalBlock(original, 0, original.Length);
            }
    
            ///   <summary> 
            ///   使用给定密钥解密数据 
            ///   </summary> 
            ///   <param   name= "encrypted "> 密文 </param> 
            ///   <param   name= "key "> 密钥 </param> 
            ///   <returns> 明文 </returns> 
            public static byte[] Decrypt(byte[] encrypted, byte[] key)
            {
                TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
                des.Key = MakeMD5(key);
                des.Mode = CipherMode.ECB;
    
                return des.CreateDecryptor().TransformFinalBlock(encrypted, 0, encrypted.Length);
            }
    
            ///   <summary> 
            ///   使用给定密钥加密 
            ///   </summary> 
            ///   <param   name= "original "> 原始数据 </param> 
            ///   <param   name= "key "> 密钥 </param> 
            ///   <returns> 密文 </returns> 
            public static byte[] Encrypt(byte[] original)
            {
                byte[] key = System.Text.Encoding.Default.GetBytes("haha");
                return Encrypt(original, key);
            }
    
            ///   <summary> 
            ///   使用缺省密钥解密数据 
            ///   </summary> 
            ///   <param   name= "encrypted "> 密文 </param> 
            ///   <param   name= "key "> 密钥 </param> 
            ///   <returns> 明文 </returns> 
            public static byte[] Decrypt(byte[] encrypted)
            {
                byte[] key = System.Text.Encoding.Default.GetBytes("haha");
                return Decrypt(encrypted, key);
            }
    
        }
    
    public class EncryDecry
        {
    
            #region 加密
    
            /// <summary>
            /// 加密
            /// </summary>
            /// <param name="data">明文</param>
            /// <param name="Key_64">Key_64 长度8位</param>
            /// <param name="Iv_64">Key_64 长度8位</param>
            /// <returns></returns>
    
            public static string Encrypt(string data, string Key_64, string Iv_64)
            {
    
                string KEY_64 = Key_64;// "12345678";
    
                string IV_64 = Iv_64;// "12345678";
    
                try
                {
    
                    byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
    
                    byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
    
                    DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
    
                    int i = cryptoProvider.KeySize;
    
                    MemoryStream ms = new MemoryStream();
    
                    CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
    
                    StreamWriter sw = new StreamWriter(cst);
    
                    sw.Write(data);
    
                    sw.Flush();
    
                    cst.FlushFinalBlock();
    
                    sw.Flush();
    
                    return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
    
                }
    
                catch (Exception x)
                {
    
                    return x.Message;
    
                }
    
            }
    
            #endregion
    
    
            #region 解密
    
            
            /// <summary>
            /// 解密
            /// </summary>
            /// <param name="data">密文</param>
            /// <param name="Key_64">Key_64 长度8位</param>
            /// <param name="Iv_64">Key_64 长度8位</param>
            /// <returns></returns>
            public static string Decrypt(string data, string Key_64, string Iv_64)
            {
    
                string KEY_64 = Key_64;// "12345678";密钥
    
                string IV_64 = Iv_64;// "12345678"; 向量
    
                try
                {
    
                    byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
    
                    byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
    
                    byte[] byEnc;
    
                    byEnc = Convert.FromBase64String(data); //把需要解密的字符串转为8位无符号数组
    
                    DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
    
                    MemoryStream ms = new MemoryStream(byEnc);
    
                    CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
    
                    StreamReader sr = new StreamReader(cst);
    
                    return sr.ReadToEnd();
    
                }
    
                catch (Exception x)
                {
    
                    return x.Message;
    
                }
    
            }
    
            #endregion
    
        }
  • 相关阅读:
    python(5)python安装for Mac
    jmeter(16)安装jmeter插件jar包 pluginsmanager.jar
    jmeter(17)参数化之csv随机参数化【Random CSV Data Set Config】
    jmeter(14)参数化之csv文件设置
    jmeter(11)jdbc请求及请求后的响应结果如何提取
    pku2192(并查集+枚举)
    stl中的next_permutation算法
    pku1204 Word Puzzles
    hdu1075 What Are You Talking About
    pku1733 Parity game(离散化+并查集拓展应用)
  • 原文地址:https://www.cnblogs.com/zoro-zero/p/4401561.html
Copyright © 2020-2023  润新知