Md5helper helper class

Source: Internet
Author: User

Des encryption and decryption

 

Public class md5helper {// DES encryption // skey Public String md5encrypt (string ptoencrypt, string skey) {descryptoserviceprovider des = new descryptoserviceprovider (); byte [] inputbytearray = encoding. default. getbytes (ptoencrypt); des. key = asciiencoding. ASCII. getbytes (skey); des. IV = asciiencoding. ASCII. getbytes (skey); memorystream MS = new memorystream (); cryptostream cs = new cryptostream (MS, Des. cr Eateencryptor (), 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 () ;}// des decryption Public String md5decrypt (string ptodecrypt, string skey) {descryptoserviceprovider des = new descryptoserviceprovider (); BYT E [] inputbytearray = new byte [ptodecrypt. length/2]; for (INT x = 0; x <ptodecrypt. length/2; X ++) {int I = (convert. toint32 (ptodecrypt. substring (x * 2, 2), 16); inputbytearray [x] = (byte) I;} des. key = asciiencoding. ASCII. getbytes (skey); des. IV = asciiencoding. ASCII. getbytes (skey); memorystream MS = new memorystream (); cryptostream cs = new cryptostream (MS, Des. createdecryptor (), cryptostrea Mmode. write); CS. write (inputbytearray, 0, inputbytearray. length); CS. flushfinalblock (); stringbuilder ret = new stringbuilder (); return system. text. encoding. default. getstring (Ms. toarray ();} Public String md5encrypt (string ptoencrypt, string skey, out string MSG) {descryptoserviceprovider des = new descryptoserviceprovider (); stringbuilder ret = new stringbuilder (); try {byte [] inputbytearray = Encoding. default. getbytes (ptoencrypt); MSG = "succ:"; int length = skey. length; If (length <8) {MSG = "Err: The key length cannot be less than 8"; Return "" ;}int _ DIF = length-8; int _ sub_index = _ DIF/2 + _ DIF % 2; _ sub_index = _ sub_index <= 0? 0: _ sub_index; skey = skey. substring (_ sub_index, 8); des. key = asciiencoding. ASCII. getbytes (skey); des. IV = asciiencoding. ASCII. getbytes (skey); memorystream MS = new memorystream (); cryptostream cs = new cryptostream (MS, Des. createencryptor (), cryptostreammode. write); CS. write (inputbytearray, 0, inputbytearray. length); CS. flushfinalblock (); foreach (byte B in ms. toarray () {ret. appendformat ("{0: X2} ", B) ;}} catch (exception ex) {MSG =" Err: "+ ex. message;} return ret. tostring () ;}// des decryption Public String md5decrypt (string ptodecrypt, string skey, out string MSG) {descryptoserviceprovider des = new descryptoserviceprovider (); string _ value = ""; try {MSG = "succ:"; int length = skey. length; If (length <8) {MSG = "Err: The key length cannot be less than 8"; Return "" ;}int _ DIF = length-8; int _ sub_index = _ DIF/2 + _ DIF % 2; _ sub_index = _ sub_index <= 0? 0: _ sub_index; skey = skey. substring (_ sub_index, 8); byte [] inputbytearray = new byte [ptodecrypt. length/2]; for (INT x = 0; x <ptodecrypt. length/2; X ++) {int I = (convert. toint32 (ptodecrypt. substring (x * 2, 2), 16); inputbytearray [x] = (byte) I;} des. key = asciiencoding. ASCII. getbytes (skey); des. IV = asciiencoding. ASCII. 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 (); _ value = encoding. default. getstring (Ms. toarray ();} catch (exception ex) {MSG = "Err:" + ex. message;} return _ value ;}}

Call Method

 MD5Helper md5 = new MD5Helper(); string key = ConfigurationManager.AppSettings["MD5Encrypt"]; loginPassword = md5.MD5Encrypt(loginPassword, key);

 

Md5helper helper class

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.