C # implements DES Encryption and decryption, AES encryption and decryption

Source: Internet
Author: User

Introduction to des algorithm Description:

Des is the abbreviation for the data Encryption standard. It is a cryptographic algorithm developed by IBM, the United States National Standards Bureau published in 1977 to use it as a non-confidential department of Data encryption standards; It is a packet encryption algorithm that encrypts data in 64-bit groupings. Des is also a symmetric algorithm: the same algorithm is used for encryption and decryption. Its key length is 56 bits (because each 8th bit is Tochi parity), the key can be any number of 56 bits, and can be changed at any time.

<summary>
DES encryption
</summary>
<param name= "Data" > Encrypted plaintext </param>
<param name= "key" > Key </param>
<param name= "vector" > Vectors </param>
<returns> redaction </returns>
public static byte[] Desencrypt (byte[] Data, String Key, String Vector)
{
byte[] Bkey = new Byte[8];
Array.copy (Encoding.UTF8.GetBytes (Key.padright (bkey.length)), Bkey, bkey.length);
byte[] Bvector = new Byte[8];
Array.copy (Encoding.UTF8.GetBytes (Vector.padright (bvector.length)), Bvector, bvector.length);

byte[] cryptograph = null; Ciphertext after encryption

DESCryptoServiceProvider Encryptprovider = new DESCryptoServiceProvider ();
Encryptprovider.mode = CIPHERMODE.CBC;
encryptprovider.padding = Paddingmode.zeros;

Try
{
Open up a memory stream
using (MemoryStream Memory = new MemoryStream ())
{
Wraps the memory stream object into an encrypted stream object
using (CryptoStream encryptor = new CryptoStream (Memory,
Encryptprovider.createencryptor (Bkey, Bvector),
CryptoStreamMode.Write))
{
PlainText data is written to the encrypted stream
Encryptor.write (Data, 0, data.length);
Encryptor.flushfinalblock ();

Cryptograph = Memory.toarray ();
}
}
}
Catch
{
Cryptograph = null;
}

return cryptograph;
}

<summary>
Des decryption
</summary>
<param name= "Data" > Decrypted ciphertext </param>
<param name= "key" > Key </param>
<param name= "vector" > Vectors </param>
<returns> Clear </returns>
public static byte[] Desdecrypt (byte[] Data, String Key, String Vector)
{
byte[] Bkey = new Byte[8];
Array.copy (Encoding.UTF8.GetBytes (Key.padright (bkey.length)), Bkey, bkey.length);
byte[] Bvector = new Byte[8];
Array.copy (Encoding.UTF8.GetBytes (Vector.padright (bvector.length)), Bvector, bvector.length);

byte[] original = null;

DESCryptoServiceProvider CryptoProvider = new DESCryptoServiceProvider ();
Cryptoprovider.mode = CIPHERMODE.CBC;
cryptoprovider.padding = Paddingmode.zeros;

Try
{
Open up a memory stream and store the ciphertext
using (MemoryStream Memory = new MemoryStream (Data))
{
Wraps the memory stream object into an encrypted stream object
using (CryptoStream decryptor = new CryptoStream (Memory,
Cryptoprovider.createdecryptor (Bkey, Bvector),
CryptoStreamMode.Read))
{
Clear Text Storage Area
using (MemoryStream originalmemory = new MemoryStream ())
{
byte[] Buffer = new byte[1024];
Int32 readbytes = 0;
while ((Readbytes = Decryptor.read (Buffer, 0, buffer.length)) > 0)
{
Originalmemory.write (Buffer, 0, readbytes);
}

Original = Originalmemory.toarray ();
}
}
}
}
Catch
{
original = null;
}

return original;
}





Introduction to the AES algorithm description:

Des data Encryption Standard algorithm because the key length is small (56 bit), has not adapted to today's distributed open network to the data encryption security requirements, so 1997 NIST publicly solicit new data encryption standard, namely AES. After the screening of the Tri-rounds, the Rijndael algorithm submitted by the Belgian Joan Daeman and Vincent Rijmen was proposed as the final algorithm for AES. This algorithm will become a new data encryption standard in the United States and is widely used in various fields. While there is a different view of AES, in general, AES, as a new generation of data encryption standards, brings together the advantages of strong security, high performance, high efficiency, ease of use and flexibility. The AES design has three key lengths: 128,192,256 bits, whereas AES's 128 key is 1021 times times stronger than Des's 56 key.

<summary>
AES Encryption
</summary>
<param name= "Data" > Encrypted plaintext </param>
<param name= "key" > Key </param>
<param name= "vector" > Vectors </param>
<returns> redaction </returns>
public static byte[] Aesencrypt (byte[] Data, String Key, String Vector)
{
byte[] Bkey = new BYTE[32];
Array.copy (Encoding.UTF8.GetBytes (Key.padright (bkey.length)), Bkey, bkey.length);
byte[] Bvector = new BYTE[16];
Array.copy (Encoding.UTF8.GetBytes (Vector.padright (bvector.length)), Bvector, bvector.length);

byte[] cryptograph = null; Ciphertext after encryption

Rijndael Aes = Rijndael.create ();
Try
{
Open up a memory stream
using (MemoryStream Memory = new MemoryStream ())
{
Wraps the memory stream object into an encrypted stream object
using (CryptoStream encryptor = new CryptoStream (Memory,
Aes.createencryptor (Bkey, Bvector),
CryptoStreamMode.Write))
{
PlainText data is written to the encrypted stream
Encryptor.write (Data, 0, data.length);
Encryptor.flushfinalblock ();

Cryptograph = Memory.toarray ();
}
}
}
Catch
{
Cryptograph = null;
}

return cryptograph;
}

<summary>
AES Decryption
</summary>
<param name= "Data" > Decrypted ciphertext </param>
<param name= "key" > Key </param>
<param name= "vector" > Vectors </param>
<returns> Clear </returns>
public static byte[] Aesdecrypt (byte[] Data, String Key, String Vector)
{
byte[] Bkey = new BYTE[32];
Array.copy (Encoding.UTF8.GetBytes (Key.padright (bkey.length)), Bkey, bkey.length);
byte[] Bvector = new BYTE[16];
Array.copy (Encoding.UTF8.GetBytes (Vector.padright (bvector.length)), Bvector, bvector.length);

byte[] original = null; PlainText after decryption

Rijndael Aes = Rijndael.create ();
Try
{
Open up a memory stream and store the ciphertext
using (MemoryStream Memory = new MemoryStream (Data))
{
Wraps the memory stream object into an encrypted stream object
using (CryptoStream decryptor = new CryptoStream (Memory,
Aes.createdecryptor (Bkey, Bvector),
CryptoStreamMode.Read))
{
Clear Text Storage Area
using (MemoryStream originalmemory = new MemoryStream ())
{
byte[] Buffer = new byte[1024];
Int32 readbytes = 0;
while ((Readbytes = Decryptor.read (Buffer, 0, buffer.length)) > 0)
{
Originalmemory.write (Buffer, 0, readbytes);
}

Original = Originalmemory.toarray ();
}
}
}
}
Catch
{
original = null;
}

return original;
}

C # implements DES Encryption and decryption, AES encryption and decryption

Related Article

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.