base64coder调用
base64coder 可以查看官网: http://www.source-code.biz/base64coder/java/
我所涉及到的 base64coder调用:
某天,因需要修改Properties文件中的部分内容,而该内容是base64做过加密的,所有要进行解密、修改、加密、存储,
主要对 gui_preferences 中的color 进行修改,gui_preferences 在Properties文件中如下:
gui_preferences = rO0ABXNyABFqYXZhLnV0aWwuSGFzaE1hcAUH2sHDFmDRAwACRgAKbG9hZEZhY3RvckkACXRocmVzaG9sZHhwP0AAAAAAAAx3CAAAABAAAAAKdAAYYXBwX3ByZWZlcmVuY2VzX3JldmlzaW9uc3IAEWphdmEubGFuZy5JbnRlZ2VyEuKgpPeBhzgCAAFJAAV2YWx1ZXhyABBqYXZhLmxhbmcuTnVtYmVyhqyVHQuU4IsCAAB4cAAAAAF0AA5hcHBfcGFuZWxfZm9udHNyAA1qYXZhLmF3dC5Gb250xaE15szeVnMDAAZJABlmb250U2VyaWFsaXplZERhdGFWZXJzaW9uRgAJcG9pbnRTaXplSQAEc2l6ZUkABXN0eWxlTAAUZlJlcXVlc3RlZEF0dHJpYnV0ZXN0ABVMamF2YS91dGlsL0hhc2h0YWJsZTtMAARuYW1ldAASTGphdmEvbGFuZy9TdHJpbmc7eHAAAAABQSAAAAAAAAoAAAAAcHQABlRhaG9tYXh0ABNhcHBfc2Nyb2xsYmFyX3dpZHRodAAFc21hbGx0ABBhcHBfYm9yZGVyX3dpZHRoc3EAfgADAAAAAnQAFGFwcF90aXRsZV9mb3JlZ3JvdW5kc3IADmphdmEuYXd0LkNvbG9yAaUXgxCPM3UCAAVGAAZmYWxwaGFJAAV2YWx1ZUwAAmNzdAAbTGphdmEvYXd0L2NvbG9yL0NvbG9yU3BhY2U7WwAJZnJnYnZhbHVldAACW0ZbAAZmdmFsdWVxAH4AE3hwAAAAAP////9wcHB0ABBhcHBfYm9yZGVyX2NvbG9yc3EAfgARAAAAAP+JiYlwcHB0ABBhcHBfZ3JhZGllbnRfbWluc3EAfgARAAAAAP+z7jpwcHB0AA5hcHBfdGl0bGVfZm9udHNxAH4ABwAAAAFBQAAAAAAADAAAAAFwdAAFQXJpYWx4dAAQYXBwX2NvbG9yX3NjaGVtZXQACnNlbGZEZXNpbmd0ABBhcHBfZ3JhZGllbnRfbWF4c3EAfgARAAAAAP+50+5wcHB4
这个Color对象就存在此密文中【红色部分及目标】,所以得解密然后才能看到,我把解密后的内容列出来供对比,
app_preferences_revision:1
app_border_width:2
app_scrollbar_width:small
app_panel_font:java.awt.Font[family=Tahoma,name=Tahoma,style=plain,size=10]
app_title_foreground:java.awt.Color[r=255,g=255,b=255]
app_border_color:java.awt.Color[r=137,g=137,b=137]
app_gradient_min:java.awt.Color[r=179,g=238,b=58]
app_title_font:java.awt.Font[family=Arial,name=Arial,style=bold,size=12]
app_gradient_max:java.awt.Color[r=185,g=211,b=238]
app_color_scheme:selfDesing
涉及 :base64加密解密,对象序列化
以下为此过程,另附 base64coder 文件。
一调用过程:
1 import java.awt.Color; 2 import java.io.ByteArrayInputStream; 3 import java.io.ByteArrayOutputStream; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.FileNotFoundException; 7 import java.io.FileOutputStream; 8 import java.io.IOException; 9 import java.io.ObjectInputStream; 10 import java.io.ObjectOutputStream; 11 import java.util.HashMap; 12 import java.util.Iterator; 13 import java.util.Properties; 14 15 16 /** 17 * @author yang 18 * @category 19 * @see http://www.114la.com/other/rgb.htm 20 */ 21 22 public class Do { 23 24 public static void main(String[] args) { 25 26 //1 read propriety 27 String filepath="D:\\ec.properties"; 28 29 //-----------------gui_preferences-------------- 30 String gui_preferences="gui_preferences"; 31 Color color_gradient_min=new Color(179,238,58); 32 Color color_gradient_max=new Color(185,211,238); 33 String color_gradient_scheme="selfDesing"; 34 //-----------------gui_preferences-------------- 35 36 37 Properties pro=new Properties(); 38 try { 39 pro.load(new FileInputStream(new File(filepath))); 40 } catch (FileNotFoundException e) { 41 // TODO Auto-generated catch block 42 e.printStackTrace(); 43 } catch (IOException e) { 44 // TODO Auto-generated catch block 45 e.printStackTrace(); 46 } 47 ///gui_preferences 48 System.out.println(pro.getProperty(gui_preferences)); 49 String tempstr=pro.getProperty(gui_preferences); 50 51 //2 decode 52 //HashMap hm=new HashMap(); 53 HashMap hm=Do.getMapFromString(tempstr); 54 Iterator t=hm.keySet().iterator(); 55 56 //3 modify 57 while(t.hasNext()){ 58 59 String s=(String) t.next(); 60 // System.out.println(s+":"+hm.get(s)); 61 if(s.equals("app_gradient_min")) 62 { 63 hm.put("app_gradient_min", color_gradient_min); 64 } 65 if(s.equals("app_gradient_max")) 66 { 67 hm.put("app_gradient_max", color_gradient_max); 68 } 69 if(s.equals("app_color_scheme")) 70 { 71 hm.put("app_color_scheme", color_gradient_scheme); 72 } 73 } 74 75 //print 76 // Iterator i=hm.keySet().iterator(); 77 // 78 // while(i.hasNext()){ 79 // String s=(String) i.next(); 80 // System.out.println(s+":"+hm.get(s)); 81 // } 82 83 84 //4 encode 85 if(hm!=null && hm.size()>0){ 86 try { 87 pro.setProperty(gui_preferences,getStringFromObject(hm)); 88 } catch (IOException e) { 89 // TODO Auto-generated catch block 90 e.printStackTrace(); 91 } 92 } 93 94 //5 save 95 try { 96 pro.store(new FileOutputStream(new File(filepath)), ""); 97 } catch (FileNotFoundException e) { 98 // TODO Auto-generated catch block 99 e.printStackTrace(); 100 } catch (IOException e) { 101 // TODO Auto-generated catch block 102 e.printStackTrace(); 103 } 104 105 } 106 107 private static Object getObjectFromString(String value) throws IOException, ClassNotFoundException, IOException { 108 Object obj = null; 109 ByteArrayOutputStream out = new ByteArrayOutputStream(); 110 ByteArrayInputStream bin = new ByteArrayInputStream(value.getBytes()); 111 Base64Coder.decodeStream(bin, out); 112 113 bin = new ByteArrayInputStream(out.toByteArray()); 114 ObjectInputStream is = new ObjectInputStream(bin); 115 obj = is.readObject(); 116 117 return obj; 118 } 119 120 public static HashMap getMapFromString(String value) { 121 HashMap map = null; 122 if (value != null) { 123 try { 124 Object obj = getObjectFromString(value); 125 if (obj != null) { 126 if (obj instanceof HashMap) { 127 map = (HashMap) obj; 128 } 129 } 130 131 } catch (Exception ex) { 132 //logger.log(Level.SEVERE, "Can't get hash map from string: " + value, ex); 133 System.out.println("Can't get hash map from string: " + ex); 134 } 135 } 136 return map; 137 } 138 139 public static String getStringFromObject(Object obj) throws IOException { 140 String str = null; 141 ByteArrayOutputStream out = new ByteArrayOutputStream(); 142 ObjectOutputStream os = new ObjectOutputStream(out); 143 os.writeObject(obj); 144 ByteArrayInputStream bin = new ByteArrayInputStream(out.toByteArray()); 145 out.reset(); 146 //Base64Encoder be = new Base64Encoder(bin, out); 147 Base64Coder.encodeStream(bin, out); 148 str = out.toString(); 149 return str; 150 } 151 }
二 base64coder 文件
/** * A Base64 Encoder/Decoder. * * <p> * This class is used to encode and decode data in Base64 format as described in RFC 1521. * * <p> * This is "Open Source" software and released under the <a href="http://www.gnu.org/licenses/lgpl.html">GNU/LGPL</a> license.<br> * It is provided "as is" without warranty of any kind.<br> * Copyright 2003: Christian d'Heureuse, Inventec Informatik AG, Switzerland.<br> * Home page: <a href="http://www.source-code.biz">www.source-code.biz</a><br> * * <p> * Version history:<br> * 2003-07-22 Christian d'Heureuse (chdh): Module created.<br> * 2005-08-11 chdh: Lincense changed from GPL to LGPL.<br> * 2006-11-21 chdh:<br> * Method encode(String) renamed to encodeString(String).<br> * Method decode(String) renamed to decodeString(String).<br> * New method encode(byte[],int) added.<br> * New method decode(String) added.<br> */ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Base64Coder { // Mapping table from 6-bit nibbles to Base64 characters. private static char[] map1 = new char[64]; static { int i = 0; for (char c = 'A'; c <= 'Z'; c++) { map1[i++] = c; } for (char c = 'a'; c <= 'z'; c++) { map1[i++] = c; } for (char c = '0'; c <= '9'; c++) { map1[i++] = c; } map1[i++] = '+'; map1[i++] = '/'; } // Mapping table from Base64 characters to 6-bit nibbles. private static byte[] map2 = new byte[128]; static { for (int i = 0; i < map2.length; i++) { map2[i] = -1; } for (int i = 0; i < 64; i++) { map2[map1[i]] = (byte) i; } } /** * Encodes a string into Base64 format. * No blanks or line breaks are inserted. * @param s a String to be encoded. * @return A String with the Base64 encoded data. */ public static String encodeString(String s) { return new String(encode(s.getBytes())); } /** * Encodes a byte array into Base64 format. * No blanks or line breaks are inserted. * @param in an array containing the data bytes to be encoded. * @return A character array with the Base64 encoded data. */ public static char[] encode(byte[] in) { return encode(in, in.length); } /** * Encodes a byte array into Base64 format. * No blanks or line breaks are inserted. * @param in an array containing the data bytes to be encoded. * @param iLen number of bytes to process in <code>in</code>. * @return A character array with the Base64 encoded data. */ public static char[] encode(byte[] in, int iLen) { int oDataLen = (iLen * 4 + 2) / 3; // output length without padding int oLen = ((iLen + 2) / 3) * 4; // output length including padding char[] out = new char[oLen]; int ip = 0; int op = 0; while (ip < iLen) { int i0 = in[ip++] & 0xff; int i1 = ip < iLen ? in[ip++] & 0xff : 0; int i2 = ip < iLen ? in[ip++] & 0xff : 0; int o0 = i0 >>> 2; int o1 = ((i0 & 3) << 4) | (i1 >>> 4); int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6); int o3 = i2 & 0x3F; out[op++] = map1[o0]; out[op++] = map1[o1]; out[op] = op < oDataLen ? map1[o2] : '='; op++; out[op] = op < oDataLen ? map1[o3] : '='; op++; } return out; } /** * Decodes a string from Base64 format. * @param s a Base64 String to be decoded. * @return A String containing the decoded data. * @throws IllegalArgumentException if the input is not valid Base64 encoded data. */ public static String decodeString(String s) { return new String(decode(s)); } /** * Decodes a byte array from Base64 format. * @param s a Base64 String to be decoded. * @return An array containing the decoded data bytes. * @throws IllegalArgumentException if the input is not valid Base64 encoded data. */ public static byte[] decode(String s) { return decode(s.toCharArray()); } /** * Decodes a byte array from Base64 format. * No blanks or line breaks are allowed within the Base64 encoded data. * @param in a character array containing the Base64 encoded data. * @return An array containing the decoded data bytes. * @throws IllegalArgumentException if the input is not valid Base64 encoded data. */ public static byte[] decode(char[] in) { int iLen = in.length; if (iLen % 4 != 0) { throw new IllegalArgumentException("Length of Base64 encoded input string is not a multiple of 4."); } while (iLen > 0 && in[iLen - 1] == '=') { iLen--; } int oLen = (iLen * 3) / 4; byte[] out = new byte[oLen]; int ip = 0; int op = 0; while (ip < iLen) { int i0 = in[ip++]; int i1 = in[ip++]; int i2 = ip < iLen ? in[ip++] : 'A'; int i3 = ip < iLen ? in[ip++] : 'A'; if (i0 > 127 || i1 > 127 || i2 > 127 || i3 > 127) { throw new IllegalArgumentException("Illegal character in Base64 encoded data."); } int b0 = map2[i0]; int b1 = map2[i1]; int b2 = map2[i2]; int b3 = map2[i3]; if (b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0) { throw new IllegalArgumentException("Illegal character in Base64 encoded data."); } int o0 = (b0 << 2) | (b1 >>> 4); int o1 = ((b1 & 0xf) << 4) | (b2 >>> 2); int o2 = ((b2 & 3) << 6) | b3; out[op++] = (byte) o0; if (op < oLen) { out[op++] = (byte) o1; } if (op < oLen) { out[op++] = (byte) o2; } } return out; } public static void decodeStream(InputStream in, OutputStream out) throws IOException { String s = inputStreamToString(in); byte[] buf = decode(s); out.write(buf); } public static String inputStreamToString(InputStream in) throws IOException { StringBuffer out = new StringBuffer(); byte[] b = new byte[4096]; for (int n; (n = in.read(b)) != -1;) { out.append(new String(b, 0, n)); } return out.toString(); } public static void encodeStream(InputStream in, OutputStream out) throws IOException { int lineLength = 72; byte[] buf = new byte[lineLength / 4 * 3]; while (true) { int len = in.read(buf); if (len <= 0) { break; } String str = new String(encode(buf, len)); out.write(str.getBytes()); } } // Dummy constructor. private Base64Coder() { } } // end class Base64Coder