Base64 encryption and decryption and Base64URL

A, Base64 codec:

Base64 is a character represented by 64 arbitrary binary data.

When this open exe, jpg, pdf file with Notepad these, we will see a lot of gibberish, because the binary file that contains a lot of characters can not be displayed and printed, so if you want such as Notepad text processing software can handle binary data conversion method, you need to a binary string, then Base64 is the most common method of binary encoding.

Base64 principle is very simple, first of all, to prepare an array of 64 characters include:

['A', 'B', 'C', ... 'a', 'b', 'c', ... '0', '1', ... '+', '/']

Then, binary data is processed, a group of 3 bytes each, a total of 3x8 = 24bit. 24bit then reclassified 4 groups of exactly six bit.

And we get four digits as an index, and look-up table, to obtain the corresponding four characters, the string is encoded. Therefore, Base64 encoded will encode binary data 3 bytes 4 bytes of text data, 33% increase in length, the benefits of the encoded text data can be displayed directly in the message body, web pages.

If you want to encode binary data is not a multiple of three, will be the last remaining one or two bytes how to do? Base64 with \ x00 byte after bringing the end and then at the end of encoding plus one or two = number indicates how many bytes make up, when decoded, it will be automatically removed.

Python be built directly base64 base64 codec:

>>> import base64
>>> base64.b64encode('binary\x00string')
'YmluYXJ5AHN0cmluZw=='
>>> base64.b64decode('YmluYXJ5AHN0cmluZw==')
'binary\x00string'

It can occur following a standard Base64 encoded characters + and / in the URL can not be directly used as parameters, so harboring a "url safe" base64 encoded, in fact, the characters + and / respectively become - and _:

>>> base64.b64encode('i\xb7\x1d\xfb\xef\xff')
'abcd++//'
>>> base64.urlsafe_b64encode('i\xb7\x1d\xfb\xef\xff')
'abcd--__'
>>> base64.urlsafe_b64decode('abcd--__')
'i\xb7\x1d\xfb\xef\xff'

You can define your own in the order of 64 characters, so that you can customize Base64 encoding, however, usually not absolutely necessary.

Base64 is a coding method by the look-up table, the encryption can not be used, even if the custom code table too.

Base64 encoding is suitable for small pieces of content, such as digital signature certificate, Cookie content and so on.

Since = characters may appear in Base64 encoding, but = used in the URL, Cookie which can cause ambiguity, so, after a lot of Base64 encoding = will remove:

# 标准Base64:
'abcd' -> 'YWJjZA=='
# 自动去掉=:
'abcd' -> 'YWJjZA'

After removing = how to decode it? Base64 is because the three bytes to 4 bytes, the length of Base64 encoding is always a multiple of 4, therefore, the need to add = Base64 string length becomes a multiple of four, it can be properly decoded .

Exercise: Please write to handle a base64-decoded rid = function.

>>> base64.b64decode('YWJjZA==')
'abcd'
>>> base64.b64decode('YWJjZA')
Traceback (most recent call last):
  ...
TypeError: Incorrect padding
>>> safe_b64decode('YWJjZA')
'abcd'

summary:

Base64 is a binary arbitrary text string to the encoding method, commonly used for transferring small amounts of binary data in the URL, Cookie, page.

 

Two, Base64URL codec

A BASE64URL is the BASE64 encoded on the basis of a new form of encryption, coding for safe and smooth transfer, the need for BASE64 be processed in the network to do, especially the Internet.

  • BASE64URL encoding process:

      1, the plaintext is encrypted using BASE64

      2, the following BASE64 encoded on the basis of:

               1) removing the tail "="

               2) The "+" replaced by "-"

               3) The "/" replace "_"

  • BASE64URL decoding process:

      1, the BASE64URL decoding coded as follows:

               1) The "-" replaced by "+"

               2) The "_" replaced by "/"

               3) (code length calculated BASE64URL) 4%

                           a) the result is 0, no processing

                           b) 2 as a result, the character string adding "=="

                           c) 3 as a result, the character string adding "="

      2, using BASE64 decoding ciphertext the original plaintext obtained

Attached to achieve Base64URL in JAVA of:

public static String base64UrlEncode(byte[] simple) 
{
   String s = new String(Base64.encodeBase64(simple)); // Regular base64 encoder 
   s = s.split("=")[0]; // Remove any trailing '='s 
   s = s.replace('+', '-'); // 62nd char of encoding 
   s = s.replace('/', '_'); // 63rd char of encoding 
   return s;
} 

public static byte[] base64UrlDecode(String cipher) 
{ 
   String s = cipher; 
   s = s.replace('-', '+'); // 62nd char of encoding 
   s = s.replace('_', '/'); // 63rd char of encoding 
   switch (s.length() % 4) 
   { // Pad with trailing '='s 
      case 0: 
         break; // No pad chars in this case 
      case 2: 
         s += "=="; 
         break; // Two pad chars 
      case 3: 
         s += "="; 
         break; // One pad char 
      default: 
         System.err.println("Illegal base64url String!");
    } 
    return Base64.decodeBase64(s); // Standard base64 decoder
}

 

Based on the original others, organized into a document, I hope we learn together, and thank originality of the following two students and Liao Xuefeng.

https://www.liaoxuefeng.com/wiki/897692888725344/949441536192576

https://blog.csdn.net/weixin_42506905/article/details/82053888

https://blog.csdn.net/oceanyang520/article/details/52688959

Released five original articles · won praise 3 · Views 364

Guess you like

Origin blog.csdn.net/lotus_2015/article/details/104637938