Code for character encryption & file encryption

Source: Internet
Author: User
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 ();
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.