base64

base64加密与URL
文章分类:Java编程
base64加密与URL
开发WEB的时候,比较常用的单向加密方式是MD5,比较常用的双向加密方法是BASE64。
以前我常用的BASE64加密方法是JDK1.5自带的,写成StringUtil如下:
加密
public static String base64Encode(String str) {
sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
return encoder.encodeBuffer(str.getBytes()).trim();
}

public static String base64Decode(String str) {
sun.misc.BASE64Decoder dec = new sun.misc.BASE64Decoder();
try {
return new String(dec.decodeBuffer(str));
} catch (IOException io) {
throw new RuntimeException(io.getMessage(), io.getCause());
}
}

这个方式不错,但是有时加密出的字符串在URL传递的时候会有问题,会被URL当做是特殊的自负而转掉,就算用
UrlEncode也不是很爽。

后来发现了一个新的改进版本的BASE64方法:

public static String base64Encode(String str) {
byte[] bytes = UrlBase64.encode(str.getBytes());
return new String(bytes);
}

public static String base64Decode(String str) {
byte[] bytes = UrlBase64.decode(str.getBytes());
return new String(bytes);
}

这里用到的包org.bouncycastle.util.encoders.UrlBase64是来自于bcprov-jdk15-136.jar
这里也贴个下载地址吧:)
https://www.360docs.net/doc/f42299716.html,/pub/mirrors/maven/bouncycastle/jars/bcprov-jdk15-136.jar
这种方式就能解决BASE64在URL传递中遇到的问题啦:)

新的解决方法:

加密算法基于Base64,但为了便于在URL中使用,对该算法进行了变异处理。加/解密的工具类封装在base64utils.jar包中(请下载),另专门有一个类URLEncypt简化字符串的加密和解密。

如下:

public static String encode(String str) {
String ret = null;
if (str == null || str.trim().length() == 0) return str;
try {
byte[] bytes = UrlBase64.encode(str.getBytes());
ret = new String(bytes);
} catch (Exception e) {
e.printStackTrace();
}
// System.out.println("rawStr=" + str + "\nencodedStr=" + ret + "\ndecodedStr=" + decode(ret));
return ret;
}

