用java解密AES加密文件

发布于 2024-12-28 18:12:35 字数 333 浏览 0 评论 0原文

我有一个使用 AES 使用 java 应用程序加密的文件。我还有一个加密的密钥文件。但我不明白如何使用密钥来解密文件。大多数教程和示例都会在一个地方创建临时随机密钥、加密文件和解密。 所以,问题是如何指定必须用于解密的密钥?

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

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

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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

坚持沉默 2025-01-04 18:12:35

只是总结我对路西法答案的评论。

  1. 如果您不知道使用什么填充来加密,请使用“无填充”集进行解密。这将解密所有内容,包括填充,并且不会因为填充不匹配而引发错误。

  2. 解密密文后,查看输出的最后一个块并查看使用了哪些填充。不同的填充会留下不同的字节模式,因此通常很容易辨别。

  3. 将您的解密方法设置为期望正确的填充类型,系统会自动为您删除它。

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.

聚集的泪 2025-01-04 18:12:35

答案可能只是将密钥数据作为字节放入 SecretKeySpec 中,如下所示:

SecretKeySpec aesKey = new SecretKeySpec(myKeyData, "AES");

请注意 SecretKeySpec 实现了 Key 接口,因此您可以直接在 Cipher.init() 方法中使用它。因此不需要 SecretKeyFactory,否则您将使用它。

The answer could be simply to put the key data as bytes into a SecretKeySpec like this:

SecretKeySpec aesKey = new SecretKeySpec(myKeyData, "AES");

Note that SecretKeySpec implements the Key interface, so you can use it directly in a Cipher.init() method. So there is no SecretKeyFactory needed, which you would use otherwise.

触ぅ动初心 2025-01-04 18:12:35

如果对您有帮助,请尝试以下方法。

private static byte[] cipherData(PaddedBufferedBlockCipher cipher, byte[] data)
        throws Exception
{
    int minSize = cipher.getOutputSize(data.length);
    byte[] outBuf = new byte[minSize];
    int length1 = cipher.processBytes(data, 0, data.length, outBuf, 0);
    int length2 = cipher.doFinal(outBuf, length1);
    int actualLength = length1 + length2;
    byte[] result = new byte[actualLength];
    System.arraycopy(outBuf, 0, result, 0, result.length);
    return result;
}

private static byte[] decrypt(byte[] cipher, byte[] key, byte[] iv) throws Exception
{
    PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(
            new AESEngine()));
    CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
    aes.init(false, ivAndKey);
    return cipherData(aes, cipher);
}

private static byte[] encrypt(byte[] plain, byte[] key, byte[] iv) throws Exception
{
    PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(
            new AESEngine()));
    CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
    aes.init(true, ivAndKey);
    return cipherData(aes, plain);
}

Please try following methods, if might helpful for you.

private static byte[] cipherData(PaddedBufferedBlockCipher cipher, byte[] data)
        throws Exception
{
    int minSize = cipher.getOutputSize(data.length);
    byte[] outBuf = new byte[minSize];
    int length1 = cipher.processBytes(data, 0, data.length, outBuf, 0);
    int length2 = cipher.doFinal(outBuf, length1);
    int actualLength = length1 + length2;
    byte[] result = new byte[actualLength];
    System.arraycopy(outBuf, 0, result, 0, result.length);
    return result;
}

private static byte[] decrypt(byte[] cipher, byte[] key, byte[] iv) throws Exception
{
    PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(
            new AESEngine()));
    CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
    aes.init(false, ivAndKey);
    return cipherData(aes, cipher);
}

private static byte[] encrypt(byte[] plain, byte[] key, byte[] iv) throws Exception
{
    PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(
            new AESEngine()));
    CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
    aes.init(true, ivAndKey);
    return cipherData(aes, plain);
}
甜柠檬 2025-01-04 18:12:35

加密/解密一个巨大视频而不引发Java OutOfMemoryException并使用Java SecureRandom生成初始化向量的完整示例。还描述了将密钥字节存储到数据库,然后从这些字节重建相同的密钥。

https://stackoverflow.com/a/18892960/185022

Complete example of encrypting/Decrypting a huge video without throwing Java OutOfMemoryException and using Java SecureRandom for Initialization Vector generation. Also depicted storing key bytes to database and then reconstructing same key from those bytes.

https://stackoverflow.com/a/18892960/185022

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文