加密算法--总结
2016-02-03 18:07
357 查看
最近在给文件做加密时,遇到一些问题,主要是在加密过程中采用加密输入流遇到的问题,总结成代码如下:
package utils; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.ArrayUtils; public class Test { protected static MessageDigest messagedigest = null; static { try { messagedigest = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } } /** * ByteArrayOutputStream版本 * 存在问题:当加密文件比较大时会出现内存溢出的问题 * @param file * @return */ public static String getFileMD5WithByteArrayOutputStream(File file) { FileInputStream in = null; ByteArrayOutputStream out = null; try { in = new FileInputStream(file); out = new ByteArrayOutputStream((int)file.length()); byte[] cache = new byte[1024]; for(int i = in.read(cache);i != -1;i = in.read(cache)){ out.write(cache, 0, i); } messagedigest.update(out.toByteArray()); return changeBase64(messagedigest.digest()); } catch (IOException e) { e.printStackTrace(); } finally{ try { in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } /** * MappedByteBuffer版本 * 存在问题:当文件加密之后,如果删除会出现文件被占用无法删除的错误 * @param file * @return */ public static String getFileMD5WithMappedByteBuffer(File file) { FileInputStream in = null; try { in = new FileInputStream(file); FileChannel ch = in.getChannel(); MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length()); messagedigest.update(byteBuffer); return changeBase64(messagedigest.digest()); } catch (IOException e) { e.printStackTrace(); } finally{ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } /** * byteBuffer版本 * @param file * @return */ public static String getMD5WithbyteBuffer(File file ){ FileInputStream fis= null; try{ fis= new FileInputStream(file); FileChannel fci= fis.getChannel(); ByteBuffer block= ByteBuffer.allocate(1024); while(fci.position() < fci.size()){ int readLen= fci.read(block); block.flip(); messagedigest.update(ArrayUtils.subarray(block.array(), 0, readLen)); block.clear(); } return changeBase64(messagedigest.digest()); }catch(IOException e){ e.printStackTrace(); } finally{ IOUtils.closeQuietly(fis); } return null; } public static void main(String[] args) { String filePath = "F:\\迅雷下载\\[电影天堂www.dygod.cn].回到未来.[中英双字.1024分辨率].rmvb"; long fileBegin1 = System.currentTimeMillis(); System.out.println("文件加密结果为:"+getMD5WithbyteBuffer(new File(filePath))); long fileEnd1 = System.currentTimeMillis(); System.out.println("文件加密耗时:" + ((fileEnd1 - fileBegin1) / 1000) + "s"); long fileBegin2 = System.currentTimeMillis(); System.out.println("文件加密结果为:"+getFileMD5WithMappedByteBuffer(new File(filePath))); long fileEnd2 = System.currentTimeMillis(); System.out.println("文件加密耗时:" + ((fileEnd2 - fileBegin2) / 1000) + "s"); long fileBegin3 = System.currentTimeMillis(); System.out.println("文件加密结果为:"+getFileMD5WithByteArrayOutputStream(new File(filePath))); long fileEnd3 = System.currentTimeMillis(); System.out.println("文件加密耗时:" + ((fileEnd3 - fileBegin3) / 1000) + "s"); } //------------------------------加密运算---------------------------------- protected static final byte[] encodingTable = { (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G', (byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L', (byte)'M', (byte)'N', (byte)'O', (byte)'P', (byte)'Q', (byte)'R', (byte)'S', (byte)'T', (byte)'U', (byte)'V', (byte)'W', (byte)'X', (byte)'Y', (byte)'Z', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f', (byte)'g', (byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', (byte)'m', (byte)'n', (byte)'o', (byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t', (byte)'u', (byte)'v', (byte)'w', (byte)'x', (byte)'y', (byte)'z', (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7', (byte)'8', (byte)'9', (byte)'+', (byte)'/' }; /** * copy base64的算法 * @param data * @return */ public static String changeBase64(byte[] data){ int length = data.length; int modulus = length % 3; int dataLength = (length - modulus); int a1, a2, a3; StringBuffer buffer = new StringBuffer(); for (int i = 0; i < dataLength; i += 3) { a1 = data[i] & 0xff; a2 = data[i + 1] & 0xff; a3 = data[i + 2] & 0xff; buffer.append(encodingTable[(a1 >>> 2) & 0x3f]); buffer.append(encodingTable[((a1 << 4) | (a2 >>> 4)) & 0x3f]); buffer.append(encodingTable[((a2 << 2) | (a3 >>> 6)) & 0x3f]); buffer.append(encodingTable[a3 & 0x3f]); } return buffer.toString(); } }
相关文章推荐
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- 用ASP编写的加密和解密类
- Ruby实现的矩阵连乘算法
- VBS脚本加密/解密VBS脚本(简易免杀版1.1)
- C#插入法排序算法实例分析
- BAT加密工具 EncryBat 非编译型bat批处理加密方案与代码
- C#对称加密(AES加密)每次生成的结果都不同的实现思路和代码实例
- SQLServer 2008中的代码安全(一) 存储过程加密与安全上下文
- 实例讲解SQL Server加密功能
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#实现对文件进行加密解密的方法
- C#实现数据包加密与解密实例详解
- C#冒泡法排序算法实例分析
- C#最简单的字符串加密解密方法
- 算法练习之从String.indexOf的模拟实现开始
- C#加密app.config中连接字符串的方法
- C#算法之关于大牛生小牛的问题
- C#使用伪随机数实现加密用户密码的方法