Encryption
<summary> file encryption class uses DES to encrypt file flow </summary>
<param>deskey:des key; Desiv:des Vector </param>
Class encrypfile{
Public byte[] Deskey;
Public byte[] Desiv;
Public Encrypfile (byte[] inputkey,byte[] inputiv) {
Deskey=inputkey;
Desiv=inputiv;
}
<summary> Encryption Main Method </summary>
<param>inname: Encrypted filename; outname: encrypted filename </param>
public void Begintoencry (string inname,string outname) {
FileStream fin = new FileStream (inname, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream (outname, FileMode.OpenOrCreate, FileAccess.Write);
Fout. SetLength (0);
byte[] bin = new byte[100]; This is intermediate storage for the encryption.
Long Rdlen = 0; This is the total number of bytes written.
Long Totlen = Fin. Length; This is the total length of the input file.
int Len; This is the number of bytes to being written at a time.
Des des = new descryptoserviceprovider ();
CryptoStream encstream = new CryptoStream (Fout, Des. CreateEncryptor (Deskey, Desiv), cryptostreammode.write);
while (Rdlen < Totlen)
{
Len = Fin. Read (Bin, 0, 100);
Encstream.write (Bin, 0, Len);
Rdlen = Rdlen + len;
}
Encstream.close () ;
Fout. Close ();
Fin. Close ();
}
}
Encrypted character stream
//ptoencrypt is required to encrypt a string, Skey is a key
public string Encrypt (string Ptoencrypt, String sKey)
{
descryptoserviceprovider des = new DESCryptoServiceProvider ();
//the string into a byte array
byte[] Inputbytearray = Encoding.Default.GetBytes ( Ptoencrypt);
Establish key and vector for encrypted object
Des. Key = ASCIIEncoding.ASCII.GetBytes (SKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes (SKey);
MemoryStream ms = new MemoryStream ();
CryptoStream cs = new CryptoStream (MS, Des. CreateEncryptor (), cryptostreammode.write);
Cs. Write (Inputbytearray, 0, inputbytearray.length);
Cs. FlushFinalBlock ();
StringBuilder ret = new StringBuilder ();
foreach (Byte b in Ms.) ToArray ())
{
Ret. AppendFormat ("{0:x2}", b);
}
return ret. ToString ();
}