java 3des解密过程_3des加密/解密文件java

我有一个读取文件(例如* .zip)的问题,并使用3DES对其进行加密,使用加密文件的名称生成的secretKey。 然后我需要解密这个文件,然后把它写在硬盘上。 我试图解决这个问题,但解密文件时卡住了。3des加密/解密文件java

这里是加密

public class Encryptor {

private static String inputFilePath = "D:/1.txt";

public static void main(String[] args) {

FileOutputStream fos = null;

File file = new File(inputFilePath);

String keyString = "140405PX_0.$88";

String algorithm = "DESede";

try {

FileInputStream fileInputStream = new FileInputStream(file);

byte[] fileByteArray = new byte[fileInputStream.available()];

fileInputStream.read(fileByteArray);

for (byte b : fileByteArray) {

System.out.println(b);

}

SecretKey secretKey = getKey(keyString);

Cipher cipher = Cipher.getInstance(algorithm);

cipher.init(Cipher.ENCRYPT_MODE, secretKey);

ObjectOutputStream objectOutputStream = new ObjectOutputStream

(new CipherOutputStream

(new FileOutputStream

("D:/Secret.file"), cipher));

objectOutputStream.writeObject(fileByteArray);

objectOutputStream.close();

} catch (Exception e) {

e.printStackTrace();

}

}

public static SecretKey getKey(String message) throws Exception {

String messageToUpperCase = message.toUpperCase();

byte[] digestOfPassword = messageToUpperCase.getBytes();

byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);

SecretKey key = new SecretKeySpec(keyBytes, "DESede");

return key;

}

}

的代码这里是解密

public class Decryptor {

public static void main(String[] args) {

try {

File inputFileNAme = new File("d:/Secret.file");

FileInputStream fileInputStream = new FileInputStream(inputFileNAme);

FileOutputStream fileOutputStream = new FileOutputStream(outputFilePath);

SecretKey secretKey = getKey(keyString);

Cipher cipher = Cipher.getInstance(algorithm);

cipher.init(Cipher.DECRYPT_MODE, secretKey);

ObjectInputStream objectInputStream = new ObjectInputStream

(new CipherInputStream(fileInputStream, cipher));

System.out.println(objectInputStream.available());

while (objectInputStream.available() != 0) {

fileOutputStream.write((Integer) objectInputStream.readObject());

System.out.println(objectInputStream.readObject());

}

fileOutputStream.flush();

fileOutputStream.close();

fileInputStream.close();

objectInputStream.close();

} catch (Exception e) {

e.printStackTrace();

}

}

public static SecretKey getKey(String message) throws Exception {

String messageToUpperCase = message.toUpperCase();

byte[] digestOfPassword = messageToUpperCase.getBytes();

byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);

SecretKey key = new SecretKeySpec(keyBytes, "DESede");

return key;

}

}

当我尝试解密我的文件,我没有得到在输出文件中的任何代码。

我试过做调试,并看到,objectInputStream.available()总是包含0.

请告诉我,我该如何解决这个问题,以及为什么会发生。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值