This article describes the Base64 encryption algorithm implemented by Java. Share it for your reference, as follows:
An algorithm implementation
1. JDK
2. Commonc Codec
3. Bouncy Castle
Two codes
package com.imooc.security.base64;import java.io.IOException;import org.apache.commons.codec.binary.Base64;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;public class ImoocBase64 {private static String src = "cakin24 security base64";public static void main(String[] args) {jdkBase64();commonsCodesBase64();buncyCastleBase64();}public static void jdkBase64() {try {BASE64Encoder encoder = new BASE64Encoder();String encode = encoder.encode(src.getBytes());System.out.println("encode : " + encode);BASE64Decoder decoder = new BASE64Decoder();System.out.println("decode : " + new String(decoder.decodeBuffer(encode)));} catch (IOException e) {e.printStackTrace();}}public static void commonsCodesBase64() {byte[] encodeBytes = Base64.encodeBase64(src.getBytes());System.out.println("encode : " + new String(encodeBytes));byte[] decodeBytes = Base64.decodeBase64(encodeBytes);System.out.println("decode : " + new String(decodeBytes));}public static void bouncyCastleBase64() {byte[] encodeBytes = org.bouncycastle.util.encoders.Base64.encode(src.getBytes());System.out.println("encode : " + new String(encodeBytes));byte[] decodeBytes = org.bouncycastle.util.encoders.Base64.decode(encodeBytes);System.out.println("decode : " + new String(decodeBytes));}}Three run effects
encode : Y2FraW4yNCBzZWN1cmml0eSBiYXNlNjQ=
decode: cakin24 security base64
encode : Y2FraW4yNCBzZWN1cmml0eSBiYXNlNjQ=
decode: cakin24 security base64
encode : Y2FraW4yNCBzZWN1cmml0eSBiYXNlNjQ=
decode: cakin24 security base64
Four application scenarios
Email, key, certificate file
Five causes
Historical issues of emails
Six Others
Encoding algorithm based on 64 characters, defined in RFC 2045
Supplement: sun.misc.BASE64Encoder and sun.misc.BASE64Decoder are used here, which can be used in Eclipse through the following settings:
Right-click the project --> Properties --> Java Build Path --> Click on JRE System Library --> Click on Access rules --> Edit --> Add --> Resolution Select Accessible --> Rule Pattern Fill in ** --> OK
The settings of the Chinese version of Eclipse are shown in the figure below:
PS: Here are a few more encryption and decryption-related online tools for your reference:
Line encoding conversion tool (utf-8/utf-32/Punycode/Base64):
http://tools.VeVB.COM/transcoding/decode_encode_tool
BASE64 encoding and decoding tools:
http://tools.VeVB.COM/transcoding/base64
Image conversion to Base64 encoding online tool:
http://tools.VeVB.COM/transcoding/img2base64
Online MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160 encryption tool:
http://tools.VeVB.COM/password/hash_md5_sha
For more information about Java related content, please check out the topics of this site: "Summary of Java Mathematical Operation Skills", "Tutorial on Java Data Structures and Algorithms", "Summary of Java Characters and String Operation Skills", "Summary of Java Operation DOM Node Skills" and "Summary of Java Cache Operation Skills"
I hope this article will be helpful to everyone's Java programming.