Chapter 2: Base64, URLBase64, and base64urlbase64

Source: Internet
Author: User

Chapter 2: Base64, URLBase64, and base64urlbase64

2.1 Basic algorithm rules:

  • Public encryption algorithms
  • The encrypted key is not public.

The features of Base64 algorithm disclosure and Key Disclosure do not comply with basic algorithm rules, so they are easily cracked. Therefore, they are generally not used for enterprise-level encryption operations.

Note: Specific algorithms and keys (for Base64, It is a character ing table) can be found in Java encryption and decryption art (version 2nd) chapter 4 "Email transmission algorithm-Base64"

 

2.2 application scenarios

  • Simple encryption (in the case of low encryption requirements, that is, it can be used at a Glance)
  • Encryption (transforming the character ing table, that is, the private key)
  • Email transmission (The Mail content is encrypted using Base64, which is mainly used to solve Chinese problems)
  • Network Data Transmission (related to URLBase64, as described below)
  • Key storage (the key is a piece of binary data. The key is transmitted by both parties. Party A uses Base64 encoding to write the key into the document and transmits it to Party B through security channels (such as offline, base64 is used to store keys to enhance the ease of authorization of keys)
  • Digital Certificate Storage
  • Use Base64 to generate a digital certificate

Generally, the most commonly used Base64 is the storage key.

 

2.3. Base64 implementation

There are three implementation methods:

  • Commons Codec ("CC" for short ")
  • Bouncy Castle ("BC" for short ")
  • JDK (not recommended, as JDK is not implemented)

Commons Codec is recommended.

Note: The cc and bc versions are also called below JDK 1.6.

2.3.1 CC-based Base64 encryption and decryption

Step 1: Add jar

Step 2: write code

 

1 package com. util. base64; 2 3 import java. io. unsupportedEncodingException; 4 import org. apache. commons. codec. binary. base64; 5 6/** 7 * Base64 encryption based on Commons Codec 8 */9 public class Base64CoderCC {10 private static final String ENCODING = "UTF-8 "; 11 12/** 13 * General Base64 encryption 14 */15 public static String encode (String data) throws UnsupportedEncodingException {16 byte [] encodedByte = Base64.encodeBase64 (d Ata. getBytes (ENCODING); 17 return new String (encodedByte, ENCODING); 18} 19 20/** 21 * Security Base64 encryption 22 */23 public static String encodeSafe (String data) throws UnsupportedEncodingException {24/* 25 * Note: When encodeBase64 (byte [] bytes, boolean arg1) 26 * arg1 is true, the encrypted string contains 76 characters, "\ r \ n" 27 */28 byte [] encodedByte = Base64.encodeBase64 (data. getBytes (ENCODING), true); 29 return new String (EncodedByte, ENCODING); 30} 31 32/** 33 * Base64 decryption 34 */35 public static String decode (String data) throws UnsupportedEncodingException {36 byte [] decodedByte = Base64.decodeBase64 (data. getBytes (ENCODING); 37 return new String (decodedByte, ENCODING ); 38} 39 40/** 41 * test 42 * @ param args43 * @ throws UnsupportedEncodingException 44 */45 public static void main (String [] args) throws UnsupportedEncodingE Xception {46 ************* * ******/47 String data = "It is my dream to find a good girl to be a wife! "; 48 System. out. println ("original -->" + data); 49 String encodedStr = Base64CoderCC. encode (data); 50 System. out. println ("encrypted -->" + encodedStr); 51 String decodedStr = Base64CoderCC. decode (encodedStr); 52 System. out. println ("decrypted -->" + decodedStr); 53 System. out. println (data. equals (decodedStr); 54 System. out. println ("================================ "); 55/******************* test the security encode *************** * *****/56 String Data2 = "it's my dream to find a good girl to be a wife! It is my dream to find a good girl to be a wife! "; 57 System. out. println ("original -->" + data2); 58 String encodedStr2 = Base64CoderCC. encodeSafe (data2); 59 System. out. println ("encrypted -->" + encodedStr2); 60 String decodedStr2 = Base64CoderCC. decode (encodedStr2); 61 System. out. println ("decrypted -->" + decodedStr2); 62 System. out. println (data2.equals (decodedStr2); 63} 64}View Code

 

Step 3: Test

The main () method of the above Code.

Test results:

Note: You can use debug breakpoint debugging to view the value of each variable to verify the above situation.

 2.3.2. Base64 encryption and decryption algorithm based on BC

For BC, only the general Base64 encryption algorithm is supported. The jar package and the brief Code are as follows:

1 package com. util. base64; 2 3 import java. io. unsupportedEncodingException; 4 5 import org. bouncycastle. util. encoders. base64; 6 7/** 8 * Base64 encryption based on Bouncy Castle 9 */10 public class Base64CoderBC {11 private static final String ENCODING = "UTF-8 "; 12 13/** 14 * Base64 encryption 15 */16 public static String encode (String data) throws UnsupportedEncodingException {17 byte [] encodedByte = Base64.encode (data. get Bytes (ENCODING); 18 return new String (encodedByte, ENCODING); 19} 20 21/** 22 * Base64 decryption 23 */24 public static String decode (String data) throws UnsupportedEncodingException {25 byte [] decodedByte = Base64.decode (data. getBytes (ENCODING); 26 return new String (decodedByte, ENCODING ); 27} 28 29/** 30 * Test 31 * @ param args32 * @ throws UnsupportedEncodingException 33 */34 public static void main (String [] ar Gs) throws UnsupportedEncodingException {35 String data = "It is my dream to find a good girl as a wife! "; 36 System. out. println ("original -->" + data); 37 String encodedStr = Base64CoderBC. encode (data); 38 System. out. println ("encrypted -->" + encodedStr); 39 String decodedStr = Base64CoderBC. decode (encodedStr); 40 System. out. println ("decrypted -->" + decodedStr); 41 System. out. println (data. equals (decodedStr); 42} 43}View Code

2.4 URLBase64 implementation

Generally, this algorithm is only used to transmit private data in get mode and binary data in get mode.

  • Commons codec (CC, recommended. Because tail completion is not used, the Data Length is short, which reduces the network transmission time and the end supplement is Base64)
  • Bouncy castle (BC)

2.4.1 implement URLBase64 Based on CC

1 package com. util. base64; 2 3 import java. io. unsupportedEncodingException; 4 import org. apache. commons. codec. binary. base64; 5/** 6 * Based on Commons Codec URLBase64 encryption 7 */8 public class URLBase64CoderCC {9 private static final String ENCODING = "UTF-8 "; 10/** 11 * URLBase64 encryption 12 */13 public static String encode (String data) throws UnsupportedEncodingException {14 byte [] encodedByte = Base64.encodeBase64 URLSafe (data. getBytes (ENCODING); 15 return new String (encodedByte, ENCODING); 16} 17/** 18 * URLBase64 decrypt 19 */20 public static String decode (String data) throws UnsupportedEncodingException {21 byte [] decodedByte = Base64.decodeBase64 (data. getBytes (ENCODING); 22 return new String (decodedByte, ENCODING ); 23} 24 25/** 26 * Test 27 * @ param args28 * @ throws UnsupportedEncodingException 29 */30 public static Void main (String [] args) throws UnsupportedEncodingException {31 String data = "It is my dream to find a good girl as a wife! "; 32 System. out. println ("original -->" + data); 33 String encodedStr = URLBase64CoderCC. encode (data); 34 System. out. println ("encrypted -->" + encodedStr); 35 String decodedStr = URLBase64CoderCC. decode (encodedStr); 36 System. out. println ("decrypted -->" + decodedStr); 37 System. out. println (data. equals (decodedStr); 38} 39}View Code

View the test results by yourself.

2.4.2 implement URLBase64 Based on BC

1 package com. util. base64; 2 3 import java. io. unsupportedEncodingException; 4 import org. bouncycastle. util. encoders. urlBase64; 5/** 6 * BC-based URLBase64 encryption 7 */8 public class URLBase64CoderBC {9 private static final String ENCODING = "UTF-8 "; 10/** 11 * URLBase64 encryption 12 */13 public static String encode (String data) throws UnsupportedEncodingException {14 byte [] encodedByte = UrlBase64.encode (data. getByt Es (ENCODING); 15 return new String (encodedByte, ENCODING); 16} 17/** 18 * URLBase64 decrypt 19 */20 public static String decode (String data) throws UnsupportedEncodingException {21 byte [] decodedByte = UrlBase64.decode (data. getBytes (ENCODING); 22 return new String (decodedByte, ENCODING ); 23} 24 25/** 26 * Test 27 * @ param args28 * @ throws UnsupportedEncodingException 29 */30 public static void main (String [] ar Gs) throws UnsupportedEncodingException {31 String data = "It is my dream to find a good girl as a wife! "; 32 System. out. println ("original -->" + data); 33 String encodedStr = URLBase64CoderBC. encode (data); 34 System. out. println ("encrypted -->" + encodedStr); 35 String decodedStr = URLBase64CoderBC. decode (encodedStr); 36 System. out. println ("decrypted -->" + decodedStr); 37 System. out. println (data. equals (decodedStr); 38} 39}View Code

Test results: Check whether the encrypted string is different from the encrypted string after CC encryption.

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.