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

base64coder调用的更多相关文章

  1. 黑马毕向东Java基础知识总结

    Java基础知识总结(超级经典) 转自:百度文库 黑马毕向东JAVA基础总结笔记    侵删! 写代码: 1,明确需求.我要做什么? 2,分析思路.我要怎么做?1,2,3. 3,确定步骤.每一个思路部 ...

  2. 《果壳中的C# C# 5.0 权威指南》 - 学习笔记

    <果壳中的C# C# 5.0 权威指南> ========== ========== ==========[作者] (美) Joseph Albahari (美) Ben Albahari ...

  3. linux下java调用C

    linux下java调用C 分类: linux2012-05-22 09:12 1529人阅读 评论(0) 收藏 举报 javalinuxmakefilegccclasscommand 下面是在ubu ...

  4. JS调用Android、Ios原生控件

    在上一篇博客中已经和大家聊了,关于JS与Android.Ios原生控件之间相互通信的详细代码实现,今天我们一起聊一下JS调用Android.Ios通信的相同点和不同点,以便帮助我们在进行混合式开发时, ...

  5. 【原创分享·支付宝支付】HBuilder打包APP调用支付宝客户端支付

    前言 最近有点空余时间,所以,就研究了一下APP支付.前面很早就搞完APP的微信支付了,但是由于时间上和应用上的情况,支付宝一直没空去研究.然后等我空了的时候,发现支付宝居然升级了支付逻辑,虽然目前还 ...

  6. 操作系统篇-调用门与特权级(CPL、DPL和RPL)

    || 版权声明:本文为博主原创文章,未经博主允许不得转载. 一.前言 在前两篇文章(<操作系统篇-浅谈实模式与保护模式>和<操作系统篇-分段机制与GDT|LDT>)中,我们提到 ...

  7. 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)

    一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...

  8. django server之间通过remote user 相互调用

    首先,场景是这样的:存在两个django web应用,并且两个应用存在一定的联系.某些情况下彼此需要获取对方的数据. 但是我们的应用肯经都会有对应的鉴权机制.不会让人家随随便便就访问的对吧.好比上车要 ...

  9. 调用AJAX做登陆和注册

    先建立一个页面来检测一下我们建立的用户名能不能用,看一下有没有已经存在的用户名吗 可以通过ajax提示一下 $("#uid").blur(function(){ //取用户名 va ...

随机推荐

  1. Gym 100637F F. The Pool for Lucky Ones

    F. The Pool for Lucky Ones Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  2. 循环日期的shell

    date="2015-09-23"enddate='2015-11-08'while [[ $date < $enddate ]] do date=`date -d &quo ...

  3. MySQL出现无法删除行记录

    今天mysql在删除一张InnoDB类型的表时,出现错误Error No. 1451 MYSQL: Cannot delete or update a parent row: a foreign ke ...

  4. “无法加载一个或多个请求的类型。有关更多信息,请检索 LoaderExceptions 属性 “之解决

    今天在学习插件系统设计的时候遇到一个问题:“System.Reflection.ReflectionTypeLoadException: 无法加载一个或多个请求的类型. 于是百度一下,很多内容都差不多 ...

  5. cocos2d 定时器

    //获取当前系统的语言 LanguageType language=CCApplication::sharedApplication()->getCurrentLanguage(); //每一帧 ...

  6. BZOJ4298 : [ONTAK2015]Bajtocja

    设f[i][j]为第i张图中j点所在连通块的编号,加边时可以通过启发式合并在$O(dn\log n)$的时间内维护出来. 对于每个点,设h[i]为f[j][i]的hash值,若两个点hash值相等,则 ...

  7. BZOJ3577 : 玩手机

    很明显网络流. S到每个发射站连边,容量为该站限制 每个接收站到T连边,容量为该站限制 矩阵每个点拆成两个点i和i',i向i'连边,容量为该位置手机数 每个发射站向该正方形内所有点i连边,容量为无穷大 ...

  8. Session赋值(备注)

    Session赋值也是在后台赋,不是在前台赋 追问 不好意思 那还真能在AJAX中赋值 我已经解决了 加一个接口IRequiresSessionState 就OK 提问者评价 太感谢了,真心有用

  9. redis API使用说明

    List相关: LPOP key : 删除并取得LIST头部一个元素 RPOP key : 删除并取得LIST尾部一个元素 BLPOP key [key ...] timeout : 删除并取得LIS ...

  10. Hashtable和Dictionary<T,K>的使用

    由于Hashtable内部自带有排序(根据Key的HashCode来进行的),因此有时在使用Hashtable时就会造成数据顺序不可控的情况,有两种办法可以解决, 测试代码: Dictionary&l ...