C # encryption and decryption algorithm.

Source: Internet
Author: User
1. method 1 (irreversible encryption) public string EncryptPassword (string PasswordString, string PasswordFormat) {string encryptPassword = null; if (PasswordFormat = "SHA1") {encryptPassword = FormsAuthortication. hashPasswordForStoringInConfigFile (PasswordString, "SHA1");} elseif (PasswordFormat = "MD5") {encryptPassword = FormsAuthortication. hashPasswordForStoringInConfigFile (PasswordString, "MD5");} return encryptPassword;} 2. method 2 (reversible encryption) public interface IBindesh {string encode (string str ); string decode (string str);} public class EncryptionDecryption: IBindesh {public string encode (string str) {string htext = ""; for (int I = 0; I <str. length; I ++) {htext = htext + (char) (str [I] + 10-1*2);} return htext;} public string decode (string str) {string dtext = ""; for (int I = 0; I <str. length; I ++) {dtext = dtext + (char) (str [I]-10 + 1*2);} return dtext;} 3. Method 3 (reversible encryption) const string KEY_64 = "VavicApp"; // note that it is an 8-character, 64-bit const string IV_64 = "VavicApp"; public string Encode (string data) {byte [] byKey = System. text. ASCIIEncoding. ASCII. getBytes (KEY_64); byte [] byIV = System. text. ASCIIEncoding. ASCII. getBytes (IV_64); DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider (); int I = cryptoProvider. keySize; MemoryStream MS = new MemoryStream (); CryptoStream cst = new CryptoStream (MS, cryptoProvider. createEncryptor (byKey, byIV), CryptoStreamMode. write); StreamWriter sw = new StreamWriter (cst); sw. write (data); sw. flush (); cst. flushFinalBlock (); sw. flush (); return Convert. toBase64String (ms. getBuffer (), 0, (int) ms. length);} public string Decode (string data) {byte [] byKey = System. text. ASCIIEncoding. ASCII. getBytes (KEY_64); byte [] byIV = System. text. ASCIIEncoding. ASCII. getBytes (IV_64); byte [] byEnc; try {byEnc = Convert. fromBase64String (data);} catch {return null;} DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider (); MemoryStream MS = new MemoryStream (byEnc); CryptoStream cst = new CryptoStream (MS, cryptoProvider. createDecryptor (byKey, byIV), CryptoStreamMode. read); StreamReader sr = new StreamReader (cst); return sr. readToEnd ();} 4. MD5 irreversible encryption (32-bit encryption) public string GetMD5 (string s, string _ input_charset) {/** // <summary> // MD5 encryption algorithm compatible with ASP /// </summary> MD5 md5 = new MD5CryptoServiceProvider (); byte [] t = md5.ComputeHash (Encoding. getEncoding (_ input_charset ). getBytes (s); StringBuilder sb = new StringBuilder (32); for (int I = 0; I <t. length; I ++) {sb. append (t [I]. toString ("x "). padLeft (2, '0');} return sb. toString () ;}( 16-bit encryption) public static string GetMd5Str (string ConvertString) {MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider (); string t2 = BitConverter. toString (md5.ComputeHash (UTF8Encoding. default. getBytes (ConvertString), 4, 8); t2 = t2.Replace ("-", ""); return t2 ;} 5. encryption of text files // encryption of private static void EncryptData (String inName, String outName, byte [] cipher ey, byte [] desIV) {// Create the file streams to handle the input and output files. fileStream fin = new FileStream (inName, FileMode. open, FileAccess. read); FileStream fout = new FileStream (outName, FileMode. openOrCreate, FileAccess. write); fout. setLength (0); // Create variables to help with read and write. byte [] bin = new byte [100]; // This is intermediate storage for the encryption. long rdlen = 0; // This is the total number of bytes written. long totlen = fin. length; // This is the total length of the input file. int len; // This is the number of bytes to be written at a time. DES des = new DESCryptoServiceProvider (); CryptoStream encStream = new CryptoStream (fout, des. createEncryptor (Cipher ey, desIV), CryptoStreamMode. write); // Read from the input file, then encrypt and write to the output file. while (rdlen <totlen) {len = fin. reads (bin, 0,100); encStream. write (bin, 0, len); rdlen = rdlen + len;} encStream. close (); fout. close (); fin. close () ;}// decrypt the private static void DecryptData (String inName, String outName, byte [] cipher ey, byte [] desIV) File) {// Create the file streams to handle the input and output files. fileStream fin = new FileStream (inName, FileMode. open, FileAccess. read); FileStream fout = new FileStream (outName, FileMode. openOrCreate, FileAccess. write); fout. setLength (0); // Create variables to help with read and write. byte [] bin = new byte [100]; // This is intermediate storage for the encryption. long rdlen = 0; // This is the total number of bytes written. long totlen = fin. length; // This is the total length of the input file. int len; // This is the number of bytes to be written at a time. DES des = new DESCryptoServiceProvider (); CryptoStream encStream = new CryptoStream (fout, des. createDecryptor (Cipher ey, desIV), CryptoStreamMode. write); // Read from the input file, then encrypt and write to the output file. while (rdlen <totlen) {len = fin. reads (bin, 0,100); encStream. write (bin, 0, len); rdlen = rdlen + len;} encStream. close (); fout. close (); fin. close () ;}6, using System; using System. collections. generic; using System. text; using System. security. cryptography; using System. IO; namespace Component {public class Security {public Security () {}// default key vector private static byte [] Keys = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; /** // <summary> // DES encrypted string /// </summary> /// <param name = "encryptString"> string to be encrypted </param> /// <param name = "encryptKey"> encryption key, the value must be 8-bit </param> /// <returns>. The encrypted string is returned successfully, and the source string </returns> public static string EncryptDES (string encryptString, string encryptKey) {try {byte [] rgbKey = Encoding. UTF8.GetBytes (encryptKey. substring (0, 8); byte [] rgbIV = Keys; byte [] inputByteArray = Encoding. UTF8.GetBytes (encryptString); DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider (); MemoryStream mStream = new MemoryStream (); CryptoStream cStream = new CryptoStream (mStream, dCSP. createEncryptor (rgbKey, rgbIV), CryptoStreamMode. write); cStream. write (inputByteArray, 0, inputByteArray. length); cStream. flushFinalBlock (); return Convert. toBase64String (mStream. toArray ();} catch {return encryptString ;}} /** // <summary> // decode the string /// </summary> /// <param name = "decryptString"> string to be decrypted </param> /// <param name = "decryptKey"> decrypt the key, the value must be 8 bits. It must be the same as the encryption key. </param> /// <returns> the decrypted string is returned, returned source string failed </returns> public static string DecryptDES (string decryptString, string decryptKey) {try {byte [] rgbKey = Encoding. UTF8.GetBytes (decryptKey); byte [] rgbIV = Keys; byte [] inputByteArray = Convert. fromBase64String (decryptString); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider (); MemoryStream mStream = new MemoryStream (); CryptoStream cStream = new CryptoStream (mStream, DCSP. createDecryptor (rgbKey, rgbIV), CryptoStreamMode. write); cStream. write (inputByteArray, 0, inputByteArray. length); cStream. flushFinalBlock (); return Encoding. UTF8.GetString (mStream. toArray () ;}catch {return decryptString ;}}}}

 

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.