C#base64 UTF8 string encryption and decryption

Source: Internet
Author: User

Base 64 decoding

New base64 (); string  orgstr= Encoding.Default.GetString (BB. Getdecoded ("Base64 compiled character "));

UTF8

Subject = encoding.getencoding ("utf-8"). GetString (convert.frombase64string ("UTF-compiled characters "

Base64 encryption

public class Base64 {char[] source;        int length, length2, length3;        int blockcount;        int paddingcount;        public static Base64 Decoder = new Base64 ();            Public base64 () {} private void init (char[] input) {int temp = 0;            Source = input; Length = input.            Length;            for (int x = 0; x < 2; + +) {if (input[length-x-1] = = ' = ') temp++;            } paddingcount = temp;            Blockcount = LENGTH/4;        length2 = Blockcount * 3; }///<summary>//encryption string decryption///</summary>//<param name= "Strinput" ><            /param>//<returns></returns> public byte[] getdecoded (string strinput) {            Initialize init (Strinput.tochararray ());            byte[] buffer = new Byte[length];         byte[] Buffer2 = new Byte[length2];   for (int x = 0; x < length; × x + +) {Buffer[x] = Char2sixbit (source[x]);            } byte B, B1, B2, B3;            Byte Temp1, Temp2, Temp3, Temp4;                for (int x = 0; x < Blockcount; × x + +) {Temp1 = buffer[x * 4];                TEMP2 = buffer[x * 4 + 1];                Temp3 = buffer[x * 4 + 2];                Temp4 = buffer[x * 4 + 3];                b = (byte) (Temp1 << 2);                B1 = (Byte) ((Temp2 &) >> 4);                B1 + = b;                b = (byte) ((Temp2 &) << 4);                b2 = (byte) ((Temp3 &) >> 2);                B2 + = b;                b = (byte) ((Temp3 & 3) << 6);                B3 = Temp4;                B3 + = b;                Buffer2[x * 3] = B1;                BUFFER2[X * 3 + 1] = B2;            BUFFER2[X * 3 + 2] = B3;            } length3 = Length2-paddingcount; Byte[] result = new Byte[leNGTH3];            for (int x = 0; x < length3; × x + +) {Result[x] = buffer2[x];        } return result; } Private byte Char2sixbit (char c) {char[] lookuptable = new char[64]{' A ', ' B '                 , ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' j ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' w ', ' x '            , ' y ', ' z ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' + ', '/';            if (c = = ' = ') return 0;  else {for (int x = 0; x < × x + +) {if (lookuptable[x] = =                c) return (byte) x;            } return 0; }}///<summary>//Base64 decryption, decrypted with UTF8 encoding///</summary>//<param NA Me="Result" > Ciphertext to Decrypt </param>//<returns> decrypted string </returns> public static string DecodeBase6        4 (string result) {return DecodeBase64 (Encoding.UTF8, result); }///<summary>//BASE64 decryption///</summary>/<param name= "codename" > Decryption using the Encoding method, note and encryption in the same way </param>///<param Name= "result" > ciphertext to be decrypted </param>//<returns> decrypted String </returns> public static string DecodeBase64 (Encoding encode, string result) {string de            Code = "";            byte[] bytes = convert.frombase64string (result); try {decode = encode.            GetString (bytes);            } catch {decode = result;        } return decode; }///<summary>//BASE64 encryption///</summary>/<param name= "codename" > Encryption using the Encoding </param>///&LT;p Aram Name= "source" > PlainText to be encrypted </param>//<returns></returns> public static string Encode            Base64 (Encoding encode, string source) {String decode = ""; byte[] bytes = encode.            GetBytes (source);            try {decode = convert.tobase64string (bytes);            } catch {decode = source;        } return decode; }///<summary>//BASE64 encryption, encrypted using UTF8 encoding///</summary>//<param name= "Sourc E "> The plaintext to be encrypted </param>//<returns> encrypted strings </returns> public static string EncodeBase64 (Stri        Ng source) {return EncodeBase64 (Encoding.UTF8, source); }    }

C#base64 UTF8 string encryption and decryption

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.