Base64Util

  1 package com.jf.utils;
  2 
  3 import java.util.HashMap;
  4 import java.util.Map;
  5 
  6 
  7 public class Base64Utils {
  8 
  9     private static final Map<Integer, Character> base64CharMap = new HashMap<Integer, Character>();
 10     private static final String base64CharString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 11     
 12     static {
 13         for (int i = 0; i < base64CharString.length(); i++) {
 14             char c = base64CharString.charAt(i);
 15             base64CharMap.put(new Integer(i), new Character(c));
 16         }
 17     }
 18     
 19     public static String encode(String origin) {
 20         if (origin == null) {
 21             return null;
 22         }
 23         if (origin.length() == 0) {
 24             return "";
 25         }
 26         int length = origin.length();
 27         String binaryString = "";
 28         for (int i = 0; i < length; i++) {
 29             int ascii = origin.charAt(i);
 30             String binaryCharString = Integer.toBinaryString(ascii);
 31             while (binaryCharString.length() < 8) {
 32                 binaryCharString = "0" + binaryCharString;
 33             }
 34             binaryString += binaryCharString;
 35         }
 36         int beginIndex = 0;
 37         int endIndex = beginIndex + 6;
 38         String base64BinaryString = "";
 39         String charString = "";
 40         while ((base64BinaryString = binaryString.substring(beginIndex, endIndex)).length() > 0) {
 41             // if length is less than 6, add "0".
 42             while (base64BinaryString.length() < 6) {
 43                     base64BinaryString += "0";
 44             }
 45             int index = Integer.parseInt(base64BinaryString, 2);
 46             char base64Char = base64CharMap.get(index);
 47             charString = charString + base64Char;
 48             beginIndex += 6;
 49             endIndex += 6;
 50             if (endIndex >= binaryString.length()) {
 51                 endIndex = binaryString.length();
 52             }
 53             if (endIndex < beginIndex) {
 54                 break;
 55             }
 56         }
 57         if (length % 3 == 2) {
 58             charString += "=";
 59         }
 60         if (length % 3 == 1) {
 61             charString += "==";
 62         }
 63         return charString;
 64     }
 65     
 66     /**
 67     * This method is used to decode from base64 string to a normal string.
 68     * 
 69     * @param encodedString
 70     *            The string to be decoded.
 71     * @return The string after decoded.
 72     */
 73     public static String decode(String encodedString) {
 74         if (encodedString == null) {
 75             return null;
 76         }
 77         if (encodedString.length() == 0) {
 78             return "";
 79         }
 80         // get origin base64 String
 81         String origin = encodedString.substring(0, encodedString.indexOf("="));
 82         String equals = encodedString.substring(encodedString.indexOf("="));
 83         String binaryString = "";
 84         // convert base64 string to binary string
 85         for (int i = 0; i < origin.length(); i++) {
 86             char c = origin.charAt(i);
 87             int ascii = base64CharString.indexOf(c);
 88             String binaryCharString = Integer.toBinaryString(ascii);
 89             while (binaryCharString.length() < 6) {
 90                 binaryCharString = "0" + binaryCharString;
 91             }
 92             binaryString += binaryCharString;
 93         }
 94         // the encoded string has 1 "=", means that the binary string has append
 95         // 2 "0"
 96         if (equals.length() == 1) {
 97             binaryString = binaryString.substring(0, binaryString.length() - 2);
 98         }
 99         // the encoded string has 2 "=", means that the binary string has append
100         // 4 "0"
101         if (equals.length() == 2) {
102             binaryString = binaryString.substring(0, binaryString.length() - 4);
103         }
104         // convert to String
105         String charString = "";
106         String resultString = "";
107         int beginIndex = 0;
108         int endIndex = beginIndex + 8;
109         while ((charString = binaryString.substring(beginIndex, endIndex)).length() == 8) {
110             int ascii = Integer.parseInt(charString, 2);
111             resultString += (char) ascii;
112             beginIndex += 8;
113             endIndex += 8;
114             if (endIndex > binaryString.length()) {
115                 break;
116             }
117         }
118         return resultString;
119     }
120     
121     public static void main(String[] args) {
122         String originString = "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.";
123         String base64String = "TWFxxxbxx"
125         + "dGhlIG1xxYSBwZXJzxxxY2xxx0aGUgY29udGlu"
126         + "dxxxxlIGdlbmVyYXRpb24gb2YxxxBleGNlZWRzIHRo"
127         + "xxx";
128         System.out.println("===============encode=================");
129         String result = encode(originString);
130         System.out.println(result);
131         System.out.println(result.equals(base64String));
132         
133         System.out.println("===============decode=================");
134         String decodedString = decode(base64String);
135         System.out.println(decodedString);
136         System.out.println(originString.equals(decodedString));
137         
138         System.out.println(decode(encode("abcdefg")));
139     }
140 }

 

posted @ 2018-09-16 22:30  面向bug编程  阅读(264)  评论(0编辑  收藏  举报