.net加密解密
前几天因为要做点加密解密的东西来保存配置信息,找了很久终于找到一个比较常用的类,贴出来.共享...
里面已经有两种加密解密的算法:DES, Rijndael
加需要的,去查一下msdn,自己加啦..
public class SymmCrypto
{
public enum SymmProvEnum : int
{
DES, Rijndael
}
private static SymmetricAlgorithm mobjCryptoService;
public static void ChangeCryptoService(SymmProvEnum NetSelected)
{
switch (NetSelected)
{
case SymmProvEnum.DES:
mobjCryptoService = new DESCryptoServiceProvider();
break;
case SymmProvEnum.Rijndael:
mobjCryptoService = new RijndaelManaged();
break;
}
}
public static void ChangeCryptoService(SymmetricAlgorithm ServiceProvider)
{
mobjCryptoService = ServiceProvider;
}
private static byte[] GetLegalKey(string Key)
{
string sTemp;
if (mobjCryptoService.LegalKeySizes.Length > 0)
{
int lessSize = 0, moreSize = mobjCryptoService.LegalKeySizes[0].MinSize;
while (Key.Length * 8 > moreSize)
{
lessSize = moreSize;
moreSize += mobjCryptoService.LegalKeySizes[0].SkipSize;
}
sTemp = Key.PadRight(moreSize / 8, ' ');
}
else
sTemp = Key;
return ASCIIEncoding.ASCII.GetBytes(sTemp);
}
static void SetmobjCryptoService()
{
if (null == mobjCryptoService)
mobjCryptoService = new RijndaelManaged();
}
public static string Encrypting(string Source, string Key)
{
SetmobjCryptoService();
byte[] bytIn = System.Text.ASCIIEncoding.ASCII.GetBytes(Source);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
byte[] bytKey = GetLegalKey(Key);
mobjCryptoService.Key = bytKey;
mobjCryptoService.IV = bytKey;
ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
byte[] bytOut = ms.GetBuffer();
int i = 0;
for (i = 0; i < bytOut.Length; i++)
if (bytOut[i] == 0)
break;
return System.Convert.ToBase64String(bytOut, 0, i);
}
public static string Decrypting(string Source, string Key)
{
SetmobjCryptoService();
byte[] bytIn = System.Convert.FromBase64String(Source);
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, 0, bytIn.Length);
byte[] bytKey = GetLegalKey(Key);
mobjCryptoService.Key = bytKey;
mobjCryptoService.IV = bytKey;
ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
System.IO.StreamReader sr = new System.IO.StreamReader(cs);
return sr.ReadToEnd();
}
}
{
public enum SymmProvEnum : int
{
DES, Rijndael
}
private static SymmetricAlgorithm mobjCryptoService;
public static void ChangeCryptoService(SymmProvEnum NetSelected)
{
switch (NetSelected)
{
case SymmProvEnum.DES:
mobjCryptoService = new DESCryptoServiceProvider();
break;
case SymmProvEnum.Rijndael:
mobjCryptoService = new RijndaelManaged();
break;
}
}
public static void ChangeCryptoService(SymmetricAlgorithm ServiceProvider)
{
mobjCryptoService = ServiceProvider;
}
private static byte[] GetLegalKey(string Key)
{
string sTemp;
if (mobjCryptoService.LegalKeySizes.Length > 0)
{
int lessSize = 0, moreSize = mobjCryptoService.LegalKeySizes[0].MinSize;
while (Key.Length * 8 > moreSize)
{
lessSize = moreSize;
moreSize += mobjCryptoService.LegalKeySizes[0].SkipSize;
}
sTemp = Key.PadRight(moreSize / 8, ' ');
}
else
sTemp = Key;
return ASCIIEncoding.ASCII.GetBytes(sTemp);
}
static void SetmobjCryptoService()
{
if (null == mobjCryptoService)
mobjCryptoService = new RijndaelManaged();
}
public static string Encrypting(string Source, string Key)
{
SetmobjCryptoService();
byte[] bytIn = System.Text.ASCIIEncoding.ASCII.GetBytes(Source);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
byte[] bytKey = GetLegalKey(Key);
mobjCryptoService.Key = bytKey;
mobjCryptoService.IV = bytKey;
ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
byte[] bytOut = ms.GetBuffer();
int i = 0;
for (i = 0; i < bytOut.Length; i++)
if (bytOut[i] == 0)
break;
return System.Convert.ToBase64String(bytOut, 0, i);
}
public static string Decrypting(string Source, string Key)
{
SetmobjCryptoService();
byte[] bytIn = System.Convert.FromBase64String(Source);
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, 0, bytIn.Length);
byte[] bytKey = GetLegalKey(Key);
mobjCryptoService.Key = bytKey;
mobjCryptoService.IV = bytKey;
ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
System.IO.StreamReader sr = new System.IO.StreamReader(cs);
return sr.ReadToEnd();
}
}