Java对文件进行加密解密操作
生活随笔
收集整理的這篇文章主要介紹了
Java对文件进行加密解密操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
package com.siheal.wahson.task;import java.io.*;public class QuartzJobTest {/*** 秘鑰(可以自己定義,也可隨機生成)*/private static String key = "123456";/*** 文件file進行加密** @param url* @param fileUrl 文件路徑* @param key 密碼* @throws Exception*/public static void encrypt(String url, String fileUrl, String key) throws Exception {File file = new File(fileUrl);String path = file.getPath();if(!file.exists()){return;}int index = path.lastIndexOf("\\");String destFile = path.substring(0, index)+"\\"+"abc";File dest = new File(destFile);InputStream in = new FileInputStream(fileUrl);OutputStream out = new FileOutputStream(destFile);byte[] buffer = new byte[1024];int r;byte[] buffer2=new byte[1024];while (( r= in.read(buffer)) > 0) {for(int i=0;i<r;i++){byte b=buffer[i];buffer2[i]=b==255?0:++b;}out.write(buffer2, 0, r);out.flush();}in.close();out.close();file.delete();dest.renameTo(new File(fileUrl));appendMethodA(fileUrl, key);System.out.println("加密成功");}/**** @param fileName* @param content 密鑰*/public static void appendMethodA(String fileName, String content) {try {// 打開一個隨機訪問文件流,按讀寫方式RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");// 文件長度,字節數long fileLength = randomFile.length();//將寫文件指針移到文件尾。randomFile.seek(fileLength);randomFile.writeBytes(content);randomFile.close();} catch (IOException e) {e.printStackTrace();}}/*** 解密* @param fileUrl 源文件* @param tempUrl 臨時文件* @param keyLength 秘鑰長度* @return* @throws Exception*/public static String decrypt(String fileUrl, String tempUrl, int keyLength) throws Exception{File file = new File(fileUrl);if (!file.exists()) {return null;}File dest = new File(tempUrl);if (!dest.getParentFile().exists()) {dest.getParentFile().mkdirs();}InputStream is = new FileInputStream(fileUrl);OutputStream out = new FileOutputStream(tempUrl);byte[] buffer = new byte[1024];byte[] buffer2=new byte[1024];byte bMax=(byte)255;long size = file.length() - keyLength;int mod = (int) (size%1024);int div = (int) (size>>10);int count = mod==0?div:(div+1);int k = 1, r;while ((k <= count && ( r = is.read(buffer)) > 0)) {if(mod != 0 && k==count) {r = mod;}for(int i = 0;i < r;i++){byte b=buffer[i];buffer2[i]=b==0?bMax:--b;}out.write(buffer2, 0, r);k++;}out.close();is.close();return tempUrl;}/*** 判斷文件是否加密* @param fileName* @return*/public static String readFileLastByte(String fileName, int keyLength) {File file = new File(fileName);if(!file.exists())return null;StringBuffer str = new StringBuffer();try {// 打開一個隨機訪問文件流,按讀寫方式RandomAccessFile randomFile = new RandomAccessFile(fileName, "r");// 文件長度,字節數long fileLength = randomFile.length();if(fileLength < keyLength){return null;}//將寫文件指針移到文件尾。for(int i = keyLength ; i>=1 ; i--){randomFile.seek(fileLength-i);str.append((char)randomFile.read());}randomFile.close();return str.toString();} catch (IOException e) {e.printStackTrace();}return null;}public static void main(String[] args) throws Exception {encrypt("C:\\Users\\Documents\\WXWork\\1688856115382798\\Cache\\File\\2022-10\\12.09.txt", "C:\\Users\\Documents\\WXWork\\1688856115382798\\Cache\\File\\2022-10\\12.09.txt",key); //加密
// if(key.equals(readFileLastByte("C:\\Users\\Documents\\WXWork\\1688856115382798\\Cache\\File\\2022-10\\12.10.txt",key.length()))){
// decrypt("C:\\Users\\Documents\\WXWork\\1688856115382798\\Cache\\File\\2022-10\\12.10.txt","C:\\Users\\Documents\\WXWork\\1688856115382798\\Cache\\File\\2022-10\\12.23.txt",key.length());
// }//解密}
}
總結
以上是生活随笔為你收集整理的Java对文件进行加密解密操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于学Linux的时候安装vmtools
- 下一篇: 不在以下合法域名列表中,请参考文档:ht