1 package com.protomatter.util; 2 3 52 53 57 public class Base64 58 { 59 private Base64() 60 { 61 super(); 62 } 63 64 67 public final static String encode(byte[] d) 68 { 69 if (d == null) return null; 70 byte data[] = new byte[d.length+2]; 71 System.arraycopy(d, 0, data, 0, d.length); 72 byte dest[] = new byte[(data.length/3)*4]; 73 74 for (int sidx = 0, didx=0; sidx < d.length; sidx += 3, didx += 4) 76 { 77 dest[didx] = (byte) ((data[sidx] >>> 2) & 077); 78 dest[didx+1] = (byte) ((data[sidx+1] >>> 4) & 017 | 79 (data[sidx] << 4) & 077); 80 dest[didx+2] = (byte) ((data[sidx+2] >>> 6) & 003 | 81 (data[sidx+1] << 2) & 077); 82 dest[didx+3] = (byte) (data[sidx+2] & 077); 83 } 84 85 for (int idx = 0; idx <dest.length; idx++) 87 { 88 if (dest[idx] < 26) dest[idx] = (byte)(dest[idx] + 'A'); 89 else if (dest[idx] < 52) dest[idx] = (byte)(dest[idx] + 'a' - 26); 90 else if (dest[idx] < 62) dest[idx] = (byte)(dest[idx] + '0' - 52); 91 else if (dest[idx] < 63) dest[idx] = (byte)'+'; 92 else dest[idx] = (byte)'/'; 93 } 94 95 for (int idx = dest.length-1; idx > (d.length*4)/3; idx--) 97 { 98 dest[idx] = (byte)'='; 99 } 100 return new String (dest); 101 } 102 103 106 public final static byte[] decode(String str) 107 { 108 if (str == null) return null; 109 return decode(str.getBytes()); 110 } 111 112 116 public final static byte[] decode(byte[] data) 117 { 118 int tail = data.length; 119 while (data[tail-1] == '=') tail--; 120 byte dest[] = new byte[tail - data.length/4]; 121 122 for (int idx = 0; idx <data.length; idx++) 124 { 125 if (data[idx] == '=') data[idx] = 0; 126 else if (data[idx] == '/') data[idx] = 63; 127 else if (data[idx] == '+') data[idx] = 62; 128 else if (data[idx] >= '0' && data[idx] <= '9') 129 data[idx] = (byte)(data[idx] - ('0' - 52)); 130 else if (data[idx] >= 'a' && data[idx] <= 'z') 131 data[idx] = (byte)(data[idx] - ('a' - 26)); 132 else if (data[idx] >= 'A' && data[idx] <= 'Z') 133 data[idx] = (byte)(data[idx] - 'A'); 134 } 135 136 int sidx, didx; 138 for (sidx = 0, didx=0; didx < dest.length-2; sidx += 4, didx += 3) 139 { 140 dest[didx] = (byte) ( ((data[sidx] << 2) & 255) | 141 ((data[sidx+1] >>> 4) & 3) ); 142 dest[didx+1] = (byte) ( ((data[sidx+1] << 4) & 255) | 143 ((data[sidx+2] >>> 2) & 017) ); 144 dest[didx+2] = (byte) ( ((data[sidx+2] << 6) & 255) | 145 (data[sidx+3] & 077) ); 146 } 147 if (didx < dest.length) 148 { 149 dest[didx] = (byte) ( ((data[sidx] << 2) & 255) | 150 ((data[sidx+1] >>> 4) & 3) ); 151 } 152 if (++didx < dest.length) 153 { 154 dest[didx] = (byte) ( ((data[sidx+1] << 4) & 255) | 155 ((data[sidx+2] >>> 2) & 017) ); 156 } 157 return dest; 158 } 159 160 163 public static final void main(String args[]) 164 { 165 if (args.length != 1) 166 { 167 System.out.println("Usage: Base64 string"); 168 System.exit(0); 169 } 170 try 171 { 172 String e = Base64.encode(args[0].getBytes()); 173 String d = new String (Base64.decode(e)); 174 System.out.println("Input = '" + args[0] + "'"); 175 System.out.println("Encoded = '" + e + "'"); 176 System.out.println("Decoded = '" + d + "'"); 177 } 178 catch (Exception x) 179 { 180 x.printStackTrace(); 181 } 182 } 183 } 184 185 | Popular Tags |