BASE64相关资料

相关网站 : 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;

}

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,514评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,958评论 18 399
  • /**ios常见的几种加密方法: 普通的加密方法是讲密码进行加密后保存到用户偏好设置( [NSUserDefaul...
    彬至睢阳阅读 8,190评论 0 7
  • 这几天我经常去航校,我们的第二球馆就坐落在那里。虽然保定不大,但是要找到那里还是费一番周折的。2011年我们选...
    子不雨阅读 864评论 0 1
  • 最近不知道怎么了,经常能听到“套路”一词。 套路,本意是指编制成套的武术动作,是武术运动的一种形式。当下特制...
    洛洛一阅读 6,457评论 0 1