java 文件aes加密_java编程一个AES加密txt文件的程序,其中AES解密文件的方法出错,求大神搭救...

//其中AES的加密解密方法encrypt(),decrypt()是正确的!而运行测试到文件加密方法FileEn()可以运行但不知道是不是AES的加密而文件解密方法FileDe()是出错的。不知道这个和文件的格式有...

//其中AES的加密解密方法encrypt(),decrypt()是正确的!

而运行测试到文件加密方法FileEn()可以运行但不知道是不是AES的加密

而文件解密方法FileDe()是出错的。

不知道这个和文件的格式有什么样的关系了?

/* ************************************文件加密方法 *********************************** */

public void FileEn(String fileReadPath,String fileWritePath,String password,int length){

File fileRead=new File(fileReadPath);

File fileWrite=new File(fileWritePath);

if(!fileRead.exists())

{

System.out.println("文件不存在");

System.exit(0);

}

if(!fileWrite.exists()){

System.out.println("创建文件");

try {

fileWrite.createNewFile();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

try {

FileInputStream fins=new FileInputStream(fileRead);

FileOutputStream fous=new FileOutputStream(fileWrite);

byte[] data=new byte[1024];

byte[] newData=new byte[1024];

while((fins.read(data))!=-1){

newData=encrypt(new String(data), password, length);

// **** encrypt(String content,String password,int length) 其中content为待加密内容 ,password加密密钥,length为密钥长度

// **** encrypt方法是将conttent用AES加密算法加密

fous.write(newData);

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/* *************************************文件解密方法******************************************* */

public void FileDe(String FileReadPath,String FileWritePath,String password,int length){

File FileRead=new File(FileReadPath);

File FileWrite=new File(FileWritePath);

if(!FileRead.exists())

{

System.out.println("文件不存在");

System.exit(0);

}

if(!FileWrite.exists()){

System.out.println("创建文件");

try {

FileWrite.createNewFile();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

try {

FileInputStream fins=new FileInputStream(FileRead);

FileOutputStream fous=new FileOutputStream(FileWrite);

byte[] data=new byte[1024];

byte[] newData=new byte[1024];

while((fins.read(data))!=-1){

newData=decrvpt(data, password, length);

// decrvpt(dyte[] content,String password,int length) 其中content为解密内容 ,password解密密钥,length为密钥长度

// encrypt()方法是将content解密为明文,用的是AES算法

fous.write(newData);

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值