Share 3des encryption c#

huynhbao.tien

New member
### Mã hóa 3DES trong C#

3DES (Triple DES) là một mật mã khối quan hệ đối xứng được sử dụng để mã hóa và giải mã dữ liệu.Nó là một biến thể của mật mã DES (tiêu chuẩn mã hóa dữ liệu) và nó an toàn hơn DES vì nó sử dụng ba khóa riêng biệt thay vì một.

3DES được triển khai trong `System.Security.Cryptography` không gian tên trong C#.Mã sau đây cho thấy cách mã hóa và giải mã một chuỗi bằng 3DE:

`` `C#
sử dụng System.Security.Cryptography;

Chuỗi plaintext = "Đây là một tin nhắn bí mật";

// Tạo đối tượng TripleDesCryptoServiceProvider.
TripleSesCryptoServiceProvider Crypto = New TripdeScryptoServiceProvider ();

// Đặt chế độ mã hóa thành ciphermode.ecb.
crypto.mode = ciphermode.ecb;

// Đặt chế độ đệm thành paddingmode.pkcs7.
crypto.padding = paddingmode.pkcs7;

// Tạo một đối tượng chính.
byte [] key = new byte [] {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};

// Tạo một đối tượng IV.
byte [] iv = new byte [] {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};

// Mã hóa các bản rõ.
Byte [] CIPHERTEXT = crypto.encrypt (plaintext, key, iv);

// giải mã mật mã.
Chuỗi plaintext2 = crypto.decrypt (mã hóa, khóa, iv);

// In bản rõ.
Console.WriteLine (PlainText2);
`` `

### hashtags

* #3des
* #encryption
* #C#
* #Cryptography
* #bảo vệ
=======================================
### 3DES Encryption in C#

3DES (Triple DES) is a symmetric-key block cipher that is used to encrypt and decrypt data. It is a variant of the DES (Data Encryption Standard) cipher, and it is more secure than DES because it uses three separate keys instead of one.

3DES is implemented in the `System.Security.Cryptography` namespace in C#. The following code shows how to encrypt and decrypt a string using 3DES:

```c#
using System.Security.Cryptography;

string plaintext = "This is a secret message";

// Create a TripleDESCryptoServiceProvider object.
TripleDESCryptoServiceProvider crypto = new TripleDESCryptoServiceProvider();

// Set the encryption mode to CipherMode.ECB.
crypto.Mode = CipherMode.ECB;

// Set the padding mode to PaddingMode.PKCS7.
crypto.Padding = PaddingMode.PKCS7;

// Create a key object.
byte[] key = new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };

// Create a IV object.
byte[] iv = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };

// Encrypt the plaintext.
byte[] ciphertext = crypto.Encrypt(plaintext, key, iv);

// Decrypt the ciphertext.
string plaintext2 = crypto.Decrypt(ciphertext, key, iv);

// Print the plaintext.
Console.WriteLine(plaintext2);
```

### Hashtags

* #3des
* #encryption
* #C#
* #Cryptography
* #Security
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top