【.NET】.NET加密与解密C#

文章目录

    • 1、加密
    • 2、解密


1、加密

public class Encrypts
    {
        ///    
        ///   构造方法 
        ///    
        public Encrypts()
        {
        }
        ///    
        ///   使用缺省密钥字符串加密 
        ///    
        ///    明文  
        ///    密文  
        public static string Encrypt(string original)
        {
            return Encrypt(original, "haha");
        }
        ///    
        ///   使用缺省密钥解密 
        ///    
        ///    密文  
        ///    明文  
        public static string Decrypt(string original)
        {
            try
            {
                return Decrypt(original, "haha", System.Text.Encoding.Default);
            }
            catch
            {
                return ("");
            }
        }
        ///    
        ///   使用给定密钥解密 
        ///    
        ///    密文  
        ///    密钥  
        ///    明文  
        public static string Decrypt(string original, string key)
        {
            return Decrypt(original, key, System.Text.Encoding.Default);
        }
        ///    
        ///   使用缺省密钥解密,返回指定编码方式明文 
        ///    
        ///    密文  
        ///    编码方式  
        ///    明文  
        public static string Decrypt(string original, Encoding encoding)
        {
            return Decrypt(original, "haha", encoding);
        }
        ///    
        ///   使用给定密钥加密 
        ///    
        ///    原始文字  
        ///    密钥  
        ///    字符编码方案  
        ///    密文  
        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));
        }

        ///    
        ///   使用给定密钥解密 
        ///    
        ///    密文  
        ///    密钥  
        ///    字符编码方案  
        ///    明文  
        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));
        }
        ///    
        ///   生成MD5摘要 
        ///    
        ///    数据源  
        ///    摘要  
        public static byte[] MakeMD5(byte[] original)
        {
            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            byte[] keyhash = hashmd5.ComputeHash(original);
            hashmd5 = null;
            return keyhash;
        }

        ///    
        ///   使用给定密钥加密 
        ///    
        ///    明文  
        ///    密钥  
        ///    密文  
        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);
        }

        ///    
        ///   使用给定密钥解密数据 
        ///    
        ///    密文  
        ///    密钥  
        ///    明文  
        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);
        }

        ///    
        ///   使用给定密钥加密 
        ///    
        ///    原始数据  
        ///    密钥  
        ///    密文  
        public static byte[] Encrypt(byte[] original)
        {
            byte[] key = System.Text.Encoding.Default.GetBytes("haha");
            return Encrypt(original, key);
        }

        ///    
        ///   使用缺省密钥解密数据 
        ///    
        ///    密文  
        ///    密钥  
        ///    明文  
        public static byte[] Decrypt(byte[] encrypted)
        {
            byte[] key = System.Text.Encoding.Default.GetBytes("haha");
            return Decrypt(encrypted, key);
        }

    }

2、解密

public class EncryDecry
    {

        #region 加密

        /// 
        /// 加密
        /// 
        /// 明文
        /// Key_64 长度8位
        /// Key_64 长度8位
        /// 

        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 解密

        
        /// 
        /// 解密
        /// 
        /// 密文
        /// Key_64 长度8位
        /// Key_64 长度8位
        /// 
        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

    }

你可能感兴趣的:(#,.NET全栈,.net,c#,java)