C # base64 Encryption Class. It can be used to change post to get to pass data.
Source: Internet
Author: User
Implement algorithms by yourselfMethod 1:///<summary>/// Base64 Encryption///</summary>///<paramname="Message"></param>///<returns></returns>publicstringBase64Code(stringMessage){char[]Base64Code=newchar[]{‘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‘,‘+‘,‘/‘,‘=‘};byteempty=(byte)0;System.Collections.ArrayListbyteMessage=newSystem.Collections.ArrayList(System.Text.Encoding.Default.GetBytes(Message));System.Text.StringBuilderoutmessage;intmessageLen=byteMessage.Count;// Divide the character into three bytes. If the character is not enough, the character is filled with 0.intpage=messageLen/3;intuse=0;if((use=messageLen%3)>0){for(inti=0;i<3-use;i++)byteMessage.Add(empty);page++;}// Convert each character group of three bytes into a group of four bytes. Three in one group. One group is changed to four bytes in one group.// The method is to convert the data into ASCII codes, arrange the 24-bit data in order, and then divide the 24-bit data into 4 groups, that is, 6-bit data in each group. Add two zeros before the highest bits in each group to make up one byte.outmessage=newSystem.Text.StringBuilder(page*4);for(inti=0;i<page;i++){// Obtains a group of three bytes.byte[]instr=newbyte[3];instr[0]=(byte)byteMessage[i*3];instr[1]=(byte)byteMessage[i*3+1];instr[2]=(byte)byteMessage[i*3+2];// Six digits are in one group, and 0 is supplemented to 4 bytes.int[]outstr=newint[4];// The first output byte: Take the first six digits of the first input byte, and add 0 at the top to change it to eight digits (one byte)outstr[0]=instr[0]>>2;// Second output byte: Take the first two digits of the first input byte and the first four digits of the second input byte (6 digits in total), and add 0 at the high position, change it to 8 bits (one byte)outstr[1]=((instr[0]&0x03)<<4)^(instr[1]>>4);// The third output byte: Take the last 4 bits of the second input byte and the first 2 bits of the third input byte (6 bits in total), and add 0 at the high position, change it to 8 bits (one byte)if(!instr[1].Equals(empty))outstr[2]=((instr[1]&0x0f)<<2)^(instr[2]>>6);elseoutstr[2]=64;// The fourth output byte: Take the last 6 digits of the third input byte, and add 0 at the top to change it to 8 bits (one byte)if(!instr[2].Equals(empty))outstr[3]=(instr[2]&0x3f);elseoutstr[3]=64;outmessage.Append(Base64Code[outstr[0]]);outmessage.Append(Base64Code[outstr[1]]);outmessage.Append(Base64Code[outstr[2]]);outmessage.Append(Base64Code[outstr[3]]);}returnoutmessage.ToString();}///<summary>/// Base64 decryption///</summary>///<paramname="Message"></param>///<returns></returns>publicstringBase64Decode(stringMessage){if((Message.Length%4)!=0){thrownewArgumentException("Incorrect base64 encoding. Please check. ","Message");}if(!System.Text.RegularExpressions.Regex.IsMatch(Message,"^[A-Z0-9/+=]*$",System.Text.RegularExpressions.RegexOptions.IgnoreCase)){thrownewArgumentException("Contains incorrect base64 encoding. Please check. ","Message");}stringBase64Code="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";intpage=Message.Length/4;System.Collections.ArrayListoutMessage=newSystem.Collections.ArrayList(page*3);char[]message=Message.ToCharArray();for(inti=0;i<page;i++){byte[]instr=newbyte[4];instr[0]=(byte)Base64Code.IndexOf(message[i*4]);instr[1]=(byte)Base64Code.IndexOf(message[i*4+1]);instr[2]=(byte)Base64Code.IndexOf(message[i*4+2]);instr[3]=(byte)Base64Code.IndexOf(message[i*4+3]);byte[]outstr=newbyte[3];outstr[0]=(byte)((instr[0]<<2)^((instr[1]&0x30)>>4));if(instr[2]!=64){outstr[1]=(byte)((instr[1]<<4)^((instr[2]&0x3c)>>2));}else{outstr[2]=0;}if(instr[3]!=64){outstr[2]=(byte)((instr[2]<<6)^instr[3]);}else{outstr[2]=0;}outMessage.Add(outstr[0]);if(outstr[1]!=0)outMessage.Add(outstr[1]);if(outstr[2]!=0)outMessage.Add(outstr[2]);}byte[]outbyte=(byte[])outMessage.ToArray(Type.GetType("System.Byte"));returnSystem.Text.Encoding.Default.GetString(outbyte);}Directly use library functions in. netMethod 2:///<summary>/// Base64 Encryption///</summary>///<paramname="Message"></param>///<returns></returns>publicstringBase64Code(stringMessage){byte[]bytes=Encoding.Default.GetBytes(Message);returnConvert.ToBase64String(bytes);}///<summary>/// Base64 decryption///</summary>///<paramname="Message"></param>///<returns></returns>publicstringBase64Decode(stringMessage){byte[]bytes=Convert.FromBase64String(Message);returnEncoding.Default.GetString(bytes);}Reference (1)
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.