C # implement base64 encryption and decryption algorithms

Source: Internet
Author: User

 

/// <Summary>

/// Base64 Encryption

/// </Summary>

/// <Param name = "message"> </param>

/// <Returns> </returns>

Public String base64code (string message)

{

Char [] base64code = new char [] {'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', '+', '/', '= '};

Byte empty = (byte) 0;

System. Collections. arraylist bytemessage = new system. Collections. arraylist (system. Text. encoding. Default. getbytes (Message ));

System. Text. stringbuilder outmessage;

Int messagelen = bytemessage. count;

// Divide the character into three bytes. If the character is not enough, the character is filled with 0.

Int page = messagelen/3;

Int use = 0;

If (use = messagelen % 3)> 0)

{

For (INT I = 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 = new system. Text. stringbuilder (page * 4 );

For (INT I = 0; I <page; I ++)

{

// Obtains a group of three bytes.

Byte [] instr = new byte [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 = new int [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 );

Else

Outstr [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 );

Else

Outstr [3] = 64;

Outmessage. append (base64code [outstr [0]);

Outmessage. append (base64code [outstr [1]);

Outmessage. append (base64code [outstr [2]);

Outmessage. append (base64code [outstr [3]);

}

Return outmessage. tostring ();

}

/// <Summary>

/// Base64 decryption

/// </Summary>

/// <Param name = "message"> </param>

/// <Returns> </returns>

Public String base64decode (string message)

{

If (message. Length % 4 )! = 0)

{

Throw new argumentexception ("Incorrect base64 encoding. Please check. "," Message ");

}

If (! System. Text. regularexpressions. RegEx. ismatch (message, "^ [A-Z0-9/+ =] * $", system. Text. regularexpressions. regexoptions. ignorecase ))

{

Throw new argumentexception ("contains incorrect base64 encoding. Please check. "," Message ");

}

String base64code = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 +/= ";

Int page = message. Length/4;

System. Collections. arraylist outmessage = new system. Collections. arraylist (page * 3 );

Char [] Message = message. tochararray ();

For (INT I = 0; I <page; I ++)

{

Byte [] instr = new byte [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 = new byte [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 "));

Return System. Text. encoding. Default. getstring (outbyte );

}

Method 2: directly use the library-class functions in. net

/// <Summary>

/// Base64 Encryption

/// </Summary>

/// <Param name = "message"> </param>

/// <Returns> </returns>

Public String base64code (string message)

{

Byte [] bytes = encoding. Default. getbytes (Message );

Return convert. tobase64string (bytes );

}

/// <Summary>

/// Base64 decryption

/// </Summary>

/// <Param name = "message"> </param>

/// <Returns> </returns>

Public String base64decode (string message)

{

Byte [] bytes = convert. frombase64string (Message );

Return encoding. Default. getstring (bytes );

}

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.