Des encryption algorithms using Bouncycastle in. NET Core

Source: Internet
Author: User
Tags decrypt

The. NET core of Des and other encryption algorithms to wait until 1.2 to support, we urgently need the support of this algorithm, the article "Using Javascriptservice in. NET core implementation des encryption algorithm" needs to use Nodejs, many people think this is a little bad, Today we will introduce the Bouncycastle (portable.bouncycastle) https://www.nuget.org/packages/Portable.BouncyCastle/Library to provide us with the original. NET Core's support Library des algorithm. Bouncycastle's documents are less, and it took a long time to write out the. NET code equivalent of a package.

public class Tdesbouncy
{

Iblockcipher engine = new Desengine ();

       //<summary>
       / Use DES encryption, key input password, must use English characters, case-sensitive, and the number of characters is 8, can not be less than
       //</ Summary>
       //<param name= "plaintext" > string to be encrypted </param
       //<param name= "Keys" > Key </param> for encrypted strings;
        ///<returns> string </returns> after encryption;
         public string Encrypt (string keys, string plaintext)
         {

            byte[] ptbytes = Encoding.UTF8.GetBytes ( plaintext);
            byte[] RV = Encrypt (keys, ptbytes);
            StringBuilder ret = new StringBuilder ();
            foreach (byte b in rv)
             {
                 ret. AppendFormat ("{0:x2}", b);
           }
            return ret. ToString ();
       }

        Private byte[] Encrypt (string keys, byte[] ptbytes)
         {
            byte[] Key = Encoding.UTF8.GetBytes (keys);
            Bufferedblockcipher cipher = new Paddedbufferedblockcipher (new Cbcblockcipher (engine), New pkcs7padding ());
            cipher. Init (True, new Parameterswithiv (New Desparameters (key), key));
            byte[] rv = new Byte[cipher. Getoutputsize (Ptbytes.length)];
            int tam = cipher. Processbytes (ptbytes, 0, ptbytes.length, RV, 0);

Cipher. Dofinal (rv, TAM);
return RV;
}

<summary>
Use des decryption, key input password, must use English characters, case-sensitive, and the number of characters is 8, can not be more or less
</summary>
<param name= "Ciphertext" > strings that need to be encrypted </param>
<param name= "Keys" > Keys for Encrypted strings </param>
<returns> decrypted String </returns>
public string Decrypt (string keys, string ciphertext)
{
byte[] Inputbytearray = new BYTE[CIPHERTEXT.LENGTH/2];
for (int x = 0; x < CIPHERTEXT.LENGTH/2; × x + +)
{
int i = (Convert.ToInt32 (ciphertext.substring (x * 2, 2), 16));
INPUTBYTEARRAY[X] = (byte) i;
}
var RV = Decrypt (keys, inputbytearray);

Return Encoding.UTF8.GetString (RV);

}

Private byte[] Decrypt (string keys, byte[] ciphertext)
{
byte[] key = Encoding.UTF8.GetBytes (keys);
Bufferedblockcipher cipher = new Paddedbufferedblockcipher (new Cbcblockcipher (engine));
Cipher. Init (False, new Parameterswithiv (New Desparameters (key), key));
byte[] RV = new Byte[cipher. Getoutputsize (Ciphertext.length)];
int tam = cipher. Processbytes (ciphertext, 0, ciphertext.length, RV, 0);

Cipher. Dofinal (rv, TAM);

return RV;
}

}

public static void Main (string[] args)
{

Encoding.registerprovider (codepagesencodingprovider.instance);

string key = "Geffzhan";
String content = "This project.config Whoopla is a mess. So why they mean by. Netcore, you still has to reference everything correctly ";

Tdesbouncy bouncy = new tdesbouncy ();

var encrypt = bouncy. Encrypt (key, content);
Console.WriteLine (encrypt);

String descontent = bouncy. Decrypt (key, encrypt);
Console.WriteLine (descontent);
}

"This article was published by" [email protected] ", May 23, 2017"

Des encryption algorithms using Bouncycastle in. NET Core

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.