您的位置:首页 > 其它

加密算法--总结

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();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  加密 算法