• 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);
            }
        }
    }
    

      

  • 相关阅读:
    maven本地添加Oracle包
    tomcat启动时检测到循环继承而栈溢出的问题:Caused by: java.lang.IllegalStateException: Unable to complete the scan for annotations for web application [/test] due to a StackOverflowError. Possible root causes include
    C# LINQ list遍历并组装返回新查询
    windows server 2016下360wifi安装
    Python获取本机多IP并指定出口IP
    python读取excel和读取excel图片总结
    windows2012/2016/2019 iis自带ftp被动端口修改
    Flutter IOS build成功,archive失败
    centos常用操作
    Git相关操作
  • 原文地址:https://www.cnblogs.com/xuhongfei/p/2828466.html
Copyright © 2020-2023  润新知