. Net (C #) cryptographic decryption of AES and Des

Source: Internet
Author: User

  

<summary>
/// . NET Cryptographic decryption helper class
</summary>
public class Netcryptohelper
{
#region des implementations

<summary>
Des default key vector
</summary>
public static byte[] Desiv = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
<summary>
Des plus decryption key must be 8 bits
</summary>
Public Const string Deskey = "deskey8w";
<summary>
Get DES8 bit key
</summary>
<param name= "key" >des key string </param>
&LT;RETURNS&GT;DES8-bit key </returns>
Static byte[] Getdeskey (string key)
{
if (string. IsNullOrEmpty (Key))
{
throw new ArgumentNullException ("Key", "des key cannot be null");
}
if (key. Length > 8)
{
Key = key. Substring (0, 8);
}
if (key. Length < 8)
{
Less than 8 full complement
Key = key. PadRight (8, ' 0 ');
}
return Encoding.UTF8.GetBytes (key);
}
<summary>
DES encryption
</summary>
<param name= "Source" > Sources string </param>
<param name= "key" >des key, length must be 8 bits </param>
<param name= "IV" > Key vector </param>
<returns> string after encryption </returns>
public static string Encryptdes (string source, String key, byte[] IV)
{
using (DESCryptoServiceProvider Desprovider = new DESCryptoServiceProvider ())
{
byte[] Rgbkeys = Getdeskey (key),
Rgbivs = IV,
Inputbytearray = Encoding.UTF8.GetBytes (source);
using (MemoryStream MemoryStream = new MemoryStream ())
{
using (CryptoStream cryptostream = new CryptoStream (MemoryStream, Desprovider.createencryptor (RgbKeys, RgbIvs), CryptoStreamMode.Write))
{
Cryptostream.write (Inputbytearray, 0, inputbytearray.length);
Cryptostream.flushfinalblock ();
1. The first type of
Return convert.tobase64string (Memorystream.toarray ());

2. The second type of
StringBuilder result = new StringBuilder ();
foreach (Byte b in Memorystream.toarray ())
//{
Result. AppendFormat ("{0:x2}", b);
//}
return result. ToString ();
}
}
}
}
<summary>
Des decryption
</summary>
<param name= "Source" > Sources string </param>
<param name= "key" >des key, length must be 8 bits </param>
<param name= "IV" > Key vector </param>
<returns> decrypted String </returns>
public static string Decryptdes (string source, String key, byte[] IV)
{
using (DESCryptoServiceProvider Desprovider = new DESCryptoServiceProvider ())
{
byte[] Rgbkeys = Getdeskey (key),
Rgbivs = IV,
Inputbytearray = convert.frombase64string (source);
using (MemoryStream MemoryStream = new MemoryStream ())
{
using (CryptoStream cryptostream = new CryptoStream (MemoryStream, Desprovider.createdecryptor (RgbKeys, RgbIvs), CryptoStreamMode.Write))
{
Cryptostream.write (Inputbytearray, 0, inputbytearray.length);
Cryptostream.flushfinalblock ();
Return Encoding.UTF8.GetString (Memorystream.toarray ());
}
}
}
}

#endregion

#region AES Implementation

<summary>
AES decryption key must be 32 bits
</summary>
public static string Aeskey = "asekey32w";
<summary>
Get AES32 bit key
</summary>
<param name= "key" >aes key string </param>
&LT;RETURNS&GT;AES32-bit key </returns>
Static byte[] Getaeskey (string key)
{
if (string. IsNullOrEmpty (Key))
{
throw new ArgumentNullException ("Key", "AES key cannot be null");
}
if (key. Length < 32)
{
Less than 32 full complement
Key = key. PadRight (32, ' 0 ');
}
if (key. Length > 32)
{
Key = key. Substring (0, 32);
}
return Encoding.UTF8.GetBytes (key);
}
<summary>
AES Encryption
</summary>
<param name= "Source" > Sources string </param>
<param name= "key" >aes key, length must be 32 bits </param>
<returns> string after encryption </returns>
public static string Encryptaes (string source, string key)
{
using (Aescryptoserviceprovider Aesprovider = new Aescryptoserviceprovider ())
{
Aesprovider.key = Getaeskey (Key);
Aesprovider.mode = CIPHERMODE.ECB;
aesprovider.padding = PADDINGMODE.PKCS7;
using (ICryptoTransform cryptotransform = Aesprovider.createencryptor ())
{
byte[] Inputbuffers = Encoding.UTF8.GetBytes (source);
Byte[] results = cryptotransform.transformfinalblock (inputbuffers, 0, inputbuffers.length);
Aesprovider.clear ();
Aesprovider.dispose ();
Return convert.tobase64string (results, 0, results. Length);
}
}
}
<summary>
AES Decryption
</summary>
<param name= "Source" > Sources string </param>
<param name= "key" >aes key, length must be 32 bits </param>
<returns> decrypted String </returns>
public static string Decryptaes (string source, string key)
{
using (Aescryptoserviceprovider Aesprovider = new Aescryptoserviceprovider ())
{
Aesprovider.key = Getaeskey (Key);
Aesprovider.mode = CIPHERMODE.ECB;
aesprovider.padding = PADDINGMODE.PKCS7;
using (ICryptoTransform cryptotransform = Aesprovider.createdecryptor ())
{
byte[] Inputbuffers = convert.frombase64string (source);
Byte[] results = cryptotransform.transformfinalblock (inputbuffers, 0, inputbuffers.length);
Aesprovider.clear ();
return Encoding.UTF8.GetString (results);
}
}
}

#endregion
}

Class Program{ Static void Main(String[]Args) { StringPlainText= "Blog Park",EncryptString= Netcryptohelper.Encryptdes(PlainText, Netcryptohelper.Deskey, Netcryptohelper.Desiv); Console.WriteLine("String before des encryption: {0}",PlainText); Console.WriteLine("Des encrypted string: {0}",EncryptString); Console.WriteLine("Des decrypted string: {0}", Netcryptohelper.Decryptdes(EncryptString, Netcryptohelper.Deskey, Netcryptohelper.Desiv)); Console.WriteLine("-----------Split Line-----------"); Console.WriteLine("String before AES encryption: {0}",PlainText);EncryptString= Netcryptohelper.Encryptaes(PlainText, Netcryptohelper.Aeskey); Console.WriteLine ( "AES-encrypted string: {0}" , Encryptstring console. Writeline ( "AES Decrypted string: {0}" , netcryptohelper. Decryptaes (encryptstring, netcryptohelper. Aeskey console. Readkey}}          /span>                

. Net (C #) cryptographic decryption of AES and Des

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.