public static String decode(String str) {
String ret = null;
if (str == null || str.trim().length() == 0) return str;
try {
byte[] bytes = UrlBase64.decode(str.getBytes());
ret = new String(bytes);
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
==================
import java.io.IOException;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Base64String
{

/**base64字符串与普通字符串之间的转换
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException
{
//base64加


String utf8string1 = "字符串之间的转换";
System.out.println("utf8string1 =" +utf8string1);
BASE64Encoder bASE64Encoder = new BASE64Encoder();
String base64string = bASE64Encoder.encode(utf8string1.getBytes("UTF-8"));
System.out.println("base64string = "+base64string);
//base64解密
BASE64Decoder bASE64Decoder = new BASE64Decoder();
byte[] bt = bASE64Decoder.decodeBuffer("5a2X56ym5Liy5LmL6Ze055qE6L2s5o2i");
String utf8String2 = new String(bt,"UTF-8");
System.out.println("utf8String2 = "+ utf8String2);
}

==================

Java代码
package com.base64;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class PictureAndBase64 {
public static void main(String[] args) {
// 测试从图片文件转换为Base64编码
String strImg = GetImageStr("C:\\test.bmp");
System.out.println(strImg);
// 测试从Base64编码转换为图片文件
GenerateImage(strImg, "C:\\test.bmp");
}
public static String GetImageStr(String imgFilePath) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
if(imgFilePath == null || imgFilePath == ""){
return "";
}
File file = new File(imgFilePath);
if(!file.exists()){
return "";
}
byte[] data = null;
// 读取图片字节数组
try {
InputStream in = new FileInputStream(imgFilePath);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);// 返回Base64编码过的字节数组字符串
}

// 对字节数组字符串进行Base64解码并生成图片
public static boolean GenerateImage(String imgStr, String imgFilePath) {
if (imgStr == null || imgStr == "") // 图像数据为空
return false;
BASE64Decoder decoder = new BASE64Decoder();
try {
// Base64解码
byte[] bytes = decoder.decodeBuffer(imgStr);
for (int i = 0; i < bytes.length; ++i) {
if (bytes[i] < 0) {// 调整异常数据
bytes[i] += 256;
}
}
// 生成jpeg图片
OutputStream out = new FileOutputStream(imgFileP

ath);
out.write(bytes);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
}
=======================

字符编码解码之乱码
悬赏:5 发布时间:2009-05-19 提问人:huntingbaby (初级程序员)
< > 猎头职位: 安徽: 合肥,杭州,苏州:诚聘java架构师
见题:

有一中文字符串“随便一做”,我想编码 再反编码 ,请教能否给点建议,以及对编码的指导?



Java代码
String temp = "随便一做";
String tempUTF = null;
tempUTF = new String(temp.getBytes("utf-8"),"gbk");
System.out.println("gbk to utf8 : "+tempUTF);
System.out.println("utf8 to gbk : "+new String(tempUTF.getBytes("gbk"),"utf-8"));

BASE64Encoder encoder = new BASE64Encoder();
BASE64Decoder decoder = new BASE64Decoder();
String t1 = encoder.encode(tempUTF.getBytes());
String t2 = new String(decoder.decodeBuffer(t1)).toString();
String t3 = new String(t2.getBytes("gbk"),"UTF-8");
System.out.println("utf8 to base64 : "+t1);
System.out.println("base64 to utf8 : "+t2);
System.out.println("utf8 to gbk : "+t3);

String t4 = encoder.encode(temp.getBytes());
String t5 = new String(decoder.decodeBuffer(t4)).toString();
System.out.println("gbk to base64 : "+t4);
System.out.println("base64 to gbk : "+t5);


来的结果是:
1)没有得到预期效果:
gbk to utf8 : 闅忎究涓?鍋?
utf8 to gbk : 随便????
2)同上:
utf8 to base64 : 6ZqP5L6/5Lg/5YE/
base64 to utf8 : 闅忎究涓?鍋?
utf8 to gbk : 随便????
3)理想效果:
gbk to base64 : y+ax49K71/Y=
base64 to gbk : 随便一做

String temp = "随便一做";
String tempUTF = null;
tempUTF = new String(temp.getBytes("utf-8"),"gbk");
System.out.println("gbk to utf8 : "+tempUTF);
System.out.println("utf8 to gbk : "+new String(tempUTF.getBytes("gbk"),"utf-8"));

BASE64Encoder encoder = new BASE64Encoder();
BASE64Decoder decoder = new BASE64Decoder();
String t1 = encoder.encode(tempUTF.getBytes());
String t2 = new String(decoder.decodeBuffer(t1)).toString();
String t3 = new String(t2.getBytes("gbk"),"UTF-8");
System.out.println("utf8 to base64 : "+t1);
System.out.println("base64 to utf8 : "+t2);
System.out.println("utf8 to gbk : "+t3);

String t4 = encoder.encode(temp.getBytes());
String t5 = new String(decoder.decodeBuffer(t4)).toString();
System.out.println("gbk to base64 : "+t4);
System.out.println("base64 to gbk : "+t5);


得出来的结果是:
1)没有得到预期效果:
gbk to utf8 : 闅忎究涓?鍋?

utf8 to gbk : 随便????
2)同上:
utf8 to base64 : 6ZqP5L6/5Lg/5YE/
base64 to utf8 : 闅忎究涓?鍋?
utf8 to gbk : 随便????
3)理想效果:
gbk to base64 : y+ax49K71/Y=
base64 to gbk : 随便一做
问题补充:
对于上面的编码问题,我是想解决从base64——》utf8的效果。其实例有:

有没有人能帮我把以下字符串变成中文:
56CB5bCa5ouN77ya6ZqP5L6/5LiA5YGa
(以上字符串经过base64编码的)


其解决方法:
Java代码
BASE64Decoder decoder = new BASE64Decoder();
String str;
try {
str = new String(decoder.decodeBuffer(s),"utf-8");
System.out.println(str);
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}

BASE64Decoder decoder = new BASE64Decoder();
String str;
try {
str = new String(decoder.decodeBuffer(s),"utf-8");
System.out.println(str);
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
其结果为:“码尚拍:随便一做 ”

而我原来的方法是:
Java代码
BASE64Decoder decoder = new BASE64Decoder();
String str = new String(decoder.decodeBuffer(baseStr)).toString();
System.out.println(str);
str = new String(str.getBytes("gbk"),"utf-8");
System.out.println(str);

BASE64Decoder decoder = new BASE64Decoder();
String str = new String(decoder.decodeBuffer(baseStr)).toString();
System.out.println(str);
str = new String(str.getBytes("gbk"),"utf-8");
System.out.println(str);

上面解码的跟下面这个有什么不一样吗?


相关文档
最新文档