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 ());
}
}
}
}