日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java实现文件加密与解密_Java实现文件的加密解密功能示例

發(fā)布時間:2023/12/15 java 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java实现文件加密与解密_Java实现文件的加密解密功能示例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本文實例講述了Java實現(xiàn)文件的加密解密功能分享給大家供大家參考,具體如下:

package com.copy.encrypt;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.io.RandomAccessFile;

public class FileEncryptAndDecrypt {

/**

* 文件file進行加密

* @param fileUrl 文件路徑

* @param key 密碼

* @throws Exception

*/

public static void encrypt(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

{

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");

// 文件長度,字節(jié)數(shù)

long fileLength = randomFile.length();

//將寫文件指針移到文件尾。

randomFile.seek(fileLength);

randomFile.writeBytes(content);

randomFile.close();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 解密

* @param fileUrl 源文件

* @param tempUrl 臨時文件

* @param ketLength 密碼長度

* @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");

// 文件長度,字節(jié)數(shù)

long fileLength = randomFile.length();

//將寫文件指針移到文件尾。

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;

}

}

PS:關(guān)于加密解密感興趣的朋友還可以參考本站在線工具:

在線MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:http://tools.jb51.net/password/hash_md5_sha

希望本文所述對大家java程序設計有所幫助。

總結(jié)

以上是生活随笔為你收集整理的java实现文件加密与解密_Java实现文件的加密解密功能示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。