. Net (C #): flexible use of cryptostream, encryption is not required with cryptostreammode. Write

Source: Internet
Author: User

First, icryptotransform in. NET is unidirectional, that is, data can only be converted from one state to another, and vice versa. Of course, manual operations on icryptotransform are complicated. You can use cryptostream to operate an icryptotransform more easily. Because icryptotransform is unidirectional, two icryptotransforms are required for encryption and decryption. This is created using the createencryptor and createdecryptor methods of the symmetricalgorithm class. Cryptostream is usually used to encrypt these two icryptotransforms. read is used for write and decryption. The name is reasonable, but the operation mode of cryptostream is not limited to this.

 

For the relationship between icryptotransform and cryptostream, see the figure below:

 

In fact, the so-called read and write operations only change the two data states of the icryptotransform operation, and the final operation is still to convert the source data of icryptotransform into the target data (one-way ), however, operations in different modes are different.

For example, we first use cryptostreammode. Write to encrypt:

// + Using system. IO;

// + Using system. Security. cryptography;

Static void main ()

{

Using (var aes = AES. Create ())

{

VaR DATA = encoding. Unicode. getbytes ("mgen! ");

Console. writeline (bitconverter. tostring (encrypt_write (AES. createencryptor (), data). toarray ()));

}

}

 

Static memorystream encrypt_write (icryptotransform ICT, byte [] data)

{

Using (var ms = new memorystream ())

Using (VAR cstream = new cryptostream (MS, ICT, cryptostreammode. Write ))

{

Cstream. Write (data, 0, Data. Length );

Return MS;

}

}

 

Encryption can also be completed using cryptostreammode. Read:

// + Using system. IO;

// + Using system. Security. cryptography;

Static void main ()

{

Using (var aes = AES. Create ())

{

VaR DATA = encoding. Unicode. getbytes ("mgen! ");

Console. writeline (bitconverter. tostring (encrypt_read (AES. createencryptor (), data). toarray ()));

}

}

 

Static memorystream encrypt_read (icryptotransform ICT, byte [] data)

{

US (var ms = new memorystream (data ))

Using (VAR cstream = new cryptostream (MS, ICT, cryptostreammode. Read ))

Using (VAR destms = new memorystream ())

{

Byte [] buffer = new byte [100];

Int readlen;

 

While (readlen = cstream. Read (buffer, 0,100)> 0)

Destms. Write (buffer, 0, readlen );

Return destms;

}

}

 

Both of them encrypt the data and enter:

DB-76-CA-5F-62-2C-59-C1-91-48-79-9F-10-B9-8E-2D

The above only talked about encryption, decryption is the same, cryptostreammode. Write or read can be decrypted!

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.