java 对文件加密解密
本帖最后由 Hong_1993 于 -09-03 10:51:35 编辑
我现在想做一个基于springmvc模式的实现对文件或目录文件的加解密(des算法的)
文件是固定的,也就是说自己随便建一个文件就是实现对该文件的加密和解密
求源代码层次要清楚要求有注释,最好详解,拜托了
------解决方案--------------------
1.这个跟mvc没啥关系吧
2.实际应用中,一般是Javajni调用cpp进行文件加解密
3.JavaDES加密小例子:
packagetest;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.InputStream;
importjava.io.OutputStream;
importjava.security.Key;
importjava.security.SecureRandom;
importjavax.crypto.Cipher;
importjavax.crypto.CipherInputStream;
importjavax.crypto.CipherOutputStream;
importjavax.crypto.KeyGenerator;
importsun.misc.BASE64Decoder;
importsun.misc.BASE64Encoder;
publicclassDESUtil{
Keykey;
publicDESUtil(){
}
publicDESUtil(Stringstr){
setKey(str);//生成密匙
}
publicKeygetKey(){
returnkey;
}
publicvoidsetKey(Keykey){
this.key=key;
}
/**
*根据参数生成KEY
*/
publicvoidsetKey(StringstrKey){
try{
KeyGenerator_generator=KeyGenerator.getInstance("DES");
_generator.init(newSecureRandom(strKey.getBytes()));
this.key=_generator.generateKey();
_generator=null;
}catch(Exceptione){
thrownewRuntimeException(
"ErrorinitializingSqlMapclass.Cause:"+e);
}
}
/**
*加密String明文输入,String密文输出
*/
publicStringencryptStr(StringstrMing){
byte[]byteMi=null;
byte[]byteMing=null;
StringstrMi="";
BASE64Encoderbase64en=newBASE64Encoder();
try{
byteMing=strMing.getBytes("UTF8");
byteMi=this.encryptByte(byteMing);
strMi=base64en.encode(byteMi);
}catch(Exceptione){
thrownewRuntimeException(
"ErrorinitializingSqlMapclass.Cause:"+e);
}finally{
base64en=null;
byteMing=null;
byteMi=null;
}
returnstrMi;