常用开发工具

常用开发工具_第1张图片

 

 

 

//-----------------------------------------------------------------------------------
// 
// * copyright: (C) 2017 720U科技有限公司 版权所有。
// * version  : 1.0.0.0
// * author   : rongbo
// * fileName : AES.cs
// * history  : created by rongbo 2017/3/13 14:11:33
// 
// 
//   EastWestWalk.NetFrameWork.Common.Encrypt.AES
// 
//-----------------------------------------------------------------------------------

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

namespace WinTools
{
    /// 
    /// AES
    ///     
    public class AES
    {
        /// 
        /// 加密
        /// 
        /// 数据字符
        /// 密文
        public static string Encrypt(string toEncrypt)
        {
            byte[] keyArray = Encoding.UTF8.GetBytes("12345678901234567890123456789012");
            byte[] toEncryptArray = Encoding.UTF8.GetBytes(toEncrypt);

            RijndaelManaged rDel = new RijndaelManaged
            {
                Key = keyArray,
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7
            };

            ICryptoTransform cTransform = rDel.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        /// 
        /// 解密
        /// 
        /// 密文
        /// 结果
        public static string Decrypt(string toDecrypt)
        {
            byte[] keyArray = Encoding.UTF8.GetBytes("12345678901234567890123456789012");
            byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);

            RijndaelManaged rDel = new RijndaelManaged
            {
                Key = keyArray,
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7
            };

            ICryptoTransform cTransform = rDel.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Encoding.UTF8.GetString(resultArray);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinTools
{
    /// 
    /// 
    /// 
    public class Base64
    {
        /// 
        /// 编码
        /// 
        /// The code_type.如 utf-8
        /// The code.
        /// 
        public static string EncodeBase64(string code, string code_type = "utf-8")
        {
            string encode = "";
            byte[] bytes = Encoding.GetEncoding(code_type).GetBytes(code);
            try
            {
                encode = Convert.ToBase64String(bytes);
            }
            catch
            {
                encode = code;
            }
            return encode;
        }
        /// 
        /// 解码
        /// 
        /// The code_type.如 utf-8
        /// The code.
        /// 
        public static string DecodeBase64(string code, string code_type = "utf-8")
        {
            string decode = "";
            byte[] bytes = Convert.FromBase64String(code);
            try
            {
                decode = Encoding.GetEncoding(code_type).GetString(bytes);
            }
            catch
            {
                decode = code;
            }
            return decode;
        }
    }
}

 

//-----------------------------------------------------------------------------------
// 
// * copyright: (C) 2017 720U科技有限公司 版权所有。
// * version  : 1.0.0.0
// * author   : rongbo
// * fileName : Des.cs
// * history  : created by rongbo 2017/3/13 14:12:06
// 
// 
//   EastWestWalk.NetFrameWork.Common.Encrypt.Des
// 
//-----------------------------------------------------------------------------------

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

namespace WinTools
{
    /// 
    /// Des加密解密类
    /// 
    public class Des
    {
        /// 
        /// Key
        /// 
        private const string KEY = "xda14121";

        /// 
        /// IV
        /// 
        private const string IV = "vva14121";

        /// 
        /// DES加密方法
        /// 
        /// 原文
        /// key
        /// 密文
        public static string Encrypt(string source, string key)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                key = "scxingyu";
            }
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                byte[] keys = Encoding.ASCII.GetBytes(key);
                byte[] ivs = Encoding.ASCII.GetBytes(key);
                byte[] dataByteArray = Encoding.UTF8.GetBytes(source);
                des.Mode = CipherMode.CBC;
                des.Key = keys;
                des.IV = ivs;
                string encrypt;
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(dataByteArray, 0, dataByteArray.Length);
                        cs.FlushFinalBlock();
                        encrypt = Convert.ToBase64String(ms.ToArray());
                    }
                }
                return encrypt;
            }
        }

        /// 
        /// 进行DES解密。
        /// 
        /// key
        /// 要解密的base64串
        /// 已解密的字符串。
        public static string Decrypt(string source, string key)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                key = "scxingyu";
            }

            try
            {
                byte[] inputByteArray = Convert.FromBase64String(source);
                using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
                {
                    des.Key = Encoding.ASCII.GetBytes(key);
                    des.IV = Encoding.ASCII.GetBytes(key);
                    MemoryStream ms = new 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;
                }
            }
            catch (Exception ex)
            {
                return string.Empty;
            }
        }
    }
}
//-----------------------------------------------------------------------------------
// 
// * copyright: (C) 2017 720U科技有限公司 版权所有。
// * version  : 1.0.0.0
// * author   : fengguotao
// * fileName : GZipString.cs
// * history  : created by fengguotao 2017/3/14 17:26:17
// 
// 
//   EastWestWalk.NetFrameWork.Common.Encrypt.GZipString
// 
//-----------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinTools
{
    /// 
    /// GZipString
    ///     
    public class GZipString
    {
        /// 
        /// 将传入字符串以GZip算法压缩后,返回Base64编码字符
        /// 
        /// 需要压缩的字符串
        /// 压缩后的Base64编码的字符串
        public static string GZipCompressString(string rawString)
        {
            if (string.IsNullOrEmpty(rawString) || rawString.Length == 0)
            {
                return string.Empty;
            }
            else
            {
                byte[] rawData = System.Text.Encoding.UTF8.GetBytes(rawString.ToString());
                byte[] zippedData = Compress(rawData);
                return Convert.ToBase64String(zippedData);
            }
        }

        /// 
        /// 将传入字符串以GZip算法压缩后
        /// 
        /// 需要压缩的字符串
        /// 压缩后的Base64编码的字符串
        public static string CompressString(string rawString)
        {
            if (string.IsNullOrEmpty(rawString) || rawString.Length == 0)
            {
                return string.Empty;
            }
            else
            {
                byte[] rawData = System.Text.Encoding.UTF8.GetBytes(rawString.ToString());
                byte[] zippedData = Compress(rawData);
                return System.Text.Encoding.UTF8.GetString(zippedData);
            }
        }

        /// 
        /// GZip压缩
        /// 
        /// 数据包
        /// 压缩数据包
        public static byte[] Compress(byte[] rawData)
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (GZipStream compressedzipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
                {
                    compressedzipStream.Write(rawData, 0, rawData.Length);
                    compressedzipStream.Close();
                    return memoryStream.ToArray();
                }
            }
        }

        /// 
        /// 将传入的二进制字符串资料以GZip算法解压缩
        /// 
        /// 经GZip压缩后的二进制字符串
        /// 原始未压缩字符串
        public static string GZipDecompressString(string gzipString)
        {
            if (string.IsNullOrEmpty(gzipString) || gzipString.Length == 0)
            {
                return string.Empty;
            }
            else
            {
                byte[] zippedData = Convert.FromBase64String(gzipString);
                return System.Text.Encoding.UTF8.GetString(Decompress(zippedData));
            }
        }

        /// 
        /// 将传入的二进制字符串资料以GZip算法解压缩
        /// 
        /// 经GZip压缩后的二进制字符串
        /// 原始未压缩字符串
        public static string DecompressString(string gzipString)
        {
            if (string.IsNullOrEmpty(gzipString) || gzipString.Length == 0)
            {
                return string.Empty;
            }
            else
            {
                byte[] zippedData = System.Text.Encoding.UTF8.GetBytes(gzipString);
                return System.Text.Encoding.UTF8.GetString(Decompress(zippedData));
            }
        }

        /// 
        /// GZIP解压
        /// 
        /// 数据包
        /// 解压数据包
        public static byte[] Decompress(byte[] gzipData)
        {
            using (MemoryStream memoryStream = new MemoryStream(gzipData))
            {
                using (GZipStream compressedzipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
                {
                    MemoryStream outBuffer = new MemoryStream();
                    byte[] block = new byte[1024];
                    while (true)
                    {
                        int bytesRead = compressedzipStream.Read(block, 0, block.Length);
                        if (bytesRead <= 0)
                            break;
                        else
                            outBuffer.Write(block, 0, bytesRead);
                    }

                    compressedzipStream.Close();
                    return outBuffer.ToArray();
                }
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinTools
{
    public class JsonFormart
    {
        /// 
        /// JSON字符串格式化
        /// 
        /// 
        /// 
        public static string JsonTree(string json)
        {
            int level = 0;
            var jsonArr = json.ToArray();  // Using System.Linq;
            string jsonTree = string.Empty;
            for (int i = 0; i < json.Length; i++)
            {
                char c = jsonArr[i];
                if (level > 0 && '\n' == jsonTree.ToArray()[jsonTree.Length - 1])
                {
                    jsonTree += TreeLevel(level);
                }
                switch (c)
                {
                    case '[':
                        jsonTree += c + "\n";
                        level++;
                        break;
                    case ',':
                        jsonTree += c + "\n";
                        break;
                    case ']':
                        jsonTree += "\n";
                        level--;
                        jsonTree += TreeLevel(level);
                        jsonTree += c;
                        break;
                    default:
                        jsonTree += c;
                        break;
                }
            }
            return jsonTree;
        }
        /// 
        /// 树等级
        /// 
        /// 
        /// 
        private static string TreeLevel(int level)
        {
            string leaf = string.Empty;
            for (int t = 0; t < level; t++)
            {
                leaf += "\t";
            }
            return leaf;
        }
    }
}
//-----------------------------------------------------------------------------------
// 
// * copyright: (C) 2017 720U科技有限公司 版权所有。
// * version  : 1.0.0.0
// * author   : rongbo
// * fileName : Md5.cs
// * history  : created by rongbo 2017/3/13 14:12:33
// 
// 
//   EastWestWalk.NetFrameWork.Common.Encrypt.Md5
// 
//-----------------------------------------------------------------------------------

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

namespace WinTools
{
    /// 
    /// Md5
    ///     
    public class Md5
    {
        /// 
        /// 加密32
        /// 
        /// 数据
        /// 密文
        public static string Encrypt32(string source)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] t = md5.ComputeHash(Encoding.UTF8.GetBytes(source));
            StringBuilder sb = new StringBuilder(32);
            foreach (byte item in t)
            {
                sb.Append(item.ToString("x").PadLeft(2, '0'));
            }
            return sb.ToString().ToUpper();
        }

        /// 
        /// 加密16
        /// 
        /// 数据
        /// 密文
        public static string Encrypt16(string source)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string t2 = BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(source)), 4, 8);
            t2 = t2.Replace("-", "");
            return t2.ToUpper();
        }


        ///   
        /// MD5 加密字符串  
        ///   
        /// 源字符串  
        /// 加密后字符串  
        public static string MD5Encoding(string rawPass)
        {
            // 创建MD5类的默认实例:MD5CryptoServiceProvider  
            MD5 md5 = MD5.Create();
            byte[] bs = Encoding.UTF8.GetBytes(rawPass);
            byte[] hs = md5.ComputeHash(bs);
            StringBuilder sb = new StringBuilder();
            foreach (byte b in hs)
            {
                // 以十六进制格式格式化  
                sb.Append(b.ToString("x2"));
            }
            return sb.ToString();
        }

        ///   
        /// MD5盐值加密  
        ///   
        /// 源字符串  
        /// 盐值  
        /// 加密后字符串  
        public static string MD5Encoding(string rawPass, object salt)
        {
            if (salt == null) return rawPass;
            return MD5Encoding(rawPass + "{" + salt.ToString() + "}");
        }
    }
}

 

