在java中解密AES加密文件 [英] Decrypt AES encrypted file in java

查看:201
本文介绍了在java中解密AES加密文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用AES加密Java文件的文件。我也有一个密钥文件加密。但我不明白如何使用密钥来解密文件。大多数教程和示例创建临时随机密钥,加密文件并将其解密在一个地方。
那么问题是如何指定一个必须用于解密的密钥?



编辑
示例我发现使用以下代码生成密钥。我不知道我可以在这里使用我的钥匙。

  KeyGenerator kgen = KeyGenerator.getInstance(AES); 
kgen.init(128);
SecretKey key = kgen.generateKey();


解决方案

只是为了总结我对Lucifer答案的意见。 p>


  1. 如果您不知道用什么填充进行加密,则使用'无填充'设置进行解密。这将解密所有内容,包括填充,并且不会因为填充不匹配而抛出错误。


  2. 当您解密cyphertext时,请查看输出的最后一个块,看看使用了什么padding。不同的paddings留下不同的字节模式,所以通常很容易告诉。


  3. 设置你的解密方法以期望正确的填充类型,自动删除。



I have a file encrypted with java application using AES. I also have a key file was encrypted with. But i can't understand how to use the key to decrypt file. Most tutorials and examples create temporary random key, encrypt file and decrypt it in one place. So, question is how to specify a key which have to be used for decryption?

EDIT: Samples i found use following code to generate key. I have no idea where i can use my key here.

KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
SecretKey key = kgen.generateKey();

解决方案

Just to summarise my comments to Lucifer's answer.

  1. If you don't know what padding was used to encrypt, then decrypt with 'no padding' set. That will decrypt everything, including the padding, and won't throw an error because of mismatched padding.

  2. When you have decrypted the cyphertext, have a look at the last block of the output and see what padding was used. Different paddings leave different byte patterns, so it is usually easy enough to tell.

  3. Set your decryption method to expect the correct type of padding, and it will be automatically removed for you.

这篇关于在java中解密AES加密文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