[★] Encrypt and decrypt local documents

Source: Internet
Author: User

using system;
using system. collections. generic;
using system. componentmodel;
using system. data;
using system. drawing;
using system. text;
using system. windows. forms;
using system. security. cryptography;
using system. io;
namespace jiami
{< br> Public partial class form1: form
{< br> // represents the abstract base class inherited by all symmetric algorithms .
Public repeated ricalgorithm symm = NULL;

Public form1 ()
{
Symm = new rijndaelmanaged ();

Initializecomponent ();
}

Private void button#click (Object sender, eventargs E)
{
Try
{
String file = @ "F: \ 3.txt"; // original file

String tempfile = @ "F: \ 4.txt"; // path of the encrypted file

Filestream FSIN = file. Open (file, filemode. Open, fileaccess. Read );

Filestream fsout = file. Open (tempfile, filemode. Open, fileaccess. Write );

// defines symmetric algorithm object instances and interfaces
icryptotransform transform = symm. createencryptor ();
cryptostream cstream = new cryptostream (fsout, transform, cryptostreammode. write);

binaryreader BR = new binaryreader (FSIN);
// read the source file to cryptostream
cstream. write (BR. readbytes (INT) FSIN. length), 0, (INT) FSIN. length);
cstream. flushfinalblock ();
cstream. close ();

fsout. Close ();
FSIN. Close ();
This. textbox2.text = tempfile;

}< br> catch (exception ex)
{< br> throw ex;
}< br> private void button3_click (Object sender, eventargs e)
{< br> string tempfile = This. textbox2.text;

Filestream FSIN = file. Open (tempfile, filemode. Open, fileaccess. Read );
// Define symmetric algorithm object instances and interfaces
 
Icryptotransform transform = symm. createdecryptor ();

Cryptostream cstream = new cryptostream (FSIN, transform, cryptostreammode. Read );

Streamreader sr = new streamreader (cstream );
This. textbox3.text = Sr. readtoend ();
FSIN. Close ();
}

}< BR >}

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.