Advertisement
jsprieto10

Java Base64 coder and decoder

Mar 5th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.48 KB | None | 0 0
  1. public class Base64 {
  2.     private static final String tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  3.    
  4.     public Base64()
  5.     {
  6.        
  7.     }
  8.     public static String encode(String string) {
  9.         String key = new String(encode(string, false));
  10.         return key;
  11.     }
  12.    
  13.     public static byte[] encode(byte[] data) {
  14.         return encode(data, false);
  15.     }
  16.    
  17.     public static byte[] encode(String string, boolean doSplit) {
  18.         //try to use the appropriate encoding string, but fallback to default locale
  19.         try { return encode(string.getBytes("UTF-8"), doSplit);
  20.         } catch (Exception ex) { }
  21.         return encode(string.getBytes(), doSplit);
  22.     }
  23.    
  24.     /**
  25.      * Base64 encode a byte array
  26.      * @param data The data to encode
  27.      * @param doSplit should we split the data into 76-character lines?
  28.      * @return base64-encoded byte array
  29.      * @author [John Comeau, Fred Sanchez, Cipher_nemo, Teresa and 9 others]<br/>
  30.      * Modified from <a href="http://www.wikihow.com/Encode-a-String-to-Base64-With-Java">WikiHow</a>
  31.      */
  32.     public static byte[] encode(byte[] data, boolean doSplit) {
  33.         String encoded = "", split = "";
  34.         //determine how many padding bytes to add to the end
  35.         int paddingCount = (3 - (data.length % 3)) % 3;
  36.         //add any necessary padding to the input
  37.         byte[] padded = new byte[data.length + paddingCount];
  38.         System.arraycopy(data, 0, padded, 0, data.length);
  39.         data = padded;
  40.         //process 3 bytes at a time, output 4 bytes at a time
  41.         for (int i = 0; i < data.length; i += 3) {
  42.             int j = ((data[i] & 0xff) << 16) + ((data[i + 1] & 0xff) << 8) + (data[i + 2] & 0xff);
  43.             encoded = encoded + tbl.charAt((j >> 18) & 0x3f) + tbl.charAt((j >> 12) & 0x3f) + tbl.charAt((j >> 6) & 0x3f) + tbl.charAt(j & 0x3f);
  44.         }
  45.         //replace encoded padding nulls with "="
  46.         encoded = encoded.substring(0, encoded.length() - paddingCount) + "==".substring(0, paddingCount);
  47.         //split into multiple lines
  48.         if (doSplit) {
  49.             for (int i = 0; i < encoded.length(); i += 76) {
  50.                 split += encoded.substring(i, Math.min(encoded.length(), i + 76)) + "\r\n";
  51.             }
  52.         }
  53.         //return the result, cleaning it up with a trim() if needed
  54.         return (doSplit ? split.trim() : encoded).getBytes();
  55.     }
  56.    
  57.     /**
  58.      * @param data The data to decode
  59.      * @return base64-decoded byte array
  60.      * @author [Unknown author]<br/>
  61.      * Modified from <a href="http://en.wikibooks.org/wiki/Algorithm_Implementation/Miscellaneous/Base64#Java_2">WikiBooks</a>
  62.      */
  63.     public static byte[] decode(byte[] data) {
  64.         String decoded = "";
  65.         //replace any incoming padding with a zero pad (the 'A' character is zero)
  66.         String pad = (data[data.length - 1] == '=' ? (data[data.length - 2] == '=' ? "AA" : "A") : "");
  67.         data = (new String(data).substring(0, data.length - pad.length()) + pad).getBytes();
  68.         //increment over the length of this encrypted string, four characters
  69.         //at a time
  70.         for (int i = 0; i < data.length; i += 4) {
  71.             //each of these four characters represents a 6-bit index in the base64 characters list
  72.             //which, when concatenated, will give the 24-bit number for the original 3 characters
  73.             int j = (tbl.indexOf(data[i]) << 18) + (tbl.indexOf(data[i+1]) << 12) + (tbl.indexOf(data[i+2]) << 6) + tbl.indexOf(data[i+3]);
  74.             //split the 24-bit number into the original three 8-bit (ASCII) characters
  75.             decoded += "" + (char) ((j >>> 16) & 0xFF) + (char) ((j >>> 8) & 0xFF) + (char) (j & 0xFF);
  76.         }
  77.         //remove any zero pad that was added to make this a multiple of 24 bits
  78.         return (decoded.substring(0, decoded.length() - pad.length())).getBytes();
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement