.NET private key encryption

xiaoxiao2021-03-06  98

The encryption is currently two: private key and public key encryption, also known as symmetry and non-symmetry encryption. The private key encryption only uses a key to add recolve, because the same key is used for encryption and for decryption, Therefore, the private key is encrypted very fast (compared to the public key algorithm), it is especially suitable for encrypting the larger data stream. There are too many things to be introduced, so this time only the private key encryption, private key encryption, only for a block of encryption, that is, convert N-byte encryption to encrypted byte output, since It is encryption. Of course, a key, Right is needed to use a chain pattern called "Password Block Chain (CBC)" in .NET, it requires a key and an IV initialization vector, it is division Blocks are encrypted, and you must use IV vector to encrypt the first block, (everything must be a starting) Next, use the previous block to encrypt the next block until the reading is completed. In addition, send and receive the message, must be The value of the key and IV is conveyed and the key cannot be known for unauthorized users.

The advantages of private key encryption: fast. Disadvantages: patient friends can use the exhaustion method, huh, I can don't be patient. Usage: Usually use the public key encryption, encrypted in public key encryption, first pair public key encryption Then send the public key to the other party. Give the public key plus algorithm a aid. Once, look at the code below: (First build a text file in the execution file directory)

//

------------------------------ file encryption process

FILESTREAM FIN

=

New

FILESTREAM

"

INPUT.TXT

"

FileMode.Open, FileAccess.read; FileStream Fout

=

New

FILESTREAM

"

OUTPUT.TXT

"

Filemode.openorcreate, FileAccess.write;

//

Create an encrypted class

DES DES

=

New

DescryptoServiceProvider (); des.generateKey ();

//

Get randomly generated keys, and IV

DES.GENERATEIV ();

Byte

Keys

=

des.Key;

Byte

Ivs

=

DES.IV;

Long

Leng

=

FIN.LENGTH;

//

Get the length of the file

Long

Temp

=

0

;

//

Temporary variables

Byte

[] Buffer

=

New

Byte

[

400

];

//

Used to store file content in cache

CryptostReam CSTREAM

=

New

CryptostReam (Fout, Des.createEncryptor (Keys, IVS), CRYPTOSTREAMMODE.WRITE);

//

Create an add-on and write to the "Output.txt" file

While

(Temp

<

Leng)

{INT WLENG = FIN.READ (Buffer, 0,400); // Write buffer CSTREAM.WRITE (Buffer, 0, WLENG); // Write file Temp = Temp Leng; console.writeLine ("Encryption success"); Console.readline ();

CStream.close (); fout.close (); fin.close ();

Decryption:

//

----------------------------- Document decryption process

FILESTREAM FIN1

=

New

FILESTREAM

"

OUTPUT.TXT

"

FileMode.open, FileAccess.read; FileStream Fout1

=

New

FILESTREAM

"

Decryption .txt

"

Filemode.openorcreate, FileAccess.write;

//

Des Des1 = New DescryptoServiceProvider ();

//

DES1.GenerateKey ();

//

Get randomly generated keys, and IV

//

DES1.GENERATEIV ();

//

BYTE [] Keys1 = des1.key;

//

IVS1 = des1.IV;

Long

Leng1

=

Fin1.Length;

//

Get the length of the file

Long

Temp1

=

0

;

//

Temporary variables

Byte

[] Buffer1

=

New

Byte

[

400

];

//

Used to store file content in cache

CryptostReam CSTREAM1

=

New

CryptostReam (Fout1, Des.createdEcryptor (Keys, IVS), CRYPTOSTREAMMODE.WRITE);

//

Create an add-on and write to the "Output.txt" file

While

(TEMP1

<

Leng1)

{INT WLENG1 = FIN1.READ (Buffer1,0,400); // Write buffer cstractReam1.write (buffer1, 0, wleng1); // Write file temp1 = temp1 leng1; console.writeline ("Decryption success"); Console.readline ();

CStream.close (); fout.close (); fin.close ();

//

-------------------------- OVER

Encryption process with regard to the DES algorithm reference: http://www.vchome.net/tech/datastruct/datasf25.htm

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

New Post(0)