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