JAVA 实现磁盘文件加解密操作的示例代码
简单实现了下:
importjavax.crypto.BadPaddingException;
importjavax.crypto.Cipher;
importjavax.crypto.KeyGenerator;
importjavax.crypto.SecretKey;
importjava.io.*;
importjava.security.GeneralSecurityException;
importjava.security.SecureRandom;
/**
*文件/目录加解密相关
*@author:Nemo
*@date:2019/3/19.
*/
publicclassFileCrote{
/**
*加密后的文件后缀
*/
privatestaticfinalStringSUBFIX=".enc";
/**
*执行路径
*/
privatestaticStringpath="";
/**
*执行模式1加密2解密
*/
privatestaticStringmode="1";
/**
*执行密码
*/
privatestaticStringpass="";
privatestaticStringcurrentFilePath=null;
/**
*根据路径加密文件
*1、如果是目录,则找它下面的文件进行加密
*2、如果是文件,则直接加密
*@parampath
*@throwsIOException
*/
privatestaticvoidencrypt(Stringpath)throwsIOException,GeneralSecurityException{
System.out.println("开始处理"+path);
Filefile=newFile(path);
if(!file.exists()){
System.out.println("需加密的路径不存在:"+path);
return;
}
if(file.isDirectory()){
//目录则遍历其下的文件
File[]files=file.listFiles();
if(files==null){
return;
}
for(FilesubFile:files){
encrypt(subFile.getAbsolutePath());
}
}else{
//文件则直接加密
encrypt(file);
}
}
/**
*文件加密
*@paramfile
*@throwsIOException
*/
privatestaticvoidencrypt(Filefile)throwsIOException,GeneralSecurityException{
Stringpath=file.getAbsolutePath();
if(path.endsWith(SUBFIX)){
System.out.println("已加密文件不需再次加密"+path);
return;
}
StringencFilePath=path+SUBFIX;
FileencFile=newFile(encFilePath);
System.out.println("开始加密文件"+path);
encryptFile(file,encFile,Cipher.ENCRYPT_MODE);
}
/**
*加解密文件操作
*@paramsrcFile
*@paramencFile
*@throwsIOException
*/
privatestaticvoidencryptFile(FilesrcFile,FileencFile,intmode)throwsIOException,GeneralSecurityException{
if(!srcFile.exists()){
System.out.println("sourcefilenotexixt");
return;
}
currentFilePath=srcFile.getAbsolutePath();
//密码
Ciphercipher=getCipher(mode);
FileInputStreamfis=null;
FileOutputStreamfos=null;
try{
fis=newFileInputStream(srcFile);
fos=newFileOutputStream(encFile);
//真正处理
crypt(fis,fos,cipher);
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(fis!=null){
fis.close();
}
if(fos!=null){
fos.close();
}
}
//源文件删除
srcFile.delete();
}
/**
*得到加解密Cipher
*@parammode
*@return
*@throwsGeneralSecurityException
*/
privatestaticCiphergetCipher(intmode)throwsGeneralSecurityException{
Stringtype="AES";
Ciphercipher=Cipher.getInstance(type+"/ECB/PKCS5Padding");
KeyGeneratorkgen=KeyGenerator.getInstance(type);
kgen.init(128,newSecureRandom(pass.getBytes()));
SecretKeykey=kgen.generateKey();
cipher.init(mode,key);
returncipher;
}
/**
*加密解密流
*@paramin加密解密前的流
*@paramout加密解密后的流
*@paramcipher加密解密
*@throwsIOException
*@throwsGeneralSecurityException
*/
privatestaticvoidcrypt(InputStreamin,OutputStreamout,Ciphercipher)throwsIOException,GeneralSecurityException{
intblockSize=cipher.getBlockSize()*1000;
intoutputSize=cipher.getOutputSize(blockSize);
byte[]inBytes=newbyte[blockSize];
byte[]outBytes=newbyte[outputSize];
intinLength=0;
booleanmore=true;
while(more){
inLength=in.read(inBytes);
if(inLength==blockSize){
intoutLength=cipher.update(inBytes,0,blockSize,outBytes);
out.write(outBytes,0,outLength);
}else{
more=false;
}
}
if(inLength>0){
outBytes=cipher.doFinal(inBytes,0,inLength);
}else{
outBytes=cipher.doFinal();
}
out.write(outBytes);
}
/**
*根据路径解密
*1、如果是目录,则找它下面的加密文件进行解密
*2、如果是文件,并且它是加密文件,则直接解密
*@parampath
*@throwsIOException
*/
privatestaticvoiddecrypt(Stringpath)throwsIOException,GeneralSecurityException{
System.out.println("开始处理"+path);
Filefile=newFile(path);
if(!file.exists()){
System.out.println("需解密的路径不存在:"+path);
return;
}
if(file.isDirectory()){
//目录则遍历其下的文件
File[]files=file.listFiles();
if(files==null){
return;
}
for(FilesubFile:files){
decrypt(subFile.getAbsolutePath());
}
}else{
decrypt(file);
}
}
/**
*文件解密
*@paramfile
*@throwsIOException
*/
privatestaticvoiddecrypt(Filefile)throwsIOException,GeneralSecurityException{
Stringpath=file.getAbsolutePath();
if(!path.endsWith(SUBFIX)){
System.out.println("非加密文件不需解密"+path);
return;
}
System.out.println("开始解密文件"+path);
StringnewPath=path.substring(0,path.length()-SUBFIX.length());
encryptFile(file,newFile(newPath),Cipher.DECRYPT_MODE);
}
/**
*字符串是否非空
*@params
*@return
*/
privatestaticbooleanisNotEmpty(Strings){
if(s==null||"".equals(s)){
returnfalse;
}
returntrue;
}
/**
*开始处理
*@throwsIOException
*@throwsGeneralSecurityException
*/
privatestaticvoiddeal()throwsIOException,GeneralSecurityException{
while(true){
print();
BufferedReaderreader=newBufferedReader(newInputStreamReader(System.in));
Strings=reader.readLine();
if("path".equals(s)){
System.out.println("\r\n请输入执行路径\r\n");
while(true){
reader=newBufferedReader(newInputStreamReader(System.in));
s=reader.readLine();
if(!isNotEmpty(s)){
System.out.println("\r\n请输入执行路径\r\n");
continue;
}else{
Filefile=newFile(s);
if(!file.exists()){
System.out.println("\r\n该路径不存在,请重新输入\r\n");
continue;
}
}
path=s;
break;
}
}elseif("pass".equals(s)){
System.out.println("\r\n请输入加/解密密码\r\n");
while(true){
reader=newBufferedReader(newInputStreamReader(System.in));
s=reader.readLine();
if(!isNotEmpty(s)){
System.out.println("\r\n请输入加/解密密码\r\n");
continue;
}
pass=s;
break;
}
}elseif("1".equals(s)){
mode=s;
System.out.println("\r\n当前已选模式:加密\n");
}elseif("2".equals(s)){
mode=s;
System.out.println("\r\n当前已选模式:解密\r\n");
}elseif("start".equals(s)){
if(!isNotEmpty(path)){
System.out.println("\r\n请输入加/解密密码再开始\r\n");
continue;
}
if(!isNotEmpty(pass)){
System.out.println("\r\n请输入执行路径后再开始\r\n");
continue;
}
if("1".equals(mode)){
encrypt(path);
System.out.println("\r\n\r\n操作完成\r\n\r\n");
}else{
try{
decrypt(path);
System.out.println("\r\n\r\n操作完成\r\n\r\n");
}catch(BadPaddingExceptione){
System.out.println("文件:"+currentFilePath+"解密失败,密码错误");
}
}
}elseif("quit".equals(s)){
System.exit(-1);
}else{
System.out.println("\r\n输入错误\r\n");
}
}
}
/**
*输出提示语
*/
privatestaticvoidprint(){
System.out.println("\r\n"+
"输入path开始选择执行路径\r\n"+
"输入pass开始选择加/解密密码\r\n"+
"输入如下数字以选择处理模式:1加密2解密\r\n"+
"输入start则开始执行已选操作\r\n"+
"输入quit则退出程序\r\n"+
"当前选择路径:"+path+"\r\n"+
"当前选择模式:"+mode+"\r\n"+
"当前选择密码:"+pass+"\r\n");
}
publicstaticvoidmain(Stringargs[])throwsIOException,GeneralSecurityException{
deal();
}
}
以上就是JAVA实现磁盘文件加解密操作的示例代码的详细内容,更多关于Java文件加解密的资料请关注毛票票其它相关文章!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。