Java加密Sqlite库及文件
原因:最近在用Sqlite存储数据,因涉及数据安全,所以需要数据库加密,Sqlite库默认不带加密功能目前已知的对SQLite加密的工具主要有〔SQLiteEncryptionExtension(SEE)〕、〔SQLiteEncrypt〕、〔SQLiteCrypt〕、〔SQLCipher〕,但是这里面仅有SQLCipher有免费版本。所以自己有个方案,针对Sqlite无法就是限制不能直接查看,那就对文件进行加密,需要连接的时候进行文件解密就可以了,如果大神有更好的解决方案,请告知,谢谢!
packagecom。ts。tools;
importcn。hutool。core。io。FileUtil;
importcn。hutool。core。io。IoUtil;
importcn。hutool。crypto。symmetric。SymmetricAlgorithm;
importcn。hutool。crypto。symmetric。SymmetricCrypto;
importjava。io。;
加密解密文件
authorxhc
version1。0。0
date20221031
publicclassFileEnDe{
publicstaticvoidmain(String〔〕args){
try{
加密密码
Stringkeyabcdefghijklmnop;
FileEnDe。encryptFile(D:test。db,key,true);
FileEnDe。decryptFile(D:test。db,key);
System。out。println(FileEnDe。isLocked(D:sysDb。db));
}catch(Exceptione){
thrownewRuntimeException(e);
}
}
加密db文件(理论可以加密任何文件)
paramfilePath文件路径
paramkey16加密密码(必须16位)
returnboolean
throwsException
publicstaticbooleanencryptFile(StringfilePath,Stringkey16,booleanisReadOnly){
FileOutputStreamfosnull;
booleanbFlagfalse;
try{
if(FileUtil。exist(filePath)){
byte〔〕keykey16。getBytes();
取文件名称含后缀
StringfileNameFileUtil。getName(filePath);
加密文件
SymmetricCryptoaesnewSymmetricCrypto(SymmetricAlgorithm。AES,key);
FileInputStreamfisnewFileInputStream(filePath);
StringnewFilePathfilePathe;
fosnewFileOutputStream(newFilePath);
aes。encrypt(fis,fos,true);
fos。close();
删除源文件,注意如果文件被使用会报:另一个程序正在使用此文件,进程无法访问
booleanisDelFileUtil。del(filePath);
if(isDel){
重命名加密文件为源文件
FileUtil。rename(newFile(newFilePath),fileName,true);
设置只读文件
if(isReadOnly){
FilefnewFile(filePath);
f。setReadOnly();
}
}
bFlagtrue;
}else{
thrownewRuntimeException(filePath文件不存在!);
}
}catch(Exceptione){
thrownewRuntimeException(e);
}finally{
IoUtil。close(fos);
}
returnbFlag;
}
解密Db库
paramfilePath文件路径
paramkey16加密密码(必须16位)
returnboolean
throwsException
publicstaticbooleandecryptFile(StringfilePath,Stringkey16){
booleanbFlagfalse;
FileOutputStreamfosnull;
try{
if(FileUtil。exist(filePath)){
byte〔〕keykey16。getBytes();
取文件名称含后缀
StringfileNameFileUtil。getName(filePath);
解密文件
SymmetricCryptoaesnewSymmetricCrypto(SymmetricAlgorithm。AES,key);
FileInputStreamfisnewFileInputStream(filePath);
StringnewFilePathfilePathd;
fosnewFileOutputStream(newFilePath);
aes。decrypt(fis,fos,true);
注意,解密之后fos并未关闭,请先关闭
fos。close();
删除源文件,注意如果文件被使用会报:另一个程序正在使用此文件,进程无法访问
booleanisDelFileUtil。del(filePath);
if(isDel){
重命名加密文件为源文件
FileUtil。rename(newFile(newFilePath),fileName,true);
}
bFlagtrue;
}else{
thrownewRuntimeException(filePath文件不存在!);
}
}catch(FileNotFoundExceptione){
thrownewRuntimeException(filePath文件不存在!);
}catch(Exceptione){
thrownewRuntimeException(e);
}finally{
IoUtil。close(fos);
}
returnbFlag;
}
文件是否占用(采用重命名的方式)
paramfilePath文件路径
returnboolean
publicstaticbooleanisLocked(StringfilePath){
booleanbFlagfalse;
StringnewFileName;
StringfileName;
try{
if(FileUtil。exist(filePath)){
采用重命名的方式,如果占用无法修改文件名称,如果未占用修改新的文件名称后再修改回来
fileNameFileUtil。getName(filePath);
newFileNamefileNamen;
FileUtil。rename(newFile(filePath),newFileName,true);
}
}catch(Exceptione){
bFlagtrue;
}finally{
如果未占用修改新的文件名称后再修改回来
if(!bFlag){
FileUtil。rename(newFile(filePathn),fileName,true);
}
}
returnbFlag;
}
}