C# 加解密工具类

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

namespace CloverNet.SecurityTool
{
    public partial class SecurityTool
    {
        public static class AES
        {
            //默认密钥向量   
            private static byte[] _key1 = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

            public static string Encrypt(string originalStr, string secretKey)
            {
                //分组加密算法  
                SymmetricAlgorithm des = Rijndael.Create();
                byte[] inputByteArray = Encoding.UTF8.GetBytes(originalStr);//得到需要加密的字节数组      
                                                                          //设置密钥及密钥向量  
                des.Key = Encoding.UTF8.GetBytes(secretKey);
                des.IV = _key1;
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                byte[] cipherBytes = ms.ToArray();//得到加密后的字节数组  
                cs.Close();
                ms.Close();
                return Convert.ToBase64String(cipherBytes);
            }

            public static string Decrypt(string secretStr, string secretKey)
            {
                var secretbytes = Convert.FromBase64String(secretStr);

                SymmetricAlgorithm des = Rijndael.Create();
                des.Key = Encoding.UTF8.GetBytes(secretKey);
                des.IV = _key1;
                byte[] decryptBytes = new byte[secretbytes.Length];
                MemoryStream ms = new MemoryStream(secretbytes);
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Read);
                cs.Read(decryptBytes, 0, decryptBytes.Length);
                cs.Close();
                ms.Close();
                return Encoding.UTF8.GetString(decryptBytes);
            }

        }
    }
}
using System;

namespace CloverNet.SecurityTool
{
    public partial class SecurityTool
    {
        public static class Base64
        {
            public static string Encrypt(string originalStr)
            {
                var bytes = System.Text.Encoding.UTF8.GetBytes(originalStr);
                return Convert.ToBase64String(bytes);
            }

            public static string Decrypt(string originalStr)
            {
                var bytes = Convert.FromBase64String(originalStr);
                return System.Text.Encoding.UTF8.GetString(bytes);
            }
        }
    }
}
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace CloverNet.SecurityTool
{
    public partial class SecurityTool
    {
        public static class DES
        {
            public static string Encrypt(string originalStr, string secretKey)
            {
                using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
                {
                    byte[] inputByteArray = Encoding.UTF8.GetBytes(originalStr);
                    des.Key = ASCIIEncoding.ASCII.GetBytes(MD5.Encrypt(secretKey).Substring(0, 8));
                    des.IV = ASCIIEncoding.ASCII.GetBytes(MD5.Encrypt(secretKey).Substring(0, 8));
                    System.IO.MemoryStream ms = new System.IO.MemoryStream();
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(inputByteArray, 0, inputByteArray.Length);
                        cs.FlushFinalBlock();
                        cs.Close();
                    }
                    string str = Convert.ToBase64String(ms.ToArray());
                    ms.Close();
                    return str;
                }
            }

            public static string Decrypt(string secretStr, string secretKey)
            {
                byte[] inputByteArray = Convert.FromBase64String(secretStr);
                using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
                {
                    des.Key = ASCIIEncoding.ASCII.GetBytes(MD5.Encrypt(secretKey).Substring(0, 8));
                    des.IV = ASCIIEncoding.ASCII.GetBytes(MD5.Encrypt(secretKey).Substring(0, 8));
                    System.IO.MemoryStream ms = new System.IO.MemoryStream();
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(inputByteArray, 0, inputByteArray.Length);
                        cs.FlushFinalBlock();
                        cs.Close();
                    }
                    string str = Encoding.UTF8.GetString(ms.ToArray());
                    ms.Close();
                    return str;
                }
            }
        }
    }
}
using System.Security.Cryptography;
using System.Text;

namespace CloverNet.SecurityTool
{
    public partial class SecurityTool
    {
        public static class MD5
        {
            public static string Encrypt(string originalStr)
            {
                MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
                byte[] hashedDataBytes;
                hashedDataBytes = md5Hasher.ComputeHash(Encoding.GetEncoding("gb2312").GetBytes(originalStr));
                StringBuilder byte2String = new StringBuilder();
                foreach (byte i in hashedDataBytes)
                {
                    byte2String.Append(i.ToString("x2"));
                }
                return byte2String.ToString();
            }
        }
    }

}
namespace CloverNet.SecurityTool
{
    public partial  class SecurityTool
    {
        public static class SHA256
        {
            public static string Encrypt(string originalStr)
            {
                //使用 SHA256 加密算法:
                System.Security.Cryptography.SHA256 sha256 = new System.Security.Cryptography.SHA256Managed();
                byte[] sha256Bytes = System.Text.Encoding.Default.GetBytes(originalStr);
                byte[] cryString = sha256.ComputeHash(sha256Bytes);
                string sha256Str = string.Empty;
                for (int i = 0; i < cryString.Length; i++)
                {
                    sha256Str += cryString[i].ToString("X2");
                }
                return sha256Str;
            }
        }
    }
}

 

posted @ 2019-10-22 18:19  DarJeely  阅读(616)  评论(0编辑  收藏  举报