Cryptography - symmetrical encryption (below) in .NET

zhaozj2021-02-16  57

Upload http://www.9cbs.net/develop/read_article.asp?id=23386

Code example

Now we have enough information about SymmetricalGorithm. Finally, let's take a look at the code snippet to be encoded and decoded. I assume that you have a form that contains txtdata and command button controls. Write the following code in the code event of the command button. This code will encrypt the text in TextBox and display with MessageBox, write the encrypted result back to TextBox.

Symmetricalgorithm mcryptprov;

Memorystream mmemstr;

/ / Encrypt the data in txtdata, then display the encrypted result with MessageBox and write back to TextBox

/ / Here you can configure any .NET supported class

DES mcryptprov = symmetricalgorithm.create ("rijndael");

// Encrypted data will be stored in the form of a stream so we need memory Stream objects.

MMEMSTR = new memorystream ();

// Create an IcryPtTransform object. (Let us use the default key and initial vector).

Icrypttramsform mtransform = mcryptProv.createEncryptor ();

Cryptostream McSwriter = New CryptostReam (MMEMSTR, MTRANSFORM, CRYPTOSTREAMMODE.WRITE);

Streamwriter mswriter = streamwriter (mcswriter);

Mswriter.writer (this.txtdata.text);

mswriter.flush ();

Mcswriter.flushfinalblock ();

There is a thing you need to pay attention to us using IV and keys anywhere in your code. In fact, when we don't specify them in your code .NET Framework will automatically generate us. However, the example code contained herein uses the key and IV specified by the user. We use the data after encryption to use MemoryStream to memory. Now let's get the code from the memory.

// Data has been written to memory but we need to return to TextBox and MessageBox, so we must do the following work.

/ / Create a byte array for accepting data.

Byte [] mbytes = new byte [mmemstr.length - 1];

MMEMSTR.POSITION = 0;

MMEMSTR.READ (mbytes, 0, mmemstr.length);

Text.utf8encoding menc = new text.utf8encoding ();

String mencdata = menc.getstring (mbytes);

Messagebox.show ("Encrypted Data is: / N" Mencdata);

THIS.TXTDATA.TEXT = MENCDATA;

Switching from byte to strings must be encoded. Here I used UTF8encoding. Finally, let's display the decrypted data again in MessageBox and TextBox.

/ / Now let us get the decrypted data from memory

// Because our data is in memory, we need to reuse the MemoryStream object.

// Place the memory point 0

MMEMSTR.POSITION = 0;

MTRANSFORM = mcryptProv.createDecryptor ();

Cryptostream McSreader = New Cryptostream (MMEMSTR, MTRANSFORM, CRYPTOSTREAMMODE.READ);

StreamReader mstrreader = new streamreader; string mdecdata = mstrreader.readToend ();

MessageBox ("Decryption data is: / N" mDecdata);

THIS.TXTDATA.TEXT = MDECDATA;

This is all work. Deciphering those data we used the same memory stream. In order to be able to read data from the stream, we first set it first. Then we create an icryptotransform object with the CreateDecryptor method of the Symmetricalgorithm object. The object (MMEMSTR) is reused in the code. You can create new objects (using new variables). Then we need a StreamReader object to read data from memory. While Reading That It Will Also Decrypt That Data Since We Passed CRYPTOSTREAM Object During The Creation of StreamReader Object.

Last words

.NET provides us with a very good managed way to protect our data. We can use a class of .NET built-in class to encrypt our data. Although there are many kinds of background still use Crypto API APIS technology, we use old Crypto APIs without any problems. But we can use these classes without worrying about the specific implementation of those classes. In the following article I will describe myths and uses of the asymmetric encryption algorithm.

About an example

The example of this article allows you to select an algorithm to encrypt or decrypt data. And it also allows you to specify your own IV and keys. The code works in two ways. One is TextBox means that you write something in TextBox and then encrypt or decrypt these content. Second, you can select files to encrypt or decrypt.

转载请注明原文地址:https://www.9cbs.com/read-23911.html

New Post(0)