Java实现文件的加密解密功能示例

网友投稿 385 2023-03-27


Java实现文件的加密解密功能示例

本文实例讲述了java实现文件的加密解密功能分享给大家供大家参考,具体如下:

package com.copy.encrypt;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundExceptihttp://on;

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

// 文件长度,字节数

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

// 文件长度,字节数

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:关于加密解密感兴趣的朋友还可以参考本站在线工具:

文字在线加密解密工具(包含AES、DES、RC4等):

http://tools.jb51.net/password/txt_encode

MD5在线加密工具:

http://tools.jb51.net/password/CreateMD5Password

在线散列/哈希算法加密工具:

http://tools.jb51.net/password/hash_encrypt

在线MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:

http://tools.jb51.net/password/hash_md5_sha

在线sha1/sha224/sha256/sha384/sha512加密工具:

http://tools.jb51.net/password/sha_encode

更多关于java相关内容感兴趣的读者可查看本站专题:《Java数学运算技巧总结》、《Java数据结构与算法教程》、《Java字符与字符串操作技巧总结》、《java日期与时间操作技巧汇总》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总》

希望本文所述对大家java程序设计有所帮助。

{

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

// 文件长度,字节数

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:关于加密解密感兴趣的朋友还可以参考本站在线工具:

文字在线加密解密工具(包含AES、DES、RC4等):

http://tools.jb51.net/password/txt_encode

MD5在线加密工具:

http://tools.jb51.net/password/CreateMD5Password

在线散列/哈希算法加密工具:

http://tools.jb51.net/password/hash_encrypt

在线MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:

http://tools.jb51.net/password/hash_md5_sha

在线sha1/sha224/sha256/sha384/sha512加密工具:

http://tools.jb51.net/password/sha_encode

更多关于java相关内容感兴趣的读者可查看本站专题:《Java数学运算技巧总结》、《Java数据结构与算法教程》、《Java字符与字符串操作技巧总结》、《java日期与时间操作技巧汇总》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总》

希望本文所述对大家java程序设计有所帮助。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:详谈jvm
下一篇:React Native中的RefreshContorl下拉刷新使用
相关文章

 发表评论

暂时没有评论,来抢沙发吧~