C #. NET encryption and decryption: AES/DES/Base64/RSA/MD5/SHA256.

Source: Internet
Author: User
Using System; using System. collections. generic; using System. linq; using System. text; using System. reflection; using System. data; using System. data. common; using System. web. script. serialization; using System. IO; using System. security. cryptography; namespace Pub. class {public static class EncryptExtensions {private static byte [] AESKeys = {0x41, 0x72, 0x65, 0x79, 0x6F, 0x75, 0x6D, 0x79, 0x53, 0x6E, 0x6F, 0x 77, 0x6D, 0x61, 0x6E, 0x3F}; public static string AESEncode (this string encryptString, string encryptKey) {encryptKey = encryptKey. subString (32, ""); encryptKey = encryptKey. padRight (32, ''); RijndaelManaged rijndaelProvider = new RijndaelManaged (); rijndaelProvider. key = Encoding. UTF8.GetBytes (encryptKey. substring (0, 32); rijndaelProvider. IV = AESKeys; ICryptoTransform rijndaelEncrypt = rijnd AelProvider. createEncryptor (); byte [] inputData = Encoding. UTF8.GetBytes (encryptString); byte [] encryptedData = rijndaelEncrypt. transformFinalBlock (inputData, 0, inputData. length); return Convert. toBase64String (encryptedData);} public static string AESDecode (this string decryptString, string decryptKey) {try {decryptKey = decryptKey. subString (32, ""); decryptKey = decryptKey. padRight (32 ,''); RijndaelManaged rijndaelProvider = new RijndaelManaged (); rijndaelProvider. key = Encoding. UTF8.GetBytes (decryptKey); rijndaelProvider. IV = AESKeys; ICryptoTransform rijndaelDecrypt = rijndaelProvider. createDecryptor (); byte [] inputData = Convert. fromBase64String (decryptString); byte [] decryptedData = rijndaelDecrypt. transformFinalBlock (inputData, 0, inputData. length); return Encoding. UTF8.GetStrin G (decryptedData);} catch {return string. empty ;}} private static byte [] DESKeys = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; public static string DESEncode (this string encryptString, string encryptKey) {encryptKey = encryptKey. subString (8, ""); encryptKey = encryptKey. padRight (8, ''); byte [] rgbKey = Encoding. UTF8.GetBytes (encryptKey. substring (0, 8); byte [] rgbIV = DESKeys; byte [] I NputByteArray = 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. toArr Ay ();} public static string DESDecode (this string decryptString, string decryptKey) {try {decryptKey = decryptKey. subString (8, ""); decryptKey = decryptKey. padRight (8, ''); byte [] rgbKey = Encoding. UTF8.GetBytes (decryptKey); byte [] rgbIV = policeys; byte [] inputByteArray = Convert. fromBase64String (decryptString); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider (); MemoryStream mStr Eam = 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 string. empty ;}} public static string Base64Encode (this string encryptString) {byte [] encbuff = System. text. E Ncoding. UTF8.GetBytes (encryptString); return Convert. toBase64String (encbuff);} public static string Base64Decode (this string decryptString) {byte [] decbuff = Convert. fromBase64String (decryptString); return System. text. encoding. UTF8.GetString (decbuff);} public static string RSADecrypt (this string s, string key) {string result = null; if (string. isNullOrEmpty (s) throw new ArgumentException (" Empty string value cannot be encrypted. "); if (string. isNullOrEmpty (key) throw new ArgumentException ("Cannot decrypt using an empty key. please supply a decryption key. "); CspParameters cspp = new CspParameters (); cspp. keyContainerName = key; RSACryptoServiceProvider rsa = new RSACryptoServiceProvider (cspp); rsa. persistKeyInCsp = true; string [] decryptArray = s. split (new string [] {"-"}, StringS PlitOptions. none); byte [] decryptByteArray = Array. convertAll <string, byte> (decryptArray, (a => Convert. toByte (byte. parse (a, System. globalization. numberStyles. hexNumber); byte [] bytes = rsa. decrypt (decryptByteArray, true); result = System. text. UTF8Encoding. UTF8.GetString (bytes); return result;} public static string RSAEncrypt (this string s, string key) {if (string. isNullOrEmpty (s) throw new ArgumentException ("An empty string value cannot be encrypted. "); if (string. isNullOrEmpty (key) throw new ArgumentException ("Cannot encrypt using an empty key. please supply an encryption key. "); CspParameters cspp = new CspParameters (); cspp. keyContainerName = key; RSACryptoServiceProvider rsa = new RSACryptoServiceProvider (cspp); rsa. persistKeyInCsp = true; byte [] bytes = rsa. encrypt (System. text. UTF8Encoding. UTF8.GetBytes (s), true); return BitConverter. toString (bytes);} public static string MD5 (this string str) {string cl1 = str; string pwd = ""; System. security. cryptography. MD5 md5 = System. security. cryptography. MD5.Create (); // The encrypted byte array byte [] s = md5.ComputeHash (Encoding. unicode. getBytes (cl1); for (int I = 0; I <s. length; I ++) {// converts an array of the byte type to a string by using a loop. This string is a regular string formatted pwd = p Wd + s [I]. ToString ("x"); // use the hexadecimal format of the obtained string. The characters in the format are lowercase letters. If uppercase letters (X) are used, the characters in the format are uppercase letters} return pwd;} public static string MD5CSP (this string enpolicstr) {string retStr; MD5CryptoServiceProvider m5 = new MD5CryptoServiceProvider (); // create an md5 object byte [] inputBye; byte [] outputBye; // convert the string into a byte array using GB2312 encoding. inputBye = Encoding. getEncoding ("GB2312 "). getBytes (enpolicstr); outputBye = m5.ComputeHash (inputBye); retStr = System. bitConverter. toString (outputBye); retStr = retStr. replace ("-",""). toLower (); return retStr;} public static string SHA256 (this string str) {byte [] SHA256Data = Encoding. UTF8.GetBytes (str); SHA256Managed Sha256 = new SHA256Managed (); byte [] Result = Sha256.ComputeHash (SHA256Data); return Convert. toBase64String (Result); // returns a 44-byte string }}}

 

Related Article

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.