原因:最近在用Sqlite存儲數(shù)據(jù),因涉及數(shù)據(jù)安全,所以需要數(shù)據(jù)庫加密,Sqlite庫默認(rèn)不帶加密功能 目前已知的對 SQLite 加密的工具主要有「[SQLite Encryption Extension (SEE)]、[SQLiteEncrypt]、[SQLiteCrypt]、[SQLCipher],但是這里面僅有 SQLCipher 有免費版本。 所以自己有個方案,針對Sqlite無法就是限制不能直接查看,那就對文件進(jìn)行加密,需要連接的時候進(jìn)行文件解密就可以了,如果大神有更好的解決方案,請告知,謝謝!
package com.ts.tools;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import java.io.*;
/** 加密解密文件
* @author xhc
* @version 1.0.0
* @date 2022-10-31
*/
public class FileEnDe {
public static void main(String[] args) {
try {
//加密密碼
String key="abcdefghijklmnop";
//FileEnDe.encryptFile("D:/test.db",key,true);
//FileEnDe.decryptFile("D:/test.db",key);
System.out.println(FileEnDe.isLocked("D:/sysDb.db"));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 加密db文件(理論可以加密任何文件)
* @param filePath 文件路徑
* @param key16 加密密碼(必須16位)
* @return boolean
* @throws Exception
*/
public static boolean encryptFile(String filePath,String key16,boolean isReadOnly){
FileOutputStream fos =null;
boolean bFlag=fal;
try {
if(FileUtil.exist(filePath)) {
byte[] key = key16.getBytes();
//取文件名稱含后綴
String fileName=FileUtil.getName(filePath);
//加密文件
SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);
FileInputStream fis = new FileInputStream(filePath);
String newFilePath=filePath+"_e";
fos=new FileOutputStream(newFilePath);
aes.encrypt(fis, fos, true);
fos.clo();
//刪除源文件,注意如果文件被使用會報:另一個程序正在使用此文件,進(jìn)程無法訪問
boolean isDel=FileUtil.del(filePath);
if(isDel){
//重命名加密文件為源文件
FileUtil.rename(new File(newFilePath),fileName,true);
//設(shè)置只讀文件
if(isReadOnly) {
File f=new File(filePath);
f.tReadOnly();
}
}
bFlag=true;
}el{
throw new RuntimeException(filePath+" 文件不存在!");
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
IoUtil.clo(fos);
}
return bFlag;
}
/**
* 解密Db庫
* @param filePath 文件路徑
* @param key16 加密密碼(必須16位)
* @return boolean
* @throws Exception
*/
public static boolean decryptFile(String filePath,String key16){
boolean bFlag=fal;
FileOutputStream fos =null;
try {
if(FileUtil.exist(filePath)) {
byte[] key = key16.getBytes();
//取文件名稱含后綴
String fileName=FileUtil.getName(filePath);
//解密文件
SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);
FileInputStream fis = new FileInputStream(filePath);
String newFilePath=filePath+"_d";
fos = new FileOutputStream(newFilePath);
aes.decrypt(fis, fos, true);
//注意,解密之后 fos 并未關(guān)閉,請先關(guān)閉
fos.clo();
//刪除源文件,注意如果文件被使用會報:另一個程序正在使用此文件,進(jìn)程無法訪問
boolean isDel=FileUtil.del(filePath);
if(isDel){
//重命名加密文件為源文件
FileUtil.rename(new File(newFilePath),fileName,true);
}
bFlag=true;
}el{
throw new RuntimeException(filePath+" 文件不存在!");
}
} catch (FileNotFoundException e) {
throw new RuntimeException(filePath+" 文件不存在!");
}catch (Exception e){
throw new RuntimeException(e);
}finally {
IoUtil.clo(fos);
}
return bFlag;
}
/**
* 文件是否占用(采用重命名的方式)
* @param filePath 文件路徑
* @return boolean
*/
public static boolean isLocked(String filePath){
boolean bFlag=fal;
String newFileName="";
String fileName="";
try {
if(FileUtil.exist(filePath)) {
// 采用重命名的方式,如果占用無法修改文件名稱,如果未占用修改新的文件名稱后再修改回來
fileName = FileUtil.getName(filePath);
newFileName = fileName + "_n";
FileUtil.rename(new File(filePath), newFileName, true);
}
} catch (Exception e) {
bFlag=true;
}finally {
//如果未占用修改新的文件名稱后再修改回來
if(!bFlag) {
FileUtil.rename(new File(filePath+"_n"), fileName, true);
}
}
return bFlag;
}
}
本文發(fā)布于:2023-02-28 20:13:00,感謝您對本站的認(rèn)可!
本文鏈接:http://m.newhan.cn/zhishi/a/167766281178822.html
版權(quán)聲明:本站內(nèi)容均來自互聯(lián)網(wǎng),僅供演示用,請勿用于商業(yè)和其他非法用途。如果侵犯了您的權(quán)益請與我們聯(lián)系,我們將在24小時內(nèi)刪除。
本文word下載地址:另一個程序正在使用此文件(另一個程序正在使用此文件怎么辦).doc
本文 PDF 下載地址:另一個程序正在使用此文件(另一個程序正在使用此文件怎么辦).pdf
| 留言與評論(共有 0 條評論) |