DESede解密 运行的时候要与上篇文章加密一起执行

package DES;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

public class DES_Decrypt {

private String getPath = null;
private SecretKey secretkey = null;
private byte [] encryptStringByte;
/*
* 传入要解密的byte [] ,保存了密钥的文件路径
*/
public byte [] decrypt(byte [] encryptStringByte, String getPath) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, IOException, ClassNotFoundException {
panDuanPath(new File(getPath));
getKeyGenerator();
this.encryptStringByte = encryptStringByte;
return decrypt();
}
/*
* 传入要解密的ArrayList<byte []> ,保存了密钥的文件路径
*/
public ArrayList<byte []> decrypt(ArrayList<byte []> list, String getPath) throws IOException, ClassNotFoundException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
ArrayList<byte []> listDecryptStringByte = new ArrayList<byte []> ();
panDuanPath(new File(getPath));
getKeyGenerator();
for(byte [] encryptStringByte : list) {
this.encryptStringByte = encryptStringByte;
listDecryptStringByte.add(decrypt());
}
return listDecryptStringByte;
}
/*
* 判断文件是否存在
*/
private void panDuanPath(File getPath) throws FileNotFoundException {
if(!getPath.exists()) {
throw new FileNotFoundException();
}
this.getPath = getPath.getPath();
}
/*
* 取得密钥,从保存密钥的文本中取得
*/
private void getKeyGenerator() throws IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream(this.getPath);
ObjectInputStream ois = new ObjectInputStream(fis);
this.secretkey = (SecretKey)ois.readObject();
ois.close();
fis.close();
}
/*
* 解密
*/
private byte [] decrypt() throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException {
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.DECRYPT_MODE, this.secretkey);
byte [] decryptStringByte= cipher.doFinal(encryptStringByte);
return decryptStringByte;
}

public static void main(String [] args) throws Exception {
DES_Decrypt des_d = new DES_Decrypt();
DES_Encrypt des_e = new DES_Encrypt();
ArrayList<String> list = new ArrayList<String> ();
list.add("wanglin");
list.add("wfambwl");
ArrayList<byte []> aa = des_e.encrypt(list,"d:/list/txt.txt");
ArrayList<byte []> aaa = des_d.decrypt(aa,"d:/list/txt.txt");
System.out.println(new String(aaa.get(0)));
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值