博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.NET 常用加密、解密& 数字签名算法
阅读量:4322 次
发布时间:2019-06-06

本文共 8491 字,大约阅读时间需要 28 分钟。

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Runtime.Serialization.Formatters.Binary;using System.Security.Cryptography;using System.Text;//xlding, 2013/07/25namespace Gemr.Utils{    public class CommonAlgorithms    {        #region Sort        public static string[] BubbleSort(string[] array)        {            int length = array.Length;            for (int i = 0; i <= length - 1; i++)            {                for (int j = length - 1; j > i; j--)                {                    if (array[j].CompareTo(array[j - 1]) < 0)                    {                        string temp = array[j];                        array[j] = array[j - 1];                        array[j - 1] = temp;                    }                }            }            return array;        }        #endregion Sort        private static char[] constant =        {            'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',            'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'        };        public byte[] ConvertStringToByteArray(string str)        {            if (string.IsNullOrEmpty(str)) return null;            byte[] byteArray = System.Text.Encoding.Default.GetBytes(str);            return byteArray;        }        public static string GenerateRandom(int Length)        {            System.Text.StringBuilder newRandom = new System.Text.StringBuilder(52);            Random rd = new Random();            for (int i = 0; i < Length; i++)            {                newRandom.Append(constant[rd.Next(52)]);            }            return newRandom.ToString();        }        ///         /// Put the object serialization for byte array        ///         public static byte[] SerializeObject(object obj)        {            if (obj == null)                return null;            MemoryStream ms = new MemoryStream();            BinaryFormatter formatter = new BinaryFormatter();            formatter.Serialize(ms, obj);            ms.Position = 0;            byte[] bytes = new byte[ms.Length];            ms.Read(bytes, 0, bytes.Length);            ms.Close();            return bytes;        }        ///         /// Byte array reverse serialized into object        ///         public static object DeserializeObject(byte[] bytes)        {            object obj = null;            if (bytes == null)                return obj;            MemoryStream ms = new MemoryStream(bytes);            ms.Position = 0;            BinaryFormatter formatter = new BinaryFormatter();            obj = formatter.Deserialize(ms);            ms.Close();            return obj;        }        public static string ConvertByteArrayToString(byte[] byteArray)        {            if (byteArray == null || byteArray.Length == 0) return null;            string str = System.Text.Encoding.Default.GetString(byteArray);            return str;        }        //public static string ByteToString(byte[] value)        //{        //    StringBuilder sb = new StringBuilder();        //    for (int i = 0; i < value.Length; i++)        //    {        //        sb.Append(value[i].ToString("x2"));        //    }        //    return sb.ToString();        //}        #region SHA1 (数字签名)               public static string GetSHA1(string strSource)        {            string strResult = "";            //Create            System.Security.Cryptography.SHA1 sha = System.Security.Cryptography.SHA1.Create();            byte[] bytResult = sha.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strSource));            for (int i = 0; i < bytResult.Length; i++)            {                strResult = strResult + bytResult[i].ToString("X2");            }            return strResult;        }        public static byte[] GetSHA1(byte[] value)        {            System.Security.Cryptography.SHA1 sha = System.Security.Cryptography.SHA1.Create();            return sha.ComputeHash(value);        }        #endregion SHA1        #region DES        /**/        ///         /// DES加密        ///         ///         /// 
public static string DesEncrypt(string encryptString, string sKey) { using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) { byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); 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; } } /**/ /// /// DES解密 /// /// ///
public static string DesDecrypt(string decryptString, string sKey) { byte[] inputByteArray = Convert.FromBase64String(decryptString); using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) { des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); 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; } } #endregion DES #region AES #region Use static key private static readonly byte[] aesKey = { 0x03, 0x00, 0x52, 0xc3, 0x03, 0x00, 0x52, 0xc3, 0x03, 0x00, 0x52, 0xc3, 0x03, 0x00, 0x52, 0xc3 }; public static string StaticAeskey { get { return System.Text.Encoding.Default.GetString(aesKey); } } /// /// AES encode. /// /// ///
public static string AesEncode(string value) { using (Aes aes = Aes.Create()) { using (ICryptoTransform encryptor = aes.CreateEncryptor(aesKey, aesKey)) { byte[] buffer = Encoding.UTF8.GetBytes(value); buffer = encryptor.TransformFinalBlock(buffer, 0, buffer.Length); return Convert.ToBase64String(buffer); } } } /// /// AES decode. /// /// ///
public static string AesDecode(string value) { using (Aes aes = Aes.Create()) { using (ICryptoTransform decryptor = aes.CreateDecryptor(aesKey, aesKey)) { byte[] buffer = Convert.FromBase64String(value); buffer = decryptor.TransformFinalBlock(buffer, 0, buffer.Length); return Encoding.UTF8.GetString(buffer); } } } #endregion Use static key public static byte[] GetKey() { Random rd = new Random(); byte[] key = new byte[16]; rd.NextBytes(key); return key; } public static byte[] AesEncode(byte[] key, byte[] value) { using (Aes aes = Aes.Create()) { using (ICryptoTransform encryptor = aes.CreateEncryptor(key, key)) { return encryptor.TransformFinalBlock(value, 0, value.Length); } } } public static byte[] AesDecode(byte[] key, byte[] value) { using (Aes aes = Aes.Create()) { using (ICryptoTransform decryptor = aes.CreateDecryptor(key, key)) { return decryptor.TransformFinalBlock(value, 0, value.Length); } } } #endregion AES }}

 

转载于:https://www.cnblogs.com/quietwalk/p/3531710.html

你可能感兴趣的文章
android app崩溃日志收集以及上传
查看>>
3、VS2010+ASP.NET MVC4+EF4+JqueryEasyUI+Oracle项目开发之——用户登录
查看>>
面试记-(1)
查看>>
压力测试 相关
查看>>
android update automatically ( android 自动升级)
查看>>
session cookie
查看>>
POJ 1222 EXTENDED LIGHTS OUT(翻转+二维开关问题)
查看>>
【BZOJ-4059】Non-boring sequences 线段树 + 扫描线 (正解暴力)
查看>>
几种简单的负载均衡算法及其Java代码实现
查看>>
TMS3705A PCF7991AT 线路图
查看>>
白盒测试实践(小组作业)day4
查看>>
为什么学sail.js
查看>>
pythen创建cocos2dx项目
查看>>
js调用.net后台事件,和后台调用前台等方法总结
查看>>
Vert.x 之 HelloWorld
查看>>
太阳能路灯项目背景知识
查看>>
Objec类和final关键字的用法
查看>>
打开matlab遗传算法工具箱的方法
查看>>
Ajax制作智能提示搜索
查看>>
打赏页面
查看>>