• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
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);
        }
    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#中ArrayList的使用方法发布时间:2022-07-10
下一篇:
C#DES加密与解密算法实例发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap