| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- /*******************************************************************************
- * Copyright(c) 2012 dongke All rights reserved. / Confidential
- * 类的信息:
- * 1.程序名称:Encryption.cs
- * 2.功能描述:系统各种字符串加密算法类
- * 编辑履历:
- * 作者 日期 版本 修改内容
- * 欧阳涛 2013/07/14 1.00 新建
- *******************************************************************************/
- using System;
- using System.Security.Cryptography;
- namespace Dongke.IBOSS.PRD.Basics.Library
- {
- /// <summary>
- /// 系统各种字符串加密算法类
- /// </summary>
- public class Encryption
- {
- #region RSA公钥加密算法
- /// <summary>
- /// RSA算法产生公共密钥和私有密钥
- /// </summary>
- /// <param name="publicKey">公共密钥</param>
- /// <param name="privateKey">私有密钥</param>
- public static void CreateRSAKeys(out string publicKey, out string privateKey)
- {
- System.Security.Cryptography.RSACryptoServiceProvider rsa =
- CreateRSACryptoServiceProvider();
- publicKey = rsa.ToXmlString(false);
- privateKey = rsa.ToXmlString(true);
- }
- public static string EncryptRSA(string value, string publicKey)
- {
- System.Security.Cryptography.RSACryptoServiceProvider rsa =
- CreateRSACryptoServiceProvider();
- rsa.FromXmlString(publicKey);
- byte[] data = System.Text.Encoding.UTF8.GetBytes(value);
- byte[] encryptedData = rsa.Encrypt(data, false);
- return System.Convert.ToBase64String(encryptedData);
- }
- public static string DecryptRSA(string value, string privateKey)
- {
- System.Security.Cryptography.RSACryptoServiceProvider rsa =
- CreateRSACryptoServiceProvider();
- rsa.FromXmlString(privateKey);
- byte[] data = System.Convert.FromBase64String(value);
- byte[] decryptedData = rsa.Decrypt(data, false);
- return System.Text.Encoding.UTF8.GetString(decryptedData);
- }
- protected static RSACryptoServiceProvider CreateRSACryptoServiceProvider()
- {
- System.Security.Cryptography.CspParameters cspParameters =
- new System.Security.Cryptography.CspParameters();
- cspParameters.Flags = System.Security.Cryptography.CspProviderFlags.UseMachineKeyStore;
- return new System.Security.Cryptography.RSACryptoServiceProvider(cspParameters);
- }
- #endregion
- #region DES加密算法
- public static string EncryptDES(string value, string key)
- {
- System.Security.Cryptography.DESCryptoServiceProvider des =
- CreateDESCryptoServiceProvider(key);
- byte[] bytesIn = System.Text.Encoding.UTF8.GetBytes(value);
- System.IO.MemoryStream stream = new System.IO.MemoryStream();
- System.Security.Cryptography.ICryptoTransform desdecrypt =
- des.CreateEncryptor();
- System.Security.Cryptography.CryptoStream cryptStream =
- new System.Security.Cryptography.CryptoStream(stream,
- desdecrypt,
- CryptoStreamMode.Write);
- cryptStream.Write(bytesIn, 0, bytesIn.Length);
- cryptStream.FlushFinalBlock();
- byte[] bytesOut = stream.ToArray();
- cryptStream.Close();
- stream.Close();
- return System.Convert.ToBase64String(bytesOut);
- }
- public static string DecryptDES(string value, string key)
- {
- System.Security.Cryptography.DESCryptoServiceProvider des =
- CreateDESCryptoServiceProvider(key);
- byte[] bytesIn = System.Convert.FromBase64String(value);
- System.IO.MemoryStream stream = new System.IO.MemoryStream(bytesIn);
- System.Security.Cryptography.ICryptoTransform desdecrypt = des.CreateDecryptor();
- System.Security.Cryptography.CryptoStream cryptStream =
- new System.Security.Cryptography.CryptoStream(stream,
- desdecrypt,
- CryptoStreamMode.Read);
- System.IO.StreamReader streamReader =
- new System.IO.StreamReader(cryptStream, System.Text.Encoding.UTF8);
- string result = streamReader.ReadToEnd();
- streamReader.Close();
- cryptStream.Close();
- stream.Close();
- return result;
- }
- protected static DESCryptoServiceProvider CreateDESCryptoServiceProvider(string key)
- {
- System.Security.Cryptography.DESCryptoServiceProvider des =
- new System.Security.Cryptography.DESCryptoServiceProvider();
- byte[] bytesKey = System.Text.Encoding.UTF8.GetBytes(key);
- des.Key = ResizeBytesArray(bytesKey, des.Key.Length);
- des.IV = ResizeBytesArray(bytesKey, des.IV.Length);
- return des;
- }
- protected static byte[] ResizeBytesArray(byte[] bytes, int newSize)
- {
- byte[] newBytes = new byte[newSize];
- if (bytes.Length <= newSize)
- {
- for (int i = 0; i < bytes.Length; i++)
- {
- newBytes[i] = bytes[i];
- }
- }
- else
- {
- int pos = 0;
- for (int i = 0; i < bytes.Length; i++)
- {
- newBytes[pos++] ^= bytes[i];
- if (pos >= newBytes.Length)
- {
- pos = 0;
- }
- }
- }
- return newBytes;
- }
- #endregion
- #region MD5加密算法
- public static string GetMD5String(string value)
- {
- return GetMD5String(System.Text.Encoding.UTF8.GetBytes(value));
- }
- public static string GetMD5String(byte[] value)
- {
- if (value == null)
- {
- return null;
- }
- System.Security.Cryptography.MD5CryptoServiceProvider md5 =
- new System.Security.Cryptography.MD5CryptoServiceProvider();
- byte[] bytes = md5.ComputeHash(value);
- return BitConverter.ToString(bytes).Replace("-", string.Empty);
- }
- #endregion
- #region SHA1加密算法
- public static string GetSHA1String(string value)
- {
- return GetSHA1String(System.Text.Encoding.UTF8.GetBytes(value));
- }
- public static string GetSHA1String(byte[] value)
- {
- if (value == null)
- {
- return null;
- }
- System.Security.Cryptography.SHA1CryptoServiceProvider sha1 =
- new System.Security.Cryptography.SHA1CryptoServiceProvider();
- byte[] bytes = sha1.ComputeHash(value);
- return BitConverter.ToString(bytes).Replace("-", string.Empty);
- }
- #endregion
- #region SHA256加密算法
- public static string GetSHA256String(string value)
- {
- return GetSHA256String(System.Text.Encoding.UTF8.GetBytes(value));
- }
- public static string GetSHA256String(byte[] value)
- {
- if (value == null)
- {
- return null;
- }
- System.Security.Cryptography.SHA256 sha256 =
- new System.Security.Cryptography.SHA256Managed();
- byte[] bytes = sha256.ComputeHash(value);
- return BitConverter.ToString(bytes).Replace("-", string.Empty);
- }
- #endregion
- }
- }
|