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


    C#中使用DES和AES加密解密
    2008-01-12 09:37

    using System;
    using System.Text;
    using System.Security.Cryptography;
    using System.IO;

    namespace MyCryptography
    {
        /// <summary>
        /// DES加密解密
        /// </summary>
        public class DES
        {
            /// <summary>
            /// 获取密钥
            /// </summary>
            private static string Key
            {
                get { return @"P@+#wG+Z"; }
            }

            /// <summary>
            /// 获取向量
            /// </summary>
            private static string IV
            {
                get { return @"L%n67}G/Mk@k%:~Y"; }
            }

            /// <summary>
            /// DES加密
            /// </summary>
            /// <param name="plainStr">明文字符串</param>
            /// <returns>密文</returns>
            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;
            }

            /// <summary>
            /// DES解密
            /// </summary>
            /// <param name="encryptStr">密文字符串</param>
            /// <returns>明文</returns>
            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;
            }
        }

        /// <summary>
        /// AES加密解密
        /// </summary>
        public class AES
        {
            /// <summary>
            /// 获取密钥
            /// </summary>
            private static string Key
            {
                get { return @")O[NB]6,YF}+efcaj{+oESb9d8>Z'e9M"; }
            }

            /// <summary>
            /// 获取向量
            /// </summary>
            private static string IV
            {
                get { return @"L+/~f4,Ir)b$=pkf"; }
            }

            /// <summary>
            /// AES加密
            /// </summary>
            /// <param name="plainStr">明文字符串</param>
            /// <returns>密文</returns>
            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;
            }

            /// <summary>
            /// AES加密
            /// </summary>
            /// <param name="plainStr">明文字符串</param>
            /// <param name="returnNull">加密失败时是否返回 null,false 返回 String.Empty</param>
            /// <returns>密文</returns>
            public static string AESEncrypt(string plainStr, bool returnNull)
            {
                string encrypt = AESEncrypt(plainStr);
                return returnNull ? encrypt : (encrypt == null ? String.Empty : encrypt);
            }

            /// <summary>
            /// AES解密
            /// </summary>
            /// <param name="encryptStr">密文字符串</param>
            /// <returns>明文</returns>
            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;
            }

            /// <summary>
            /// AES解密
            /// </summary>
            /// <param name="encryptStr">密文字符串</param>
            /// <param name="returnNull">解密失败时是否返回 null,false 返回 String.Empty</param>
            /// <returns>明文</returns>
            public static string AESDecrypt(string encryptStr, bool returnNull)
            {
                string decrypt = AESDecrypt(encryptStr);
                return returnNull ? decrypt : (decrypt == null ? String.Empty : decrypt);
            }
        }
    }
  • 相关阅读:
    指针与数组关联导致的一些现象 分类: H_HISTORY 20130211 20:14 516人阅读 评论(0) 收藏
    宏定义一些内容 分类: H_HISTORY 20130207 23:20 585人阅读 评论(0) 收藏
    使用lstat()判断文件类型 分类: H_HISTORY 20130224 11:48 703人阅读 评论(0) 收藏
    关于VMware虚拟机的上网 分类: C_OHTERS 20130220 14:36 336人阅读 评论(0) 收藏
    Segmentation fault (core dumped) 分类: H_HISTORY 20130206 11:34 18800人阅读 评论(0) 收藏
    C语言内存分配时间 分类: H_HISTORY 20130211 10:51 1432人阅读 评论(3) 收藏
    GTK+与QT的对比 分类: H_HISTORY 20130205 09:27 3101人阅读 评论(0) 收藏
    枚举作为整数 分类: H_HISTORY 20130208 11:22 576人阅读 评论(0) 收藏
    01背包问题,动态规划求解
    求两个字符串的相似度或子串
  • 原文地址:https://www.cnblogs.com/IT-haidong/p/4856848.html
Copyright © 2020-2023  润新知