首页 » Web前端 » phpsqlite加密对象类技巧_Java加密Sqlite库 及文件

phpsqlite加密对象类技巧_Java加密Sqlite库 及文件

访客 2024-10-25 0

扫一扫用手机浏览

文章目录 [+]

package com.ts.tools;

import cn.hutool.core.io.FileUtil;

phpsqlite加密对象类技巧_Java加密Sqlite库 及文件

import cn.hutool.core.io.IoUtil;

phpsqlite加密对象类技巧_Java加密Sqlite库 及文件
(图片来自网络侵删)

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=false;

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.close();

//删除源文件,把稳如果文件被利用会报:另一个程序正在利用此文件,进程无法访问

boolean isDel=FileUtil.del(filePath);

if(isDel){

//重命名加密文件为源文件

FileUtil.rename(new File(newFilePath),fileName,true);

//设置只读文件

if(isReadOnly) {

File f=new File(filePath);

f.setReadOnly();

}

}

bFlag=true;

}else{

throw new RuntimeException(filePath+" 文件不存在!");

}

} catch (Exception e) {

throw new RuntimeException(e);

} finally {

IoUtil.close(fos);

}

return bFlag;

}

/

解密Db库

@param filePath 文件路径

@param key16 加密密码(必须16位)

@return boolean

@throws Exception

/

public static boolean decryptFile(String filePath,String key16){

boolean bFlag=false;

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 并未关闭,请先关闭

fos.close();

//删除源文件,把稳如果文件被利用会报:另一个程序正在利用此文件,进程无法访问

boolean isDel=FileUtil.del(filePath);

if(isDel){

//重命名加密文件为源文件

FileUtil.rename(new File(newFilePath),fileName,true);

}

bFlag=true;

}else{

throw new RuntimeException(filePath+" 文件不存在!");

}

} catch (FileNotFoundException e) {

throw new RuntimeException(filePath+" 文件不存在!");

}catch (Exception e){

throw new RuntimeException(e);

}finally {

IoUtil.close(fos);

}

return bFlag;

}

/

文件是否占用(采取重命名的办法)

@param filePath 文件路径

@return boolean

/

public static boolean isLocked(String filePath){

boolean bFlag=false;

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;

}

}

相关文章

房山第一探寻历史文化名区的魅力与发展

房山区,位于北京市西南部,历史悠久,文化底蕴深厚。作为北京市的一个重要组成部分,房山区的发展始终与首都的发展紧密相连。房山区积极推...

Web前端 2025-02-18 阅读1 评论0

手机话费开钻代码数字时代的便捷生活

我们的生活越来越离不开手机。手机话费作为手机使用过程中的重要组成部分,其充值方式也在不断创新。手机话费开钻代码应运而生,为用户提供...

Web前端 2025-02-18 阅读1 评论0

探寻专业奥秘如何查询自己专业的代码

计算机科学已成为当今社会不可或缺的一部分。掌握一门专业代码对于个人发展具有重要意义。面对繁杂的学科体系,如何查询自己专业的代码成为...

Web前端 2025-02-18 阅读0 评论0