Descryptoserviceprovider encryption and decryption

Source: Internet
Author: User

Public String encrypt (string stringtoencrypt, string skey)
{
Descryptoserviceprovider des = new descryptoserviceprovider ();
Byte [] inputbytearray = encoding. getencoding ("UTF-8"). getbytes (stringtoencrypt );
Des. Key = asciiencoding. utf8.getbytes (skey );
Des. IV = asciiencoding. utf8.getbytes (skey );
Memorystream MS = new memorystream ();
Cryptostream cs = new cryptostream (MS, Des. createencryptor (), cryptostreammode. Write );

CS. Write (inputbytearray, 0, inputbytearray. Length );
CS. flushfinalblock ();

Stringbuilder ret = new stringbuilder ();
Foreach (byte B in ms. toarray ())
{
Ret. appendformat ("{0: X2}", B );
}
Ret. tostring ();
Return ret. tostring ();
}

Public String decrypt (string stringtodecrypt, string skey)
{
Descryptoserviceprovider des = new descryptoserviceprovider ();

Byte [] inputbytearray = new byte [stringtodecrypt. Length/2];
For (INT x = 0; x <stringtodecrypt. Length/2; X ++)
{
Int I = (convert. toint32 (stringtodecrypt. substring (x * 2, 2), 16 ));
Inputbytearray [x] = (byte) I;
}

Des. Key = asciiencoding. utf8.getbytes (skey );
Des. IV = asciiencoding. utf8.getbytes (skey );
Memorystream MS = new memorystream ();
Cryptostream cs = new cryptostream (MS, Des. createdecryptor (), cryptostreammode. Write );
CS. Write (inputbytearray, 0, inputbytearray. Length );
CS. flushfinalblock ();

Stringbuilder ret = new stringbuilder ();

Return System. Text. encoding. Default. getstring (Ms. toarray ());
}

 

 

//

 

Public sealed class desencrypt
...{
Private desencrypt ()
...{
//
// Todo: add the constructor logic here
//
}

Private Static string key = "zhoufoxcn ";

/** // <Summary>
/// Symmetric encryption and decryption key
/// </Summary>
Public static string key
...{
Get
...{
Return key;
}
Set
...{
Key = value;
}
}

/** // <Summary>
/// DES encryption
/// </Summary>
/// <Param name = "encryptstring"> </param>
/// <Returns> </returns>
Public static string desencrypt (string encryptstring)
...{
Byte [] keybytes = encoding. utf8.getbytes (key. substring (0, 8 ));
Byte [] keyiv = keybytes;
Byte [] inputbytearray = encoding. utf8.getbytes (encryptstring );
Descryptoserviceprovider provider = new descryptoserviceprovider ();
Memorystream mstream = new memorystream ();
Cryptostream cstream = new cryptostream (mstream, provider. createencryptor (keybytes, keyiv), cryptostreammode. Write );
Cstream. Write (inputbytearray, 0, inputbytearray. Length );
Cstream. flushfinalblock ();
Return convert. tobase64string (mstream. toarray ());
}

/** // <Summary>
/// Des decryption
/// </Summary>
/// <Param name = "decryptstring"> </param>
/// <Returns> </returns>
Public static string desdecrypt (string decryptstring)
...{
Byte [] keybytes = encoding. utf8.getbytes (key. substring (0, 8 ));
Byte [] keyiv = keybytes;
Byte [] inputbytearray = convert. frombase64string (decryptstring );
Descryptoserviceprovider provider = new descryptoserviceprovider ();
Memorystream mstream = new memorystream ();
Cryptostream cstream = new cryptostream (mstream, provider. createdecryptor (keybytes, keyiv), cryptostreammode. Write );
Cstream. Write (inputbytearray, 0, inputbytearray. Length );
Cstream. flushfinalblock ();
Return encoding. utf8.getstring (mstream. toarray ());
}
}

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.