Bases 64ノート
2651 ワード
@Test
public void Bases64(){
String s = "5b0aa3ebab"+":"+"aa2bc60e27e44e7bb23896d5d4002f79";
System.out.println(" :" + s);
String encryptString = encryptBASE64(s);
System.out.println(" :" + encryptString);
System.out.println(" :" + decryptBASE64(encryptString));
}
/**
* BASE64
*
* @param key
* @return
* @throws Exception
*/
public static String decryptBASE64(String key) {
byte[] bt;
try {
bt = (new BASE64Decoder()).decodeBuffer(key);
return new String(bt);// : String(bt, "utf-8") gbk
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
/**
* BASE64
*
* @param key
* @return
* @throws Exception
*/
public static String encryptBASE64(String key) {
byte[] bt = key.getBytes();
return (new BASE64Encoder()).encodeBuffer(bt);
}
/**
* @return
* @Description: base64
* @Author:
* @CreateTime:
*/
public static String getImageStr(String imgFile) {
InputStream inputStream = null;
byte[] data = null;
try {
inputStream = new FileInputStream(imgFile);
data = new byte[inputStream.available()];
inputStream.read(data);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
//
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}
/**
* @param imgStr base64
* @param path -
* @return
* @Description: base64
* @Author:
* @CreateTime:
*/
public static boolean generateImage(String imgStr, String path) {
if (imgStr == null) return false;
BASE64Decoder decoder = new BASE64Decoder();
try {
byte[] b = decoder.decodeBuffer(imgStr);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
OutputStream out = new FileOutputStream(path);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
@Test
public void test64() {
String imageStr = getImageStr("E:\\work\\ \\imag\\j01.png");
System.out.println(imageStr);
generateImage(imageStr, "E:\\work\\ \\imag\\j10.png");
}