C # implement 3DES encryption for 24-bit keys

Source: Internet
Author: User
Using System;
Using System. Collections. Generic;
Using System. text;
Using System. IO;
Using System. Security. cryptography;

Namespace Leleapplication1
{
Class Program
{
Public   Static   String Encrypt3des ( String A_strstring, String A_strkey)
{
Tripledescryptoserviceprovider des =   New Tripledescryptoserviceprovider ();

Des. Key=Asciiencoding. ASCII. getbytes (a_strkey );
Des. Mode=Ciphermode. ECB;

Icryptotransform desencrypt=Des. createencryptor ();

byte [] buffer = asciiencoding. ASCII. getbytes (a_strstring);
return convert. tobase64string (desencrypt. transformfinalblock (buffer, 0 , buffer. length);
}

Public static string decrypt3des ( string a_strstring, string a_strkey)
{< br> tripledescryptoserviceprovider des = New tripledescryptoserviceprovider ();

Des. Key=Asciiencoding. ASCII. getbytes (a_strkey );
Des. Mode=Ciphermode. ECB;
Des. padding=System. Security. cryptography. paddingmode. pkcs7;

Icryptotransform desdecrypt=Des. createdecryptor ();

String Result =   "" ;
Try
{
Byte [] Buffer = Convert. frombase64string (a_strstring );
Result = Asciiencoding. ASCII. getstring (desdecrypt. transformfinalblock (buffer, 0 , Buffer. Length ));
}
Catch (Exception E)
{

}< br> return result;
}

static void main ( string [] ARGs)
{< br> console. writeline (encrypt3des ( " 999999999 " , " # s ^ un2ye31 " );
console. readline ();
}< BR >}< br>

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.