/*******************************************************************************
* 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
{
///
/// 系统各种字符串加密算法类
///
public class Encryption
{
#region RSA公钥加密算法
///
/// RSA算法产生公共密钥和私有密钥
///
/// 公共密钥
/// 私有密钥
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
}
}