• C#中使用DES和AES加密解密


    using System;
    using System.Text;
    using System.Security.Cryptography;
    using System.IO;
    
    namespace MyCryptography
    {
        ///
        /// DES加密解密
        ///
        public class DES
        {
            ///
            /// 获取密钥
            ///
            private static string Key
            {
                get { return @"P@+#wG+Z"; }
            }
    
            ///
            /// 获取向量
            ///
            private static string IV
            {
                get { return @"L%n67}G/Mk@k%:~Y"; }
            }
    
            ///
            /// DES加密
            ///
            /// 明文字符串
            /// 密文
            public static string DESEncrypt(string plainStr)
            {
                byte[] bKey = Encoding.UTF8.GetBytes(Key);
                byte[] bIV = Encoding.UTF8.GetBytes(IV);
                byte[] byteArray = Encoding.UTF8.GetBytes(plainStr);
    
                string encrypt = null;
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                try
                {
                    using (MemoryStream mStream = new MemoryStream())
                    {
                        using (CryptoStream cStream = new CryptoStream(mStream, des.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write))
                        {
                            cStream.Write(byteArray, 0, byteArray.Length);
                            cStream.FlushFinalBlock();
                            encrypt = Convert.ToBase64String(mStream.ToArray());
                        }
                    }
                }
                catch { }
                des.Clear();
    
                return encrypt;
            }
    
            ///
            /// DES解密
            ///
            /// 密文字符串
            /// 明文
            public static string DESDecrypt(string encryptStr)
            {
                byte[] bKey = Encoding.UTF8.GetBytes(Key);
                byte[] bIV = Encoding.UTF8.GetBytes(IV);
                byte[] byteArray = Convert.FromBase64String(encryptStr);
    
                string decrypt = null;
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                try
                {
                    using (MemoryStream mStream = new MemoryStream())
                    {
                        using (CryptoStream cStream = new CryptoStream(mStream, des.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write))
                        {
                            cStream.Write(byteArray, 0, byteArray.Length);
                            cStream.FlushFinalBlock();
                            decrypt = Encoding.UTF8.GetString(mStream.ToArray());
                        }
                    }
                }
                catch { }
                des.Clear();
    
                return decrypt;
            }
        }
    
        ///
        /// AES加密解密
        ///
        public class AES
        {
            ///
            /// 获取密钥
            ///
            private static string Key
            {
                get { return @")O[NB]6,YF}+efcaj{+oESb9d8>Z'e9M"; }
            }
    
            ///
            /// 获取向量
            ///
            private static string IV
            {
                get { return @"L+/~f4,Ir)b$=pkf"; }
            }
    
            ///
            /// AES加密
            ///
            /// 明文字符串
            /// 密文
            public static string AESEncrypt(string plainStr)
            {
                byte[] bKey = Encoding.UTF8.GetBytes(Key);
                byte[] bIV = Encoding.UTF8.GetBytes(IV);
                byte[] byteArray = Encoding.UTF8.GetBytes(plainStr);
    
                string encrypt = null;
                Rijndael aes = Rijndael.Create();
                try
                {
                    using (MemoryStream mStream = new MemoryStream())
                    {
                        using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write))
                        {
                            cStream.Write(byteArray, 0, byteArray.Length);
                            cStream.FlushFinalBlock();
                            encrypt = Convert.ToBase64String(mStream.ToArray());
                        }
                    }
                }
                catch { }
                aes.Clear();
    
                return encrypt;
            }
    
            ///
            /// AES加密
            ///
            /// 明文字符串
            /// 加密失败时是否返回 null,false 返回 String.Empty
            /// 密文
            public static string AESEncrypt(string plainStr, bool returnNull)
            {
                string encrypt = AESEncrypt(plainStr);
                return returnNull ? encrypt : (encrypt == null ? String.Empty : encrypt);
            }
    
            ///
            /// AES解密
            ///
            /// 密文字符串
            /// 明文
            public static string AESDecrypt(string encryptStr)
            {
                byte[] bKey = Encoding.UTF8.GetBytes(Key);
                byte[] bIV = Encoding.UTF8.GetBytes(IV);
                byte[] byteArray = Convert.FromBase64String(encryptStr);
    
                string decrypt = null;
                Rijndael aes = Rijndael.Create();
                try
                {
                    using (MemoryStream mStream = new MemoryStream())
                    {
                        using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write))
                        {
                            cStream.Write(byteArray, 0, byteArray.Length);
                            cStream.FlushFinalBlock();
                            decrypt = Encoding.UTF8.GetString(mStream.ToArray());
                        }
                    }
                }
                catch { }
                aes.Clear();
    
                return decrypt;
            }
    
            ///
            /// AES解密
            ///
            /// 密文字符串
            /// 解密失败时是否返回 null,false 返回 String.Empty
            /// 明文
            public static string AESDecrypt(string encryptStr, bool returnNull)
            {
                string decrypt = AESDecrypt(encryptStr);
                return returnNull ? decrypt : (decrypt == null ? String.Empty : decrypt);
            }
        }
    }
    

      

  • 相关阅读:
    Perl的运算符号字符
    windows xp 使用远程桌面时的关机/重新启动方法
    抵御TCP的洪水
    远程桌面连接中的常见问题 连接上就断开
    批量kill mysql进程
    Linux如何查看硬盘型号和缓存
    Apache Rewrite 规则详解
    nginx 内置变量大全
    大数据量分页存储过程效率测试附代码
    ASP.Net 更新页面输出缓存的几种方法(包括用户控件,iframe,页面缓存等)
  • 原文地址:https://www.cnblogs.com/xuhongfei/p/2828466.html
Copyright © 2020-2023  润新知