C #. Net encryption and decryption: AEs/DES/base64/RSA/MD5/sha256

Source: Internet
Author: User
Using system; using system. globalization; using system. io; using system. security. cryptography; using system. text; namespace pub. class {public static class encryptextensions {Private Static readonly byte [] aeskeys = {0x41, 0x72, 0x65, 0x79, 0x6f, 0x75, 0x6d, 0x79, 0x53, 0x6e, 0x6f, 0x77, 0x6d, 0x61, 0x6e, 0x3f}; Private Static readonly byte [] deskeys = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef}; P Ublic static string aesencode (this string encryptstring, string encryptkey) {encryptkey = encryptkey. substring (32, ""); encryptkey = encryptkey. padright (32, ''); var rijndaelprovider = new rijndaelmanaged (); rijndaelprovider. key = encoding. utf8.getbytes (encryptkey. substring (0, 32); rijndaelprovider. IV = aeskeys; icryptotransform rijndaelencrypt = rijndaelprovider. createencryptor (); byte [] Input Data = 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, ''); var 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.getstring (decrypteddata);} catch {return string. empty ;}} Pu BLIC 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 [] inputbytearray = encoding. utf8.getbytes (encryptstring); var dcsp = new descryptoserviceprovider (); var mstream = new memorystream (); var CST Ream = new cryptostream (mstream, dcsp. createencryptor (rgbkey, rgbiv), cryptostreammode. write); cstream. write (inputbytearray, 0, inputbytearray. length); cstream. flushfinalblock (); Return convert. tobase64string (mstream. toarray ();} 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); var dcsp = new descryptoserviceprovider (); var mstream = new memorystream (); var 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 = encoding. utf8.getbytes (encryptstring); Return convert. tobase64string (encbuff);} public static string base64decode (this string decryptstring) {byte [] decbuff = convert. frombase64string (decryptstring); Return encoding. utf8.getstri Ng (decbuff);} public static string rsadecrypt (this string S, string key) {string result = NULL; If (string. isnullorempty (s) throw new argumentexception ("an empty string value cannot be encrypted. "); If (string. isnullorempty (key) throw new argumentexception ("cannot decrypt using an empty key. please supply a decryption key. "); var cspp = new cspparameters (); cspp. keycontainername = key; var RSA = New rsacryptoserviceprovider (cspp); RSA. persistkeyincsp = true; string [] decryptarray = S. split (new [] {"-"}, stringsplitoptions. none); byte [] decryptbytearray = array. convertall (decryptarray, (a => convert. tobyte (byte. parse (A, numberstyles. hexnumber); byte [] bytes = RSA. decrypt (decryptbytearray, true); Result = encoding. 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. "); var cspp = new cspparameters (); cspp. keycontainername = key; var RSA = new rsacryptoserviceprovider (cspp); RSA. persistkeyincsp = true; B Yte [] bytes = RSA. encrypt (encoding. utf8.getbytes (s), true); Return bitconverter. tostring (bytes);} public static string MD5 (this string Str) {string cl1 = STR; string Pwd = ""; 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 = PWD + 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; var M5 = new md5cryptoserviceprovider (); // create the MD5 object byte [] inputbye; byte [] outputbye; // convert the string to a byte array using gb2312 encoding. inputbye = encoding. getencoding ("gb2312 "). getbytes (enpolicstr); outputbye = m5.computehash (inputbye); retstr = bitconverter. tostring (outputbye); retstr = retstr. replace ("-",""). tolower (); Return retstr;} public static string sha256 (this string Str) {byte [] sha256data = encoding. utf8.getbytes (STR); var sha256 = new sha256managed (); byte [] result = sha256.computehash (sha256data); Return convert. tobase64string (result); // returns a 44-byte string }}}

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.