DES算法入口参数
DES算法的入口参数有三个:Key、Data、Mode。其中Key为7个字节共56位,是DES算法的工作密钥。Data为8个字节64位,是要被加密或解密的数据;Mode为DES的工作方法,有两种:加密或解密。
- 加密解密文件
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 try 20 { 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 finally 43 { 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 File 52 /// </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 try 69 { 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 finally 93 { 94 if (cryptoStream != null) { cryptoStream.Close(); } 95 if (fout != null) { fout.Close(); } 96 if (fin != null) { fin.Close(); } 97 } 98 }