用base64加密解密文件
2012-06-16 10:52
459 查看
用jdk的base64工具加密一副图片,
思路是:先将图片加密,得到加密后的字符串,然后保存到本地磁盘的某个txt文件,然后读出这个文件,将其中的内容独到byte数组中,再解密。得到解密后的字节数组,通过流,写到本地磁盘上,得到一副图片。
加密方法:
static String filedc() throws Exception {
InputStream is = null;
File file = new File("E:/a.png");
String fileString = "";
try {
is = new FileInputStream(file); //得到源图片的输入流
byte[] b = new byte[is.available()];//取得他的可用字节大小
is.read(b); //将可用字节数组塞满
BASE64Encoder be = new BASE64Encoder();// 构建加密对象
fileString = be.encode(b);//将字节数组加密为字符串
System.out.println(fileString);
} catch (Exception e) {
e.printStackTrace();
} finally {
is.close();
}
return fileString;//返回字符串
}
2.将字符串保存到本地磁盘上。比如E:/1.txt
3,读取1.txt,并输出一副图片
static void fromfile() throws Exception {
BASE64Decoder bd = new BASE64Decoder();
OutputStream os = null;
InputStream is = null;
File sfile = new File("E:1.txt"); //加密字符串所在的路径
File file = new File("E:/b.png");//解密后的图片路径
try {
is = new FileInputStream(sfile);//将加密字符串所在的文件放到输入流中
byte[] by = new byte[is.available()];//建立足够大的字节数组
is.read(by); //独到字节数组中
byte[] b = bd.decodeBuffer(new String(by));//将这个字节数组再解密到另一个字节数组中
os = new FileOutputStream(file);//将解密后的图片放入输出流中
os.write(b); //将解密后的字节数组写到输出流中
} catch (Exception e) {
e.printStackTrace();
} finally {
os.flush();
os.close();
is.close();
}
}
通过上述步骤,在e盘下可以找到解密后的图片。
jdk的base64据说不太安全,不过自己玩玩足够了,加密一些平时的图片,还可以。
思路是:先将图片加密,得到加密后的字符串,然后保存到本地磁盘的某个txt文件,然后读出这个文件,将其中的内容独到byte数组中,再解密。得到解密后的字节数组,通过流,写到本地磁盘上,得到一副图片。
加密方法:
static String filedc() throws Exception {
InputStream is = null;
File file = new File("E:/a.png");
String fileString = "";
try {
is = new FileInputStream(file); //得到源图片的输入流
byte[] b = new byte[is.available()];//取得他的可用字节大小
is.read(b); //将可用字节数组塞满
BASE64Encoder be = new BASE64Encoder();// 构建加密对象
fileString = be.encode(b);//将字节数组加密为字符串
System.out.println(fileString);
} catch (Exception e) {
e.printStackTrace();
} finally {
is.close();
}
return fileString;//返回字符串
}
2.将字符串保存到本地磁盘上。比如E:/1.txt
3,读取1.txt,并输出一副图片
static void fromfile() throws Exception {
BASE64Decoder bd = new BASE64Decoder();
OutputStream os = null;
InputStream is = null;
File sfile = new File("E:1.txt"); //加密字符串所在的路径
File file = new File("E:/b.png");//解密后的图片路径
try {
is = new FileInputStream(sfile);//将加密字符串所在的文件放到输入流中
byte[] by = new byte[is.available()];//建立足够大的字节数组
is.read(by); //独到字节数组中
byte[] b = bd.decodeBuffer(new String(by));//将这个字节数组再解密到另一个字节数组中
os = new FileOutputStream(file);//将解密后的图片放入输出流中
os.write(b); //将解密后的字节数组写到输出流中
} catch (Exception e) {
e.printStackTrace();
} finally {
os.flush();
os.close();
is.close();
}
}
通过上述步骤,在e盘下可以找到解密后的图片。
jdk的base64据说不太安全,不过自己玩玩足够了,加密一些平时的图片,还可以。
相关文章推荐
- C# DES 加密/解密类库,支持文件和中文/UNICODE字符,返回BASE64编码字符串
- Base64 解密,加密文件或者字符串 [本人加了字符串加解密]
- 读取本地json文件,转出为指定格式json 使用Base64进行string的加密和解密
- Java实现Base64给文件加密、解密
- C# DES 加密/解密类库,支持文件和中文/UNICODE字符,返回BASE64编码字符串
- python基于base64加密解密文件
- base64加密解密文件
- java base64加密文件 及解密到文件
- Java实现Base64给文件加密、解密
- C# DES 加密/解密,支持文件和中文/UNICODE字符,返回BASE64编码字符串
- base64编码和aes加密和解密配置文件
- C# DES 加密/解密,支持文件和中文/UNICODE字符,返回BASE64编码字符串
- java代码实现对文件的base64加密解密
- eval(gzinflate(base64_decode无限加密文件的解密代码
- 使用Base64加密解密文件
- Visual Studio 2017中使用正则修改部分内容 如何使用ILAsm与ILDasm修改.Net exe(dll)文件 C#学习-图解教程(1):格式化数字字符串 小程序开发之图片转Base64(C#、.Net) jquery遍历table为每一个单元格取值及赋值 。net加密解密相关方法 .net关于坐标之间一些简单操作
- Code:关于加密解密 Base64 and URL and Hex Encoding and Decoding
- php实现base64加密解密的代码
- 【加密解密】 文件的加密解密 <含源代码>