问题描述
我有一个使用AES加密Java文件的文件。我也有一个密钥文件加密。但我不明白如何使用密钥来解密文件。大多数教程和示例创建临时随机密钥,加密文件并将其解密在一个地方。那么问题是如何指定一个必须用于解密的密钥?
编辑:
示例我发现使用以下代码生成密钥。我不知道我可以在这里使用我的钥匙。
KeyGenerator kgen = KeyGenerator.getInstance(AES);
kgen.init(128);
SecretKey key = kgen.generateKey();
只是为了总结我对Lucifer答案的意见。 p>
-
如果您不知道用什么填充进行加密,则使用'无填充'设置进行解密。这将解密所有内容,包括填充,并且不会因为填充不匹配而抛出错误。
-
当您解密cyphertext时,请查看输出的最后一个块,看看使用了什么padding。不同的paddings留下不同的字节模式,所以通常很容易告诉。
-
设置你的解密方法以期望正确的填充类型,自动删除。
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.
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.
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.
Set your decryption method to expect the correct type of padding, and it will be automatically removed for you.
这篇关于在java中解密AES加密文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!