AES implements encryption and decryption of large file blocks

Source: Internet
Author: User
Tags crypt readfile

The Code is as follows:

 

Using system;
Using system. text;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. net;
Using system. net. Sockets;
Using system. Threading;
Using system. IO;
Using system. Security. cryptography;

Namespace videoencrypt
{
Class Program
{
Static void main (string [] ARGs)
{
Rijndaelmanaged rij = new rijndaelmanaged ();
Rij. keysize = 128;

String fp = @ "E: // friends // 3 // 3.mkv ";
String sphysicalfilepath = @ "E: // friends // 3 // o3.mkv ";
String fw = @ "E: // friends // 3 // dd3.mkv ";
Console. writeline ("encrypting begin ...");
Encryption (rij, FP, sphysicalfilepath );
Decryption (rij, sphysicalfilepath, Fw );

}
// Encryption function
Public static void encryption (rijndaelmanaged rij, string readfile, string writefile)
{
Try
{
Byte [] Key = rij. Key;
Byte [] IV = rij. IV;
Byte [] buffer = new byte [4096];
Rijndael crypt = Rijndael. Create ();
Icryptotransform transform = crypt. createencryptor (Key, IV );
// Write the file
Filestream fswrite = new filestream (writefile, filemode. Create );
Cryptostream cs = new cryptostream (fswrite, transform, cryptostreammode. Write );
// Open the file
Filestream fsread = new filestream (readfile, filemode. Open );
Int length;
// While (length = fsread. readbyte ())! =-1)
// CS. writebyte (byte) length );
While (length = fsread. Read (buffer, 0, 4096)> 0)
CS. Write (buffer, 0, (INT) length );

Fsread. Close ();
CS. Close ();
Fswrite. Close ();
Console. writeline ("encrypt success ");
}
Catch (exception E)
{
Console. writeline ("encrypt faile" + E. tostring ());
}
}
// Decryption Function
Public static void decryption (rijndaelmanaged rij, string readfile, string writefile)
{
Try
{
Byte [] Key = rij. Key;
Byte [] IV = rij. IV;
Byte [] buffer = new byte [4096];
Rijndael crypt = Rijndael. Create ();
Icryptotransform transform = crypt. createdecryptor (Key, IV );
// Read the encrypted file
Filestream fsopen = new filestream (readfile, filemode. Open );
Cryptostream cs = new cryptostream (fsopen, transform, cryptostreammode. Read );
// Write the decrypted result into the file
Filestream fswrite = new filestream (writefile, filemode. openorcreate );

Int length;
// While (length = cs. readbyte ())! =-1)
// Fswrite. writebyte (byte) length );
While (length = cs. Read (buffer, 0, 4096)> 0)
Fswrite. Write (buffer, 0, (INT) length );
Fswrite. Close ();
CS. Close ();
Fsopen. Close ();
Console. writeline ("decrypt success ");
}
Catch (exception E)
{
Console. writeline ("decrypt failed" + E. 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.