//-----------------------------------------------------------------------------------
// 
// * copyright: (C) 2017 720U科技有限公司 版权所有。
// * version  : 1.0.0.0
// * author   : rongbo
// * fileName : SHA1.cs
// * history  : created by rongbo 2017/3/13 14:13:07
// 
// 
//   EastWestWalk.NetFrameWork.Common.Encrypt.SHA1
// 
//-----------------------------------------------------------------------------------

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

namespace WinTools
{
    /// 
    /// SHA1
    ///     
    public class SHA1
    {
        /// 
        /// 加密
        /// 
        /// 数据
        /// 密文
        public static string Encrypt(string source)
        {
            SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
            byte[] str1 = Encoding.UTF8.GetBytes(source);
            byte[] str2 = sha1.ComputeHash(str1);
            sha1.Clear();
            ((IDisposable)sha1).Dispose();
            return Convert.ToBase64String(str2);
        }
    }
}

 

//-----------------------------------------------------------------------------------// // * copyright: (C) 2017 720U科技有限公司 版权所有。// * version  : 1.0.0.0// * author   : rongbo// * fileName : SHA1.cs// * history  : created by rongbo 2017/3/13 14:13:07// //

//   EastWestWalk.NetFrameWork.Common.Encrypt.SHA1// //-----------------------------------------------------------------------------------
using System;using System.Security.Cryptography;using System.Text;
namespace WinTools{    ///     /// SHA1    ///     public class SHA1    {        ///         /// 加密        ///         /// 数据        /// 密文        public static string Encrypt(string source)        {            SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();            byte[] str1 = Encoding.UTF8.GetBytes(source);            byte[] str2 = sha1.ComputeHash(str1);            sha1.Clear();            ((IDisposable)sha1).Dispose();            return Convert.ToBase64String(str2);        }    }}

你可能感兴趣的:(常用开发工具)