1 package net.matuschek.util; 2 3 25 26 27 33 34 42 public class Base64 43 { 44 45 private Base64() 46 { 47 super(); 48 } 49 50 53 public final static String encode(byte[] d) 54 { 55 if (d == null) return null; 56 byte data[] = new byte[d.length+2]; 57 System.arraycopy(d, 0, data, 0, d.length); 58 byte dest[] = new byte[(data.length/3)*4]; 59 60 for (int sidx = 0, didx=0; sidx < d.length; sidx += 3, didx += 4) 62 { 63 dest[didx] = (byte) ((data[sidx] >>> 2) & 077); 64 dest[didx+1] = (byte) ((data[sidx+1] >>> 4) & 017 | 65 (data[sidx] << 4) & 077); 66 dest[didx+2] = (byte) ((data[sidx+2] >>> 6) & 003 | 67 (data[sidx+1] << 2) & 077); 68 dest[didx+3] = (byte) (data[sidx+2] & 077); 69 } 70 71 for (int idx = 0; idx <dest.length; idx++) 73 { 74 if (dest[idx] < 26) dest[idx] = (byte)(dest[idx] + 'A'); 75 else if (dest[idx] < 52) dest[idx] = (byte)(dest[idx] + 'a' - 26); 76 else if (dest[idx] < 62) dest[idx] = (byte)(dest[idx] + '0' - 52); 77 else if (dest[idx] < 63) dest[idx] = (byte)'+'; 78 else dest[idx] = (byte)'/'; 79 } 80 81 for (int idx = dest.length-1; idx > (d.length*4)/3; idx--) 83 { 84 dest[idx] = (byte)'='; 85 } 86 return new String (dest); 87 } 88 89 92 public final static String encode(String s) { 93 return encode(s.getBytes()); 94 } 95 96 99 public final static byte[] decode(String str) 100 { 101 if (str == null) return null; 102 byte data[] = str.getBytes(); 103 return decode(data); 104 } 105 106 110 public final static byte[] decode(byte[] data) 111 { 112 int tail = data.length; 113 while (data[tail-1] == '=') tail--; 114 byte dest[] = new byte[tail - data.length/4]; 115 116 for (int idx = 0; idx <data.length; idx++) 118 { 119 if (data[idx] == '=') data[idx] = 0; 120 else if (data[idx] == '/') data[idx] = 63; 121 else if (data[idx] == '+') data[idx] = 62; 122 else if (data[idx] >= '0' && data[idx] <= '9') 123 data[idx] = (byte)(data[idx] - ('0' - 52)); 124 else if (data[idx] >= 'a' && data[idx] <= 'z') 125 data[idx] = (byte)(data[idx] - ('a' - 26)); 126 else if (data[idx] >= 'A' && data[idx] <= 'Z') 127 data[idx] = (byte)(data[idx] - 'A'); 128 } 129 130 int sidx, didx; 132 for (sidx = 0, didx=0; didx < dest.length-2; sidx += 4, didx += 3) 133 { 134 dest[didx] = (byte) ( ((data[sidx] << 2) & 255) | 135 ((data[sidx+1] >>> 4) & 3) ); 136 dest[didx+1] = (byte) ( ((data[sidx+1] << 4) & 255) | 137 ((data[sidx+2] >>> 2) & 017) ); 138 dest[didx+2] = (byte) ( ((data[sidx+2] << 6) & 255) | 139 (data[sidx+3] & 077) ); 140 } 141 if (didx < dest.length) 142 { 143 dest[didx] = (byte) ( ((data[sidx] << 2) & 255) | 144 ((data[sidx+1] >>> 4) & 3) ); 145 } 146 if (++didx < dest.length) 147 { 148 dest[didx] = (byte) ( ((data[sidx+1] << 4) & 255) | 149 ((data[sidx+2] >>> 2) & 017) ); 150 } 151 return dest; 152 } 153 154 157 public static final void main(String [] args) 158 { 159 if (args.length != 1) 160 { 161 System.out.println("Usage: Base64 string"); 162 System.exit(0); 163 } 164 try 165 { 166 String e = Base64.encode(args[0].getBytes()); 167 String d = new String (Base64.decode(e)); 168 System.out.println("Input = '" + args[0] + "'"); 169 System.out.println("Encoded = '" + e + "'"); 170 System.out.println("Decoded = '" + d + "'"); 171 } 172 catch (Exception x) 173 { 174 x.printStackTrace(); 175 } 176 } 177 } 178 | Popular Tags |