美文网首页
BASE64相关资料

BASE64相关资料

作者: 简单Liml | 来源:发表于2017-05-15 09:51 被阅读7次

相关网站 : http://www.cnblogs.com/mofish/archive/2010/11/26/1889126.html

public class AddSec {

public static void main(String[] args){

String addSecStr = getAddSecFileStr("E:\\lml\\原文件.txt"); // 获得加密后字符串

getFile(addSecStr, "E:\\lml\\加密后的文件.txt"); //加密后文件

String fileStr=getFileStr("E:\\lml\\加密后的文件.txt");

releaseFile(fileStr, "E:\\lml\\还原文件.txt"); //解密后的文件

}

/**

* 将文件转化为字节数组字符串,并对其进行Base64编码处理

*

* @param imgFilePath

*            文件路径

* @return

*/

public static String getAddSecFileStr(String imgFilePath) {

byte[] data = null;

// 读取文件字节数组

try {

InputStream in = new FileInputStream(imgFilePath);

data = new byte[in.available()];

in.read(data);

in.close();

} catch (IOException e) {

e.printStackTrace();

}

byte[] bytes = UrlBase64.encode(data);

return new String(bytes);

}

/**

* 将文件转化为字节数组字符串

*

* @param imgFilePath

*            文件路径

* @return

*/

public static String getFileStr(String imgFilePath) {

byte[] data = null;

// 读取字节数组

try {

InputStream in = new FileInputStream(imgFilePath);

data = new byte[in.available()];

in.read(data);

in.close();

} catch (IOException e) {

e.printStackTrace();

}

return new String(data);

}

/**

* 对字节数组字符串进行Base64解码并生成文件

*

* @param imgStr

*            加密的字符串

* @param imgFilePath

*            解密后的文件路径

* @return

*/

public static boolean releaseFile(String imgStr, String imgFilePath) {

if (imgStr == null) // 文件数据为空

return false;

try {

// Base64解码

byte[] bytes = UrlBase64.decode(imgStr.getBytes());

for (int i = 0; i < bytes.length; ++i) {

if (bytes[i] < 0) {// 调整异常数据

bytes[i] += 256;

}

}

OutputStream out = new FileOutputStream(imgFilePath);

out.write(bytes);

out.flush();

out.close();

return true;

} catch (Exception e) {

return false;

}

}

/**

* 根据字节数组字符串生成文件

*

* @param imgStr

*            加密后的文件字符串

* @param imgFilePath

*            加密后的文件路径

* @return

*/

public static boolean getFile(String imgStr, String imgFilePath) {

if (imgStr == null) // 文件数据为空

return false;

try {

byte[] bytes = imgStr.getBytes();

for (int i = 0; i < bytes.length; ++i) {

if (bytes[i] < 0) {// 调整异常数据

bytes[i] += 256;

}

}

// 生成文件

OutputStream out = new FileOutputStream(imgFilePath);

out.write(bytes);

out.flush();

out.close();

return true;

} catch (Exception e) {

return false;

}

}

}

相关文章

  • BASE64相关资料

    相关网站 : http://www.cnblogs.com/mofish/archive/2010/11/26/1...

  • 一步到位 Base64 编码

    相关文章:Base64 之 JavaScript 实现在Web前端还可以这样实现Base64 什么是Base64 ...

  • iOS开发-数据加密

    一、Base64 二、加密相关

  • Base64

    前几天,在处理IOS-SDK,遇到了历史遗留关于base64相关的问题,于是翻查资料,并总结如下 US-ASCII...

  • 在Web前端还可以这样实现Base64

    相关文章:一步到位 Base64 编码Base64 之 JavaScript 实现 上面的相关文章中有一篇Base...

  • 相关资料

    NLP seq2seq Recurrent neural network based language model...

  • 相关资料

    操作系统

  • 相关资料

    关于NOIPNOIP(National Olympiad in Informatics in Provinces)...

  • 相关资料

    https://baike.baidu.com/item/%E5%81%8F%E8%88%AA%E8%A7%92上...

  • Base64加密解密

    关于base64编码Encode和Decode编码的几种方式 Base64是一种能将任意Binary资料用64种字...

网友评论

      本文标题:BASE64相关资料

      本文链接:https://www.haomeiwen.com/subject/wrncxxtx.html