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);
}
// 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>
* 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
base64coder调用的更多相关文章
- 黑马毕向东Java基础知识总结
Java基础知识总结(超级经典) 转自:百度文库 黑马毕向东JAVA基础总结笔记 侵删! 写代码: 1,明确需求.我要做什么? 2,分析思路.我要怎么做?1,2,3. 3,确定步骤.每一个思路部 ...
- 《果壳中的C# C# 5.0 权威指南》 - 学习笔记
<果壳中的C# C# 5.0 权威指南> ========== ========== ==========[作者] (美) Joseph Albahari (美) Ben Albahari ...
- linux下java调用C
linux下java调用C 分类: linux2012-05-22 09:12 1529人阅读 评论(0) 收藏 举报 javalinuxmakefilegccclasscommand 下面是在ubu ...
- JS调用Android、Ios原生控件
在上一篇博客中已经和大家聊了,关于JS与Android.Ios原生控件之间相互通信的详细代码实现,今天我们一起聊一下JS调用Android.Ios通信的相同点和不同点,以便帮助我们在进行混合式开发时, ...
- 【原创分享·支付宝支付】HBuilder打包APP调用支付宝客户端支付
前言 最近有点空余时间,所以,就研究了一下APP支付.前面很早就搞完APP的微信支付了,但是由于时间上和应用上的情况,支付宝一直没空去研究.然后等我空了的时候,发现支付宝居然升级了支付逻辑,虽然目前还 ...
- 操作系统篇-调用门与特权级(CPL、DPL和RPL)
|| 版权声明:本文为博主原创文章,未经博主允许不得转载. 一.前言 在前两篇文章(<操作系统篇-浅谈实模式与保护模式>和<操作系统篇-分段机制与GDT|LDT>)中,我们提到 ...
- 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)
一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...
- django server之间通过remote user 相互调用
首先,场景是这样的:存在两个django web应用,并且两个应用存在一定的联系.某些情况下彼此需要获取对方的数据. 但是我们的应用肯经都会有对应的鉴权机制.不会让人家随随便便就访问的对吧.好比上车要 ...
- 调用AJAX做登陆和注册
先建立一个页面来检测一下我们建立的用户名能不能用,看一下有没有已经存在的用户名吗 可以通过ajax提示一下 $("#uid").blur(function(){ //取用户名 va ...
随机推荐
- 013_VM+WinDbg安装
预计平均三天一课,录制过程中,大纲会实时更新(更改) 主讲:郁金香灬老师 QQ150330575 开发环境:VC6,VS2003,VS2008 www.yjxsoft.net www.yjxsoft ...
- 电赛总结(二)——AD芯片总结之AD7705
1.特性参数 (1)16位无丢失代码性AD转化器 (2)只需要3.3V供电(即只需要单片机即可供电) (3)双通道差分输入 (4)基准电压为2.5V时,在单极性信号下,输入范围是0到2.5V,在双极性 ...
- WPF/Silverlight Template使用及总结(转)
WPF/Silverlight 中的控件都有Style和Template两种属性.前者解释为样式,是用来改变控件原有属性的,比如 Button 控件的(Width,Height,Background ...
- Hadoop_10_shuffle02_详解Shuffle过程【来源网络】推荐更为详细
网址:http://www.cnblogs.com/felixzh/p/4680808.html Shuffle过程,也称Copy阶段.reduce task从各个map task上远程拷贝一片数据, ...
- BZOJ3476 : [Usaco2014 Mar]The Lazy Cow
旋转坐标系后转化为正方形,$x'=x+y$,$y'=x-y+1000001$,$k'=2k-1$ 两根扫描线从左往右扫 f[i]表示y坐标下边界为i时的价值和 每次加入/删除一个点等价于一段区间加减 ...
- svg―Raphael.js Library
Raphael是一个用于在网页中绘制矢量图形的Javascript库,它使用SVG W3C推荐标准和VML作为创建图形的基础,可以通过JavaScript操作DOM来轻松创建出各种复杂的柱状图.饼图. ...
- MySQL修改root密码的各种方法整理
方法一: 1.首先cmd中输入 net stop mysql //停止Mysql服务 2.然后在my.ini文件中的[mysqld]下面一行添加 skip_grant_tables 3.在cmd ...
- RN组件之ListView
/** * Created by DaGuo on 2016/4/7. */ 'use strict' import React,{ Component, View, Text, ListView, ...
- meta标签的用法
meta是用来在HTML文档中模拟HTTP协议的响应头报文.meta 标签用于网页的<head>与</head>中,meta 标签的用处很多.meta 的属性有两种:name和 ...
- 三层架构实例 VB.NET版
三层实例 首先发现感慨,对于三成这块,用到都是一些面向对象的特征,尤其是对象的实例化.如果你不是很注意的话,那么,你就会一头雾水,就像我一样,慢慢的雾里看花,最后也是走出来的,不过用的事件是相当的. ...