base64coder调用

时间:2023-03-09 15:56:40
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 文件。

一调用过程:

 import java.awt.Color;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties; /**
* @author yang
* @category
* @see http://www.114la.com/other/rgb.htm
*/ public class Do { public static void main(String[] args) { //1 read propriety
String filepath="D:\\ec.properties"; //-----------------gui_preferences--------------
String gui_preferences="gui_preferences";
Color color_gradient_min=new Color(179,238,58);
Color color_gradient_max=new Color(185,211,238);
String color_gradient_scheme="selfDesing";
//-----------------gui_preferences-------------- Properties pro=new Properties();
try {
pro.load(new FileInputStream(new File(filepath)));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
///gui_preferences
System.out.println(pro.getProperty(gui_preferences));
String tempstr=pro.getProperty(gui_preferences); //2 decode
//HashMap hm=new HashMap();
HashMap hm=Do.getMapFromString(tempstr);
Iterator t=hm.keySet().iterator(); //3 modify
while(t.hasNext()){ String s=(String) t.next();
// System.out.println(s+":"+hm.get(s));
if(s.equals("app_gradient_min"))
{
hm.put("app_gradient_min", color_gradient_min);
}
if(s.equals("app_gradient_max"))
{
hm.put("app_gradient_max", color_gradient_max);
}
if(s.equals("app_color_scheme"))
{
hm.put("app_color_scheme", color_gradient_scheme);
}
} //print
// Iterator i=hm.keySet().iterator();
//
// while(i.hasNext()){
// String s=(String) i.next();
// System.out.println(s+":"+hm.get(s));
// } //4 encode
if(hm!=null && hm.size()>0){
try {
pro.setProperty(gui_preferences,getStringFromObject(hm));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} //5 save
try {
pro.store(new FileOutputStream(new File(filepath)), "");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } private static Object getObjectFromString(String value) throws IOException, ClassNotFoundException, IOException {
Object obj = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream bin = new ByteArrayInputStream(value.getBytes());
Base64Coder.decodeStream(bin, out); bin = new ByteArrayInputStream(out.toByteArray());
ObjectInputStream is = new ObjectInputStream(bin);
obj = is.readObject(); return obj;
} public static HashMap getMapFromString(String value) {
HashMap map = null;
if (value != null) {
try {
Object obj = getObjectFromString(value);
if (obj != null) {
if (obj instanceof HashMap) {
map = (HashMap) obj;
}
} } catch (Exception ex) {
//logger.log(Level.SEVERE, "Can't get hash map from string: " + value, ex);
System.out.println("Can't get hash map from string: " + ex);
}
}
return map;
} public static String getStringFromObject(Object obj) throws IOException {
String str = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(out);
os.writeObject(obj);
ByteArrayInputStream bin = new ByteArrayInputStream(out.toByteArray());
out.reset();
//Base64Encoder be = new Base64Encoder(bin, out);
Base64Coder.encodeStream(bin, out);
str = out.toString();
return str;
}
}

二 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>
* &nbsp; Method encode(String) renamed to encodeString(String).<br>
* &nbsp; Method decode(String) renamed to decodeString(String).<br>
* &nbsp; New method encode(byte[],int) added.<br>
* &nbsp; 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