首页 > 代码库 > 加密与解密
加密与解密
- 加密解密文件View Code
1 /// <summary> 2 /// Enctypt File 3 /// </summary> 4 /// <param name="sInputFilename"></param> 5 /// <param name="sOutputFilename"></param> 6 /// <param name="sKey"></param> 7 public void EncryptFile(string sInputFilename, string sOutputFilename, string sKey) 8 { 9 DESCryptoServiceProvider DES = new DESCryptoServiceProvider();10 //A 64 bit key and IV is required for this provider.11 //Set secret key For DES algorithm.12 DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);13 //Set initialization vector.14 DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);15 16 FileStream fin = null;17 FileStream fout = null;18 CryptoStream cryptoStream = null;19 try20 {21 fin = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);22 fout = new FileStream(sOutputFilename, FileMode.OpenOrCreate, FileAccess.Write);23 cryptoStream = new CryptoStream(fout, DES.CreateEncryptor(), CryptoStreamMode.Write);24 25 byte[] bin = new byte[100]; //This is intermediate storage for the decryption.26 long rdlen = 0; //This is the total number of bytes written.27 long totlen = fin.Length; //This is the total length of the input file.28 int len; //This is the number of bytes to be written at a time.29 30 //Read from the input file, then encrypt and write to the output file.31 while (rdlen < totlen)32 {33 len = fin.Read(bin, 0, 100);34 cryptoStream.Write(bin, 0, len);35 rdlen = rdlen + len;36 }37 }38 catch (Exception ex)39 {40 throw ex;41 }42 finally43 {44 if (cryptoStream != null) { cryptoStream.Close(); }45 if (fout != null) { fout.Close(); }46 if (fin != null) { fin.Close(); }47 }48 }49 50 /// <summary>51 /// Decrypt File52 /// </summary>53 /// <param name="sInputFilename"></param>54 /// <param name="sOutputFilename"></param>55 /// <param name="sKey"></param>56 public void DecryptFile(string sInputFilename, string sOutputFilename,string sKey)57 {58 DESCryptoServiceProvider DES = new DESCryptoServiceProvider();59 //A 64 bit key and IV is required for this provider.60 //Set secret key For DES algorithm.61 DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);62 //Set initialization vector.63 DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);64 65 FileStream fin = null;66 FileStream fout = null;67 CryptoStream cryptoStream = null;68 try69 {70 fin = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);71 fout = new FileStream(sOutputFilename, FileMode.OpenOrCreate, FileAccess.Write);72 cryptoStream = new CryptoStream(fin, DES.CreateDecryptor(), CryptoStreamMode.Read);73 74 byte[] bin = new byte[100]; //This is intermediate storage for the decryption.75 long rdlen = 0; //This is the total number of bytes written.76 long totlen = fin.Length; //This is the total length of the input file.77 int len; //This is the number of bytes to be written at a time.78 79 //Read from the input file, then encrypt and write to the output file.80 while (rdlen < totlen)81 {82 len = cryptoStream.Read(bin, 0, 100);83 if (len == 0) { break; }84 fout.Write(bin, 0, len);85 rdlen = rdlen + len;86 }87 }88 catch(Exception ex)89 {90 throw ex;91 }92 finally93 {94 if (cryptoStream != null) { cryptoStream.Close(); }95 if (fout != null) { fout.Close(); }96 if (fin != null) { fin.Close(); }97 }98 }
- 加密解密文本View Code
1 /// <summary> 2 /// Encrypt Text 3 /// </summary> 4 public string DesEncrypt(string pToEncrypt, string sKey) 5 { 6 MemoryStream ms = null; 7 CryptoStream ctyptoStream = null; 8 try 9 {10 DESCryptoServiceProvider des = new DESCryptoServiceProvider();11 des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);12 des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);13 14 byte[] inputByteArray = Encoding.ASCII.GetBytes(pToEncrypt);15 16 ms = new System.IO.MemoryStream();17 ctyptoStream = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);18 ctyptoStream.Write(inputByteArray, 0, inputByteArray.Length);19 ctyptoStream.FlushFinalBlock();20 }21 catch (Exception ex)22 {23 throw ex;24 }25 finally26 {27 if (ctyptoStream != null) { ctyptoStream.Close(); }28 if (ms != null) { ms.Close(); }29 }30 return Convert.ToBase64String(ms.ToArray());31 }32 33 /// <summary>34 /// Dectypt Text35 /// </summary>36 public string DesDecrypt(string pToDecrypt, string sKey)37 {38 MemoryStream ms = null;39 CryptoStream ctyptoStream = null;40 try41 {42 DESCryptoServiceProvider des = new DESCryptoServiceProvider();43 des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);44 des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);45 46 byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);47 48 ms = new System.IO.MemoryStream();49 ctyptoStream = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);50 ctyptoStream.Write(inputByteArray, 0, inputByteArray.Length);51 ctyptoStream.FlushFinalBlock();52 }53 catch (Exception ex)54 {55 throw ex;56 }57 finally58 {59 if (ctyptoStream != null) { ctyptoStream.Close(); }60 if (ms != null) { ms.Close(); }61 }62 return Encoding.ASCII.GetString(ms.ToArray());63 }
加密与解密
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。