1、方法一 (不可逆加密)srxljl
publicstringEncryptPassWord(stringPasswordString,stringPasswordFormat ) { stringencryptPassword=null; if(PasswordFormat="SHA1"){ encryptPassword=FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordString ,"SHA1"); } elseif (PasswordFormat="MD5") {
encryptPassword=FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordString ,"MD5"); } returnencryptPassword ; }
2、方法二 (可逆加密)srxljl
publicinterfaceIBindesh { stringencode(stringstr); stringdecode(stringstr); } publicclassEncryptionDecryption : IBindesh { publicstringencode(stringstr) { stringhtext=""; for(inti=0; i<str.Length; i++) { htext=htext+(char) (str[i]+10-1*2); } returnhtext; } publicstringdecode(stringstr) { stringdtext=""; for(inti=0; i<str.Length; i++) { dtext=dtext+(char) (str[i]-10+1*2); } returndtext; }
3、方法三 (可逆加密)srxljl
conststringKEY_64="VavicApp";//注意了,是8个字符,64位 conststringIV_64="VavicApp"; publicstringEncode(stringdata) { byte[] byKey=System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64); byte[] byIV=System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64); DESCryptoServicePRovider cryptoProvider=newDESCryptoServiceProvider(); inti=cryptoProvider.KeySize; MemoryStream ms=newMemoryStream(); CryptoStream cst=newCryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write); StreamWriter sw=newStreamWriter(cst); sw.Write(data); sw.Flush(); cst.FlushFinalBlock(); sw.Flush(); returnConvert.ToBase64String(ms.GetBuffer(),0, (int)ms.Length); } publicstringDecode(stringdata) { byte[] byKey=System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64); byte[] byIV=System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64); byte[] byEnc; try { byEnc=Convert.FromBase64String(data); } catch { returnnull; } DESCryptoServiceProvider cryptoProvider=newDESCryptoServiceProvider(); MemoryStream ms=newMemoryStream(byEnc); CryptoStream cst=newCryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read); StreamReader sr=newStreamReader(cst); returnsr.ReadToEnd(); }
4,md5(32位加密)srxljl
publicstringGetMD5(strings,string_input_charset) { ///<summary> ///与asp兼容的MD5加密算法 ///</summary> MD5 md5=newMD5CryptoServiceProvider(); byte[] t=md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(s)); StringBuilder sb=newStringBuilder(32); for(inti=0; i<t.Length; i++) { sb.Append(t[i].ToString("x").PadLeft(2,'0')); } returnsb.ToString(); }
(16位加密)srxljl
publicstaticstringGetMd5Str(stringConvertString) { MD5CryptoServiceProvider md5=newMD5CryptoServiceProvider(); stringt2=BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)),4,8); t2=t2.Replace("-",""); returnt2; }
5、加解文本文件srxljl
//加密文件 privatestaticvoidEncryptData(String inName, String outName,byte[] desKey,byte[] desIV) { //Create the file streams to handle the input and output files. FileStream fin=newFileStream(inName, FileMode.Open, Fileaccess.Read); FileStream fout=newFileStream(outName, FileMode.OpenOrCreate, FileAccess.Write); fout.SetLength(0); //Create variables to help with read and write. byte[] bin=newbyte[100];//This is intermediate storage for the encryption. longrdlen=0;//This is the total number of bytes written. longtotlen=fin.Length;//This is the total length of the input file. intlen;//This is the number of bytes to be written at a time. DES des=newDESCryptoServiceProvider(); CryptoStream encStream=newCryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write); //Read from the input file, then encrypt and write to the output file. while(rdlen<totlen) { len=fin.Read(bin,0,100); encStream.Write(bin,0, len); rdlen=rdlen+len; } encStream.Close(); fout.Close(); fin.Close(); } //解密文件 privatestaticvoidDecryptData(String inName, String outName,byte[] desKey,byte[] desIV) { //Create the file streams to handle the input and output files. FileStream fin=newFileStream(inName, FileMode.Open, FileAccess.Read); FileStream fout=newFileStream(outName, FileMode.OpenOrCreate, FileAccess.Write); fout.SetLength(0); //Create variables to help with read and write. byte[] bin=newbyte[100];//This is intermediate storage for the encryption. longrdlen=0;//This is the total number of bytes written. longtotlen=fin.Length;//This is the total length of the input file. intlen;//This is the number of bytes to be written at a time. DES des=newDESCryptoServiceProvider(); CryptoStream encStream=newCryptoStream(fout, des.CreateDecryptor(desKey, desIV), CryptoStreamMode.Write); //Read from the input file, then encrypt and write to the output file. while(rdlen<totlen) { len=fin.Read(bin,0,100); encStream.Write(bin,0, len); rdlen=rdlen+len; } encStream.Close(); fout.Close(); fin.Close(); }
新闻热点
疑难解答