最近一段時間,集團加強了資安方面的管理,所有敏感的配置字節都必須經過加密處理,把最近用到的幾個加解密類整理了一下,以做備忘.
其實這兩個類實現的方法差不多,只是有些細微區別:
對稱加密演算法類 SymmetricMethodHelper
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
/// <summary>
/// 對稱加密演算法類
/// </summary>
public class SymmetricMethodHelper
{
private SymmetricAlgorithm mobjCryptoService;
private string Key;
/// <summary>
/// 對稱加密類的構造函數
/// </summary>
public SymmetricMethodHelper()
{
mobjCryptoService = new RijndaelManaged();
Key = "Guz(%&hj7x89H$yuBI0456FtmaT5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7";
}
/// <summary>
/// 對稱加密類的構造函數
/// </summary>
public SymmetricMethodHelper(string key)
{
mobjCryptoService = new RijndaelManaged();
Key = key + "Guz(%&hj7x89H$yuBI0456FtmaT5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7";
}
/// <summary>
/// 獲得密鑰
/// </summary>
/// <returns>密鑰</returns>
private byte[] GetLegalKey()
{
string sTemp = Key;
mobjCryptoService.GenerateKey();
byte[] bytTemp = mobjCryptoService.Key;
int KeyLength = bytTemp.Length;
if (sTemp.Length > KeyLength)
sTemp = sTemp.Substring(0, KeyLength);
else if (sTemp.Length < KeyLength)
sTemp = sTemp.PadRight(KeyLength, ' ');
return ASCIIEncoding.ASCII.GetBytes(sTemp);
}
/// <summary>
/// 獲得初始向量IV
/// </summary>
/// <returns>初試向量IV</returns>
private byte[] GetLegalIV()
{
string sTemp = "E4ghj*Ghg7!rNIfb&95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjk";
mobjCryptoService.GenerateIV();
byte[] bytTemp = mobjCryptoService.IV;
int IVLength = bytTemp.Length;
if (sTemp.Length > IVLength)
sTemp = sTemp.Substring(0, IVLength);
else if (sTemp.Length < IVLength)
sTemp = sTemp.PadRight(IVLength, ' ');
return ASCIIEncoding.ASCII.GetBytes(sTemp);
}
/// <summary>
/// 加密方法
/// </summary>
/// <param name="Source">待加密的串</param>
/// <returns>經過加密的串</returns>
public string Encrypto(string Source)
{
byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);
MemoryStream ms = new MemoryStream();
mobjCryptoService.Key = GetLegalKey();
mobjCryptoService.IV = GetLegalIV();
ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
ms.Close();
byte[] bytOut = ms.ToArray();
return Convert.ToBase64String(bytOut);
}
/// <summary>
/// 解密方法
/// </summary>
/// <param name="Source">待解密的串</param>
/// <returns>經過解密的串</returns>
public string Decrypto(string Source)
{
byte[] bytIn = Convert.FromBase64String(Source);
MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
mobjCryptoService.Key = GetLegalKey();
mobjCryptoService.IV = GetLegalIV();
ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
return sr.ReadToEnd();
}
}
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
/// <summary>
/// 對稱加密演算法類
/// </summary>
public class SymmetricMethodHelper
{
private SymmetricAlgorithm mobjCryptoService;
private string Key;
/// <summary>
/// 對稱加密類的構造函數
/// </summary>
public SymmetricMethodHelper()
{
mobjCryptoService = new RijndaelManaged();
Key = "Guz(%&hj7x89H$yuBI0456FtmaT5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7";
}
/// <summary>
/// 對稱加密類的構造函數
/// </summary>
public SymmetricMethodHelper(string key)
{
mobjCryptoService = new RijndaelManaged();
Key = key + "Guz(%&hj7x89H$yuBI0456FtmaT5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7";
}
/// <summary>
/// 獲得密鑰
/// </summary>
/// <returns>密鑰</returns>
private byte[] GetLegalKey()
{
string sTemp = Key;
mobjCryptoService.GenerateKey();
byte[] bytTemp = mobjCryptoService.Key;
int KeyLength = bytTemp.Length;
if (sTemp.Length > KeyLength)
sTemp = sTemp.Substring(0, KeyLength);
else if (sTemp.Length < KeyLength)
sTemp = sTemp.PadRight(KeyLength, ' ');
return ASCIIEncoding.ASCII.GetBytes(sTemp);
}
/// <summary>
/// 獲得初始向量IV
/// </summary>
/// <returns>初試向量IV</returns>
private byte[] GetLegalIV()
{
string sTemp = "E4ghj*Ghg7!rNIfb&95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjk";
mobjCryptoService.GenerateIV();
byte[] bytTemp = mobjCryptoService.IV;
int IVLength = bytTemp.Length;
if (sTemp.Length > IVLength)
sTemp = sTemp.Substring(0, IVLength);
else if (sTemp.Length < IVLength)
sTemp = sTemp.PadRight(IVLength, ' ');
return ASCIIEncoding.ASCII.GetBytes(sTemp);
}
/// <summary>
/// 加密方法
/// </summary>
/// <param name="Source">待加密的串</param>
/// <returns>經過加密的串</returns>
public string Encrypto(string Source)
{
byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);
MemoryStream ms = new MemoryStream();
mobjCryptoService.Key = GetLegalKey();
mobjCryptoService.IV = GetLegalIV();
ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
ms.Close();
byte[] bytOut = ms.ToArray();
return Convert.ToBase64String(bytOut);
}
/// <summary>
/// 解密方法
/// </summary>
/// <param name="Source">待解密的串</param>
/// <returns>經過解密的串</returns>
public string Decrypto(string Source)
{
byte[] bytIn = Convert.FromBase64String(Source);
MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
mobjCryptoService.Key = GetLegalKey();
mobjCryptoService.IV = GetLegalIV();
ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
return sr.ReadToEnd();
}
}
Code
using System;
using System.Text;
using System.IO;
using System.Security.Cryptography;
public class EncryptUtil {
/// <summary>
/// Des加密
/// </summary>
/// <param name="clearText"></param>
/// <returns></returns>
public static string DesEncrypt(string clearText) {
byte[] byKey = System.Text.ASCIIEncoding.UTF8.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.UTF8.GetBytes(IV_64);
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memStream = new MemoryStream();
//以寫模式 把數據流和要加密的數據流建立連接
CryptoStream cryStream = new CryptoStream(memStream, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
//將要加密的數據轉換為UTF8編碼的數組
byte[] clearTextArray = Encoding.UTF8.GetBytes(clearText);
//加密 並寫到 内存流memStream中
cryStream.Write(clearTextArray, 0, clearTextArray.Length);
//清空緩衝區
cryStream.FlushFinalBlock();
//將8位無符號整數數組 轉換為 等效的System.String 的形式.
return Convert.ToBase64String(memStream.ToArray());
}
/// <summary>
/// Des解密
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static string DesDecrypt(string encryptedText) {
byte[] byKey = System.Text.ASCIIEncoding.UTF8.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.UTF8.GetBytes(IV_64);
//
byte[] byteArray = Convert.FromBase64String(encryptedText);
MemoryStream memStream = new MemoryStream();
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
CryptoStream cryStream = new CryptoStream(memStream, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Write);
cryStream.Write(byteArray, 0, byteArray.Length);
//清空緩衝區
cryStream.FlushFinalBlock();
System.Text.Encoding encoding = new System.Text.UTF8Encoding();
//把字節數組轉換為 等效的System.String 的形式.
return encoding.GetString(memStream.ToArray());
}
/// <summary>
/// Des加密,功能同DesEncrypt相同
/// </summary>
/// <param name="clearText"></param>
/// <returns></returns>
public static string DesEncode(string clearText) {
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memStream = new MemoryStream();
CryptoStream cryStream = new CryptoStream(memStream, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cryStream);
sw.Write(clearText);
sw.Flush();
cryStream.FlushFinalBlock();
sw.Flush();
return Convert.ToBase64String(memStream.GetBuffer(), 0, (int)memStream.Length);
}
/// <summary>
/// Des解密,功能同DesDecrypt相同
/// </summary>
/// <param name="encryptedText"></param>
/// <returns></returns>
public static string DesDecode(string encryptedText) {
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
byte[] byteArray;
try {
byteArray = Convert.FromBase64String(encryptedText);
}
catch {
return null;
}
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memStream = new MemoryStream(byteArray);
CryptoStream cryStream = new CryptoStream(memStream, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cryStream);
return sr.ReadToEnd();
}
private const string KEY_64 = "MyPubKey"; //公鈅
private const string IV_64 = "MyPriKey"; //私鈅,注意了:是8个字符,64位
}
注意這兩段代碼標注為紅色部分的差异,其中SymmetricMethodHelper類支持超過8位的私鑰,而EncryptUtil類不支持超過8位的私鑰.
using System;
using System.Text;
using System.IO;
using System.Security.Cryptography;
public class EncryptUtil {
/// <summary>
/// Des加密
/// </summary>
/// <param name="clearText"></param>
/// <returns></returns>
public static string DesEncrypt(string clearText) {
byte[] byKey = System.Text.ASCIIEncoding.UTF8.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.UTF8.GetBytes(IV_64);
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memStream = new MemoryStream();
//以寫模式 把數據流和要加密的數據流建立連接
CryptoStream cryStream = new CryptoStream(memStream, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
//將要加密的數據轉換為UTF8編碼的數組
byte[] clearTextArray = Encoding.UTF8.GetBytes(clearText);
//加密 並寫到 内存流memStream中
cryStream.Write(clearTextArray, 0, clearTextArray.Length);
//清空緩衝區
cryStream.FlushFinalBlock();
//將8位無符號整數數組 轉換為 等效的System.String 的形式.
return Convert.ToBase64String(memStream.ToArray());
}
/// <summary>
/// Des解密
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static string DesDecrypt(string encryptedText) {
byte[] byKey = System.Text.ASCIIEncoding.UTF8.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.UTF8.GetBytes(IV_64);
//
byte[] byteArray = Convert.FromBase64String(encryptedText);
MemoryStream memStream = new MemoryStream();
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
CryptoStream cryStream = new CryptoStream(memStream, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Write);
cryStream.Write(byteArray, 0, byteArray.Length);
//清空緩衝區
cryStream.FlushFinalBlock();
System.Text.Encoding encoding = new System.Text.UTF8Encoding();
//把字節數組轉換為 等效的System.String 的形式.
return encoding.GetString(memStream.ToArray());
}
/// <summary>
/// Des加密,功能同DesEncrypt相同
/// </summary>
/// <param name="clearText"></param>
/// <returns></returns>
public static string DesEncode(string clearText) {
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memStream = new MemoryStream();
CryptoStream cryStream = new CryptoStream(memStream, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cryStream);
sw.Write(clearText);
sw.Flush();
cryStream.FlushFinalBlock();
sw.Flush();
return Convert.ToBase64String(memStream.GetBuffer(), 0, (int)memStream.Length);
}
/// <summary>
/// Des解密,功能同DesDecrypt相同
/// </summary>
/// <param name="encryptedText"></param>
/// <returns></returns>
public static string DesDecode(string encryptedText) {
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
byte[] byteArray;
try {
byteArray = Convert.FromBase64String(encryptedText);
}
catch {
return null;
}
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memStream = new MemoryStream(byteArray);
CryptoStream cryStream = new CryptoStream(memStream, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cryStream);
return sr.ReadToEnd();
}
private const string KEY_64 = "MyPubKey"; //公鈅
private const string IV_64 = "MyPriKey"; //私鈅,注意了:是8个字符,64位
}