滚码加密
首先,感谢我的技术总监,夏总,在这里将滚码加密类做出整理
namespace WebTEST
{
public class EncriptManager
{
// Fields
private KeyManager _keyManager;
private SymCryptography _maskCriptor;
// Methods
public EncriptManager()
{
this._keyManager = new KeyManager();
this._maskCriptor = new SymCryptography(“TripleDES”);
this._maskCriptor.Key = “Web%HMKMJgfccg$%%^^&kkksl12312%&”;
}
public EncriptManager(string key)
{
this._keyManager = new KeyManager();
this._maskCriptor = new SymCryptography(“TripleDES”);
this._maskCriptor.Key = key;
}
public static void clearBytes(byte[] Buffer)
{
if (Buffer == null)
{
throw new ArgumentException(“Buffer”);
}
for (int num1 = 0; num1 <= (Buffer.Length – 1); num1++)
{
Buffer[num1] = 0;
}
}
public static byte[] createRandomSalt(int Length)
{
byte[] buffer1;
if (Length >= 1)
{
buffer1 = new byte[Length];
}
else
{
buffer1 = new byte[1];
}
RNGCryptoServiceProvider provider1 = new RNGCryptoServiceProvider();
provider1.GetBytes(buffer1);
return buffer1;
}
public string Decript(string str)
{
SymCryptography cryptography1 = new SymCryptography(“TripleDES”);
string text1 = this._maskCriptor.Decrypt(str);
if (text1.Length > 3)
{
int num1;
string text2 = text1.Substring(0, 3);
string text3 = text1.Substring(3, text1.Length – 3);
string text4 = text3;
if (int.TryParse(text2, out num1))
{
string text5 = this._keyManager.GetKey(num1);
cryptography1.Key = text5;
return cryptography1.Decrypt(text3);
}
}
return text1;
}
public string DecriptStatic(string str)
{
SymCryptography cryptography1 = new SymCryptography(“TripleDES”);
cryptography1.Key = “Web%HMKMJgfccg$%%^^&kkksl12312%&”;
return cryptography1.Decrypt(str);
}
public string DecriptStatic(string str, string seed)
{
SymCryptography cryptography1 = new SymCryptography(“TripleDES”);
cryptography1.Key = seed;
cryptography1.Salt = seed;
return cryptography1.Decrypt(str);
}
public string Encript(string str)
{
string text3;
SymCryptography cryptography1 = new SymCryptography(“TripleDES”);
Random random1 = new Random();
int num1 = random1.Next(0, 0x80);
string text1 = this._keyManager.GetKey(num1);
cryptography1.Key = text1;
string text2 = cryptography1.Encrypt(str);
if (num1 < 10)
{
text3 = “00” + num1.ToString();
}
else if (num1 < 100)
{
text3 = “0” + num1.ToString();
}
else
{
text3 = num1.ToString();
}
string text4 = text3.Substring(0, 3) + text2;
return this._maskCriptor.Encrypt(text4);
}
public string EncriptStatic(string str)
{
SymCryptography cryptography1 = new SymCryptography(“TripleDES”);
cryptography1.Key = “Web%HMKMJgfccg$%%^^&kkksl12312%&”;
return cryptography1.Encrypt(str);
}
public string EncriptStatic(string str, out string seed)
{
Random random1 = new Random();
int num1 = random1.Next(0x186a0, 0xf423f);
string text1 = num1.ToString();
SymCryptography cryptography1 = new SymCryptography(“TripleDES”);
cryptography1.Key = text1;
seed = text1;
return cryptography1.Encrypt(str);
}
public string EncriptStatic(string str, string seed)
{
SymCryptography cryptography1 = new SymCryptography(“TripleDES”);
cryptography1.Key = seed;
cryptography1.Salt = seed;
return cryptography1.Encrypt(str);
}
}
internal class Hash
{
// Methods
public Hash()
{
this.mCryptoService = new SHA1Managed();
}
public Hash(Hash.ServiceProviderEnum serviceProvider)
{
switch (serviceProvider)
{
case Hash.ServiceProviderEnum.SHA1:
{
this.mCryptoService = new SHA1Managed();
return;
}
case Hash.ServiceProviderEnum.SHA256:
{
this.mCryptoService = new SHA256Managed();
return;
}
case Hash.ServiceProviderEnum.SHA384:
{
this.mCryptoService = new SHA384Managed();
return;
}
case Hash.ServiceProviderEnum.SHA512:
{
this.mCryptoService = new SHA512Managed();
return;
}
case Hash.ServiceProviderEnum.MD5:
{
this.mCryptoService = new MD5CryptoServiceProvider();
return;
}
}
}
public Hash(string serviceProviderName)
{
try
{
this.mCryptoService = (HashAlgorithm)CryptoConfig.CreateFromName(serviceProviderName.ToUpper());
}
catch
{
throw;
}
}
public virtual string Encrypt(string plainText)
{
byte[] buffer1 = this.mCryptoService.ComputeHash(Encoding.ASCII.GetBytes(plainText + this.mSalt));
return Convert.ToBase64String(buffer1, 0, buffer1.Length);
}
// Properties
public string Salt { get; set; }
// Fields
private HashAlgorithm mCryptoService;
private string mSalt;
// Nested Types
public enum ServiceProviderEnum
{
SHA1,
SHA256,
SHA384,
SHA512,
MD5
}
}
internal class KeyManager
{
// Methods
public KeyManager()
{
this._keys = new List<string>();
this.Initialize();
}
public string GetKey(int index)
{
string text1 = “Webj~yriu!@*k0_^fa7431%p$#=@hd+&”;
if ((index >= 0) && (this._keys.Count > index))
{
text1 = this._keys[index];
}
return text1;
}
private void Initialize()
{
this._keys.Add(“Webj~yriu!@*k0_^fa7431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k$%^&*&*^%%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^fa7RTYUp$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^fa7431%KJHGFDFH&”);
this._keys.Add(“Webj~yr#$%FGH0_^fa7431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k%^&JH7431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^fa^%$#GJK#=@hd+&”);
this._keys.Add(“Webj~!@#$DFGH0_^fa7431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*kJGFERT&31%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^fa74HGDFGCVBhd+&”);
this._keys.Add(“Webj~yriu!@*k0_^fa7431%p$#=@&^%$”);
this._keys.Add(“Webj~yriu!^%$FGHfa7431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^*^%$DFGH$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^fa7431%pKJHTYd+&”);
this._keys.Add(“Webj~yriu4567jhgfa7431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^ohkhkguyt#=@hd+&”);
this._keys.Add(“Webj~yriu478uygufa7431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^f$FFDG%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^2345fds#$#=@hd+&”);
this._keys.Add(“Webj~yriu234fghqweq121%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k123124431%p$#=@hd+&”);
this._keys.Add(“Webj~yr2312234_^f34232%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0weqwecsd%p$#=@hd+&”);
this._keys.Add(“Webj~2312reyrtyrtygfd2312#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k534rsf431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^fa7431%p$#=@hd+&”);
this._keys.Add(“Webj~y34343gk0_^fa743534$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_3443341%p$#=@hd+&”);
this._keys.Add(“Webj~345345340_^fa743134534534+&”);
this._keys.Add(“Webj~yriu!@*34534534534p$#=@hd+&”);
this._keys.Add(“Webj~45345343434327431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^fa7431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0#$%^%#%$#%$#=@hd+&”);
this._keys.Add(“Webj~yr234234234237431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^fa342342342332+&”);
this._keys.Add(“Webj%TYTR$Y46yf4^%7431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu3242343fa7431%p$#=@hd+&”);
this._keys.Add(“Webj~yri4234k0_34232234p$#=@hd+&”);
this._keys.Add(“Webj~y^%$^^guyu^%^^&31%p$#=@hd+&”);
this._keys.Add(“Webj~yriu^%$&^YTYTU431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!234234237431%p$#=@hd+&”);
this._keys.Add(“Webj~yriuferertert7431%p$#=@hd+&”);
this._keys.Add(“Webj~y3423423423fdsfsdfp$#=@hd+&”);
this._keys.Add(“Webj~yriu!@34nvnnvn431%p$#=@hd+&”);
this._keys.Add(“Webj~yri343453453dddddddddd@hd+&”);
this._keys.Add(“Webj~yriu3434dfefer431%p$#=@hd+&”);
this._keys.Add(“Webj~y4534534534fernvtrgf#=@hd+&”);
this._keys.Add(“Webj~yri3453434erterf1%p$#=@hd+&”);
this._keys.Add(“Webj~yri34534ererter31%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^fa45343p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*345343453453453@hd+&”);
this._keys.Add(“Web3434534eertergdfgdf%p$#=@hd+&”);
this._keys.Add(“Webj~yr34534k0_^f34534345343hd+&”);
this._keys.Add(“Webj~3453434ererterterter#=@hd+&”);
this._keys.Add(“Webj~yriu34534343rertere$#=@hd+&”);
this._keys.Add(“Webj~yr3453453ererccchch$#=@hd+&”);
this._keys.Add(“Webj~yri3453453erterterter=@hd+&”);
this._keys.Add(“Webj~yriu34534534erterter#=@hd+&”);
this._keys.Add(“Webj~yri3534534ertertfgh$#=@hd+&”);
this._keys.Add(“Webj~yri3453453453453453343@hd+&”);
this._keys.Add(“Webj~yr3453434345345eertertehd+&”);
this._keys.Add(“Webj~yeterteghfghcghcghp$#=@hd+&”);
this._keys.Add(“Webj~yri3`12312234235343$#=@hd+&”);
this._keys.Add(“Webj~yriu2347567898791%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^fa98898p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@98689864431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^fa7itui56#=@hd+&”);
this._keys.Add(“Webj~yr8985uyitkkg7431%p$#=@hd+&”);
this._keys.Add(“Webj~857itedt0_^fa7431%p$#=@hd+&”);
this._keys.Add(“Webj~78778i78iyuigk86uuj$#=@hd+&”);
this._keys.Add(“Webj~yriu6juuyjyjgyu678657=@hd+&”);
this._keys.Add(“Webj~uyutyjyyuty678678%p$#=@hd+&”);
this._keys.Add(“Webj~yr4875675uyjuuyu1%p$#=@hd+&”);
this._keys.Add(“Web8567856iuyyujyuytyuup$#=@hd+&”);
this._keys.Add(“Webj~y6785678567uyityjtyuym@hd+&”);
this._keys.Add(“Webj57856785678jwq7431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^456345635#=@hd+&”);
this._keys.Add(“Webj~wrtwrtwk0_^fa7431%prtwrhd+&”);
this._keys.Add(“Webj~yriu!@*56363565ertyert@hd+&”);
this._keys.Add(“Webj~y3563546trrrev431%p$#=@hd+&”);
this._keys.Add(“Webj~y54634563563rr3sdfp$#=@hd+&”);
this._keys.Add(“Webj~yriu!563456345431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*krtwertwrtwp$#=@hd+&”);
this._keys.Add(“Webj~y456563456^fa7431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^435345643#=@hd+&”);
this._keys.Add(“Webj~yriu!@rtwreyetyt1%p$54@hd+&”);
this._keys.Add(“Webj~yriu!@*k0yetye431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^fartyetyereyed+&”);
this._keys.Add(“Webj~yriu345rtr5fa7431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_yeyertyer$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0356345635p$#=@hd+&”);
this._keys.Add(“Webj~yriureyrt_^fa7yetyety=@hd+&”);
this._keys.Add(“Webj~yriu!@*k043634561%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^fa74yetryet@hd+&”);
this._keys.Add(“Webj~yriu!ertyerty7431%p$#ryety&”);
this._keys.Add(“Webj~y45643654365456345p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_etye431%yetyehd+&”);
this._keys.Add(“Webj~yriu!@*k63arqqrw1%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@wertwrrdfsd%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k56yuouyio%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_mvvvvutuyuy=@hd+&”);
this._keys.Add(“Webj~yriu!@*ryuryury31%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^fafhjfjfgh=@hd+&”);
this._keys.Add(“Webj~yyuryurtyurtyuhfjngfh=@hd+&”);
this._keys.Add(“Web34534534ffffffa7431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_ewrwerwep$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^fa7431werwerhd+&”);
this._keys.Add(“Webj~yriu!@ccrwerw7431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*kcsadce3ennh$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^fa7431ntfvfcdd+&”);
this._keys.Add(“Webj~yrceddc2dc^fa7431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_dc2edc2dp$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^fa7431dc2edc2d+&”);
this._keys.Add(“Webj~yriu!c2dcedergtr1%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^fa74ferrff=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^fa7431%p$#fewd+&”);
this._keys.Add(“Webj~yricddw322235tg31%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^fa74frg3rtg3rd+&”);
this._keys.Add(“Webj~yrf2refewfwefwef1%p$#=@hd+&”);
this._keys.Add(“Webj~4rcdwfcw0_^fa7431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*kdcewfdewrfew#=@hd+&”);
this._keys.Add(“Webj~yriu!rwertwer7431%pgrgwgr+&”);
this._keys.Add(“Webj~yriu!@*k545346431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^fa743gergerggd+&”);
this._keys.Add(“Webj454534ggdfvvfdsf31%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^fa7452gfrewfvs+&”);
this._keys.Add(“Webj~yriu!@fwfsdcds431%p$#=@hd+&”);
this._keys.Add(“Webj~yrwregwergwga7431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^fwergwergw=@hd+&”);
this._keys.Add(“Webj~yrgwergwr_^fa7431%p$ewrger&”);
this._keys.Add(“Webj~yriu!@*k454ga7431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^ft25t425t2=@hd+&”);
this._keys.Add(“Webj~yrf2rffgeggfa7431%p$#=@hd+&”);
this._keys.Add(“Webj~y5g43gggbbeer7431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^fa4345g43g=@hd+&”);
this._keys.Add(“Webj~ygr43rgrgfgrtn431%p$#=@hd+&”);
this._keys.Add(“Webj~y35tgtegertgergertgt#=@hd+&”);
this._keys.Add(“Webj~yriurgergrnnythyt%p$#=@hd+&”);
this._keys.Add(“Webj~y54343g34g^fa7431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k123412331%p$#=@hd+&”);
this._keys.Add(“Webj~123431240_^f312412p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_123e43124312@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^faed31d12#=@hd+&”);
this._keys.Add(“Webj~yriu!@*kdsafd1edd%p$#=@hd+&”);
this._keys.Add(“Webj~yri14123dewasdasddqweded1d&”);
this._keys.Add(“Webj~yriu!@*k0_^fa7431dfasdfsad&”);
this._keys.Add(“Webj~yriu!@*k0_^fa7431%2d2d@hd+&”);
this._keys.Add(“Webj~yriwd@*k0_^f1d213d2$#=@hd+&”);
this._keys.Add(“Webj~yriu!dwqd2d3a7431%p$#=@hd+&”);
this._keys.Add(“Webj~yreqwdqd0_^d2desqddasd@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^fa7431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!23dd22127431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^fadsa1%p$#=@hd+&”);
this._keys.Add(“Webj~yri23@*k0_^fa7431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_sadasdasas#=@hd+&”);
this._keys.Add(“Webj~yrdu!@*k0_^fa7431%adsdad2+&”);
this._keys.Add(“Webj~yrisd@*k0_tyu743132d#=@hd3&”);
this._keys.Add(“Webj~yriu!@*k0_^fa7433%p$#=23d+&”);
this._keys.Add(“Webj~yriu!ask0_^fnhnh1%p$23@hd+&”);
this._keys.Add(“Webj~yriu!@*asdasd7431%pd#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^fa743hnp$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^fa74dtgbtnhnhd+&”);
this._keys.Add(“Webj~yrthbbgrbtgbtrbt1%p$#=@rd+&”);
this._keys.Add(“Web4bgriu!@*k0_^fa7431%p$#=@hd+&”);
this._keys.Add(“Webj~y4g4gb*k0_^fa7431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@gt4bgba7431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^f4tgb1%p$#=@hd+&”);
this._keys.Add(“Webj~gbiu!@*k0_^fa743gbgb#=@hd+&”);
this._keys.Add(“Webj~yriu!g4b0_^fa7431%p$#=@hd+&”);
this._keys.Add(“Webj~yrgb!@*k0_^fa7431%p$4g4gd+&”);
this._keys.Add(“Webj~yriugb*kgbgba7431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@4k0_^fg7431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*gb_^fab4t1%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0gtfa743gbp$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^ba7431%gb#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^f4gtb1%p$tg4hd+&”);
this._keys.Add(“Webj~yrig4rgrg_^fa743tg4$#=@b4tg”);
this._keys.Add(“Webj~yriu!@*k0f^fa7431%pb4tghd+&”);
this._keys.Add(“Webj~yriu!@*k0_ffa7431%p$#=@hd+&”);
this._keys.Add(“Web44yriu!@*k0_^ga7431%p$#=@hd+&”);
this._keys.Add(“Webj~g4gu!@*k0_^fdsf31%p$#=@hd+&”);
this._keys.Add(“Webj~yrirg@*k0_^fa74dfsp$#=@hd+&”);
this._keys.Add(“Webj~yriu!fg30_^fa7431%fgdfshd+&”);
this._keys.Add(“Webj~yriu!@*krfg3a7431%p$#=@dd+&”);
this._keys.Add(“Webj~yfiu!@*rg_^frgf31%p$#=@hsf&”);
this._keys.Add(“Webj~yriu!@*k0r^fa743rfefgfdgd+&”);
this._keys.Add(“Webj~yrdfg@*k0_^fa7431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!sdff_efg7431%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*kggsfafg31%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0_^df74e1%p$#=@hd+&”);
this._keys.Add(“Webj~yrifg@*k0_^fadsffgp$#=@hd+&”);
this._keys.Add(“Webj~yriu!s*k0_^fa743gdsf#=@hd+&”);
this._keys.Add(“Webj~yriu!@dfg_^fa7431%p$g=@hd+&”);
this._keys.Add(“Webj~yriu!@dk0sdfa7431%p$#dsfd+&”);
this._keys.Add(“Webj~yrgu!@*wf_^gsdf31%p$#=@hfds”);
this._keys.Add(“Webjfgrirewgk0wfda74df%p$#=@hd+&”);
this._keys.Add(“Webj~ysdu!@*k0_^fw7431sg$#=@hd+&”);
this._keys.Add(“Webj~yrifg@*fg_^fadf31%p$#=@hd+&”);
this._keys.Add(“Webj~yriu!sdfgwfga74dfrp$#=@hd+&”);
this._keys.Add(“Webj~yriu!@*k0sdfwf431%2$#=@hd+&”);
}
// Properties
public List<string> Keys
{
get
{
return this._keys;
}
}
// Fields
private List<string> _keys;
}
internal class SymCryptography
{
// Methods
public SymCryptography()
{
this.mKey = string.Empty;
this.mSalt = string.Empty;
this.mCryptoService = new RijndaelManaged();
this.mCryptoService.Mode = CipherMode.CBC;
this.mAlgorithm = SymCryptography.ServiceProviderEnum.Rijndael;
}
public SymCryptography(SymCryptography.ServiceProviderEnum serviceProvider)
{
this.mKey = string.Empty;
this.mSalt = string.Empty;
switch (serviceProvider)
{
case SymCryptography.ServiceProviderEnum.Rijndael:
{
this.mCryptoService = new RijndaelManaged();
this.mAlgorithm = SymCryptography.ServiceProviderEnum.Rijndael;
break;
}
case SymCryptography.ServiceProviderEnum.RC2:
{
this.mCryptoService = new RC2CryptoServiceProvider();
this.mAlgorithm = SymCryptography.ServiceProviderEnum.RC2;
break;
}
case SymCryptography.ServiceProviderEnum.DES:
{
this.mCryptoService = new DESCryptoServiceProvider();
this.mAlgorithm = SymCryptography.ServiceProviderEnum.DES;
break;
}
case SymCryptography.ServiceProviderEnum.TripleDES:
{
this.mCryptoService = new TripleDESCryptoServiceProvider();
this.mAlgorithm = SymCryptography.ServiceProviderEnum.TripleDES;
break;
}
}
this.mCryptoService.Mode = CipherMode.CBC;
}
public SymCryptography(string serviceProviderName)
{
this.mKey = string.Empty;
this.mSalt = string.Empty;
try
{
string text1;
if ((text1 = serviceProviderName.ToLower()) != null)
{
if (text1 != “rijndael”)
{
if (text1 == “rc2”)
{
goto Label_006C;
}
if (text1 == “des”)
{
goto Label_007C;
}
if (text1 == “tripledes”)
{
goto Label_008C;
}
}
else
{
serviceProviderName = “Rijndael”;
this.mAlgorithm = SymCryptography.ServiceProviderEnum.Rijndael;
}
}
goto Label_009A;
Label_006C:
serviceProviderName = “RC2”;
this.mAlgorithm = SymCryptography.ServiceProviderEnum.RC2;
goto Label_009A;
Label_007C:
serviceProviderName = “DES”;
this.mAlgorithm = SymCryptography.ServiceProviderEnum.DES;
goto Label_009A;
Label_008C:
serviceProviderName = “TripleDES”;
this.mAlgorithm = SymCryptography.ServiceProviderEnum.TripleDES;
Label_009A:
this.mCryptoService = (SymmetricAlgorithm)CryptoConfig.CreateFromName(serviceProviderName);
this.mCryptoService.Mode = CipherMode.CBC;
}
catch
{
throw;
}
}
public virtual string Decrypt(string cryptoText)
{
string text1;
byte[] buffer1 = Convert.FromBase64String(cryptoText);
byte[] buffer2 = this.GetLegalKey();
this.mCryptoService.Key = buffer2;
this.SetLegalIV();
ICryptoTransform transform1 = this.mCryptoService.CreateDecryptor(buffer2, this.mCryptoService.IV);
try
{
MemoryStream stream1 = new MemoryStream(buffer1, 0, buffer1.Length);
CryptoStream stream2 = new CryptoStream(stream1, transform1, CryptoStreamMode.Read);
StreamReader reader1 = new StreamReader(stream2);
text1 = reader1.ReadToEnd();
}
catch
{
text1 = null;
}
return text1;
}
public virtual string Encrypt(string plainText)
{
byte[] buffer1 = Encoding.ASCII.GetBytes(plainText);
byte[] buffer2 = this.GetLegalKey();
this.mCryptoService.Key = buffer2;
this.SetLegalIV();
ICryptoTransform transform1 = this.mCryptoService.CreateEncryptor(buffer2, this.mCryptoService.IV);
MemoryStream stream1 = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream1, transform1, CryptoStreamMode.Write);
stream2.Write(buffer1, 0, buffer1.Length);
stream2.FlushFinalBlock();
byte[] buffer3 = stream1.ToArray();
return Convert.ToBase64String(buffer3, 0, buffer3.GetLength(0));
}
public virtual byte[] GetLegalKey()
{
if (this.mCryptoService.LegalKeySizes.Length > 0)
{
int num1 = this.mKey.Length * 8;
int num2 = this.mCryptoService.LegalKeySizes[0].MinSize;
int num3 = this.mCryptoService.LegalKeySizes[0].MaxSize;
int num4 = this.mCryptoService.LegalKeySizes[0].SkipSize;
if (num1 > num3)
{
this.mKey = this.mKey.Substring(0, num3 / 8);
}
else if (num1 < num3)
{
int num5 = (num1 <= num2) ? num2 : ((num1 – (num1 % num4)) + num4);
if (num1 < num5)
{
this.mKey = this.mKey.PadRight(num5 / 8, ‘*’);
}
}
}
PasswordDeriveBytes bytes1 = new PasswordDeriveBytes(this.mKey, Encoding.ASCII.GetBytes(this.mSalt));
return bytes1.GetBytes(this.mKey.Length);
}
private void SetLegalIV()
{
if (this.mAlgorithm == SymCryptography.ServiceProviderEnum.Rijndael)
{
this.mCryptoService.IV = new byte[] { 15, 0x6f, 0x13, 0x2e, 0x35, 0xc2, 0xcd, 0xf9, 5, 70, 0x9c, 0xea, 0xa8, 0x4b, 0x73, 0xcc };
}
else
{
this.mCryptoService.IV = new byte[] { };
}
}
// Properties
public string Key { get; set; }
public string Salt { get; set; }
// Fields
private ServiceProviderEnum mAlgorithm;
private SymmetricAlgorithm mCryptoService;
private string mKey;
private string mSalt;
// Nested Types
public enum ServiceProviderEnum
{
Rijndael,
RC2,
DES,
TripleDES
}
}
}