Encryption, decryption, encryption and decryption
/// <Summary> /// encryption /// </summary> /// <param name = "Text"> </param> /// <returns> </ returns> public static string Encrypt (string Text) {return Encrypt (Text, "MATICSOFT ");}
View Code
/// <Summary> /// encrypt data /// </summary> /// <param name = "Text"> </param> /// <param name = "sKey"> </param> // <returns> </returns> public static string Encrypt (string Text, string sKey) {DESCryptoServiceProvider des = new DESCryptoServiceProvider (); byte [] inputByteArray; inputByteArray = Encoding. default. getBytes (Text); des. key = ASCIIEncoding. ASCII. getBytes (System. web. security. formsAuthentication. hashPasswordForStoringInConfigFile (sKey, "md5 "). substring (0, 8); des. IV = ASCIIEncoding. ASCII. getBytes (System. web. security. formsAuthentication. hashPasswordForStoringInConfigFile (sKey, "md5 "). substring (0, 8); System. IO. memoryStream MS = new System. IO. 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);} return ret. toString ();}
View Code
/// <Summary> /// decrypt /// </summary> /// <param name = "Text"> </param> /// <returns> </ returns> public static string Decrypt (string Text) {return Decrypt (Text, "MATICSOFT ");}
View Code
/// <Summary> // decrypt the data /// </summary> /// <param name = "Text"> </param> /// <param name = "sKey"> </param> // <returns> </returns> public static string Decrypt (string Text, string sKey) {DESCryptoServiceProvider des = new DESCryptoServiceProvider (); int len; len = Text. length/2; byte [] inputByteArray = new byte [len]; int x, I; for (x = 0; x <len; x ++) {I = Convert. toInt32 (Text. substring (x * 2, 2), 16); inputByteArray [x] = (byte) I;} des. key = ASCIIEncoding. ASCII. getBytes (System. web. security. formsAuthentication. hashPasswordForStoringInConfigFile (sKey, "md5 "). substring (0, 8); des. IV = ASCIIEncoding. ASCII. getBytes (System. web. security. formsAuthentication. hashPasswordForStoringInConfigFile (sKey, "md5 "). substring (0, 8); System. IO. memoryStream MS = new System. IO. memoryStream (); CryptoStream cs = new CryptoStream (MS, des. createDecryptor (), CryptoStreamMode. write); cs. write (inputByteArray, 0, inputByteArray. length); cs. flushFinalBlock (); return Encoding. default. getString (ms. toArray ());}
View Code