常用开发工具
//----------------------------------------------------------------------------------- // <copyright file="AES.cs" company="720U Enterprises"> // * copyright: (C) 2017 720U科技有限公司 版权所有。 // * version : 1.0.0.0 // * author : rongbo // * fileName : AES.cs // * history : created by rongbo 2017/3/13 14:11:33 // </copyright> // <summary> // EastWestWalk.NetFrameWork.Common.Encrypt.AES // </summary> //----------------------------------------------------------------------------------- using System; using System.Security.Cryptography; using System.Text; namespace WinTools { /// <summary> /// AES /// </summary> public class AES { /// <summary> /// 加密 /// </summary> /// <param name="toEncrypt">数据字符</param> /// <returns>密文</returns> 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); } /// <summary> /// 解密 /// </summary> /// <param name="toDecrypt">密文</param> /// <returns>结果</returns> 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 { /// <summary> /// /// </summary> public class Base64 { /// <summary> /// 编码 /// </summary> /// <param name="code_type">The code_type.如 utf-8</param> /// <param name="code">The code.</param> /// <returns></returns> 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; } /// <summary> /// 解码 /// </summary> /// <param name="code_type">The code_type.如 utf-8</param> /// <param name="code">The code.</param> /// <returns></returns> 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 file="Des.cs" company="720U Enterprises"> // * copyright: (C) 2017 720U科技有限公司 版权所有。 // * version : 1.0.0.0 // * author : rongbo // * fileName : Des.cs // * history : created by rongbo 2017/3/13 14:12:06 // </copyright> // <summary> // EastWestWalk.NetFrameWork.Common.Encrypt.Des // </summary> //----------------------------------------------------------------------------------- using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace WinTools { /// <summary> /// Des加密解密类 /// </summary> public class Des { /// <summary> /// Key /// </summary> private const string KEY = "xda14121"; /// <summary> /// IV /// </summary> private const string IV = "vva14121"; /// <summary> /// DES加密方法 /// </summary> /// <param name="source">原文</param> /// <param name="key">key</param> /// <returns>密文</returns> 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; } } /// <summary> /// 进行DES解密。 /// </summary> /// <param name="key">key</param> /// <param name="source">要解密的base64串</param> /// <returns>已解密的字符串。</returns> 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 file="GZipString.cs" company="720U Enterprises"> // * copyright: (C) 2017 720U科技有限公司 版权所有。 // * version : 1.0.0.0 // * author : fengguotao // * fileName : GZipString.cs // * history : created by fengguotao 2017/3/14 17:26:17 // </copyright> // <summary> // EastWestWalk.NetFrameWork.Common.Encrypt.GZipString // </summary> //----------------------------------------------------------------------------------- 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 { /// <summary> /// GZipString /// </summary> public class GZipString { /// <summary> /// 将传入字符串以GZip算法压缩后,返回Base64编码字符 /// </summary> /// <param name="rawString">需要压缩的字符串</param> /// <returns>压缩后的Base64编码的字符串</returns> 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); } } /// <summary> /// 将传入字符串以GZip算法压缩后 /// </summary> /// <param name="rawString">需要压缩的字符串</param> /// <returns>压缩后的Base64编码的字符串</returns> 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); } } /// <summary> /// GZip压缩 /// </summary> /// <param name="rawData">数据包</param> /// <returns>压缩数据包</returns> 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(); } } } /// <summary> /// 将传入的二进制字符串资料以GZip算法解压缩 /// </summary> /// <param name="gzipString">经GZip压缩后的二进制字符串</param> /// <returns>原始未压缩字符串</returns> 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)); } } /// <summary> /// 将传入的二进制字符串资料以GZip算法解压缩 /// </summary> /// <param name="gzipString">经GZip压缩后的二进制字符串</param> /// <returns>原始未压缩字符串</returns> 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)); } } /// <summary> /// GZIP解压 /// </summary> /// <param name="gzipData">数据包</param> /// <returns>解压数据包</returns> 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 { /// <summary> /// JSON字符串格式化 /// </summary> /// <param name="json"></param> /// <returns></returns> 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; } /// <summary> /// 树等级 /// </summary> /// <param name="level"></param> /// <returns></returns> private static string TreeLevel(int level) { string leaf = string.Empty; for (int t = 0; t < level; t++) { leaf += "\t"; } return leaf; } } }
//----------------------------------------------------------------------------------- // <copyright file="Md5.cs" company="720U Enterprises"> // * copyright: (C) 2017 720U科技有限公司 版权所有。 // * version : 1.0.0.0 // * author : rongbo // * fileName : Md5.cs // * history : created by rongbo 2017/3/13 14:12:33 // </copyright> // <summary> // EastWestWalk.NetFrameWork.Common.Encrypt.Md5 // </summary> //----------------------------------------------------------------------------------- using System; using System.Security.Cryptography; using System.Text; namespace WinTools { /// <summary> /// Md5 /// </summary> public class Md5 { /// <summary> /// 加密32 /// </summary> /// <param name="source">数据</param> /// <returns>密文</returns> 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(); } /// <summary> /// 加密16 /// </summary> /// <param name="source">数据</param> /// <returns>密文</returns> 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(); } /// <summary> /// MD5 加密字符串 /// </summary> /// <param name="rawPass">源字符串</param> /// <returns>加密后字符串</returns> 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(); } /// <summary> /// MD5盐值加密 /// </summary> /// <param name="rawPass">源字符串</param> /// <param name="salt">盐值</param> /// <returns>加密后字符串</returns> public static string MD5Encoding(string rawPass, object salt) { if (salt == null) return rawPass; return MD5Encoding(rawPass + "{" + salt.ToString() + "}"); } } }
//----------------------------------------------------------------------------------- // <copyright file="SHA1.cs" company="720U Enterprises"> // * copyright: (C) 2017 720U科技有限公司 版权所有。 // * version : 1.0.0.0 // * author : rongbo // * fileName : SHA1.cs // * history : created by rongbo 2017/3/13 14:13:07 // </copyright> // <summary> // EastWestWalk.NetFrameWork.Common.Encrypt.SHA1 // </summary> //----------------------------------------------------------------------------------- using System; using System.Security.Cryptography; using System.Text; namespace WinTools { /// <summary> /// SHA1 /// </summary> public class SHA1 { /// <summary> /// 加密 /// </summary> /// <param name="source">数据</param> /// <returns>密文</returns> 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 file="SHA1.cs" company="720U Enterprises">// * copyright: (C) 2017 720U科技有限公司 版权所有。// * version : 1.0.0.0// * author : rongbo// * fileName : SHA1.cs// * history : created by rongbo 2017/3/13 14:13:07// </copyright>// <summary>// EastWestWalk.NetFrameWork.Common.Encrypt.SHA1// </summary>//-----------------------------------------------------------------------------------
using System;using System.Security.Cryptography;using System.Text;
namespace WinTools{ /// <summary> /// SHA1 /// </summary> public class SHA1 { /// <summary> /// 加密 /// </summary> /// <param name="source">数据</param> /// <returns>密文</returns> 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); } }}