如何使用已生成的 AES 256 GCM 96 密钥(来自 Hashicorp Vault)加密数据?

How can I encrypt data with an already generated AES 256 GCM 96 key (coming from Hashicorp Vault)?

我有一个表示对称密钥的字符串,是通过使用 Hashicorp Vault 获得的(实际上这可能并不重要)。我需要这个密钥来加密大文件,所以我不能将文件直接发送到 Vault 要求它加密数据。我想在本地进行,所以我要求 Vault 为我创建一个对称密钥(通过使用 transit/datakey/plaintext/ 端点)。我现在有一个 44 字节长的对称密钥(及其密文),使用 aes256_gcm96 算法生成。据我所知,我的 32 字节密钥被 96 位(12 字节)gcm 块包裹。 现在我想使用这个密钥来加密我的数据,但是密钥太长而无法做到这一点,所以我需要以某种方式解开它或调用一些接受输入这样的密钥的函数。我试图使用 Cipher 来加密我的数据。到目前为止,这是我(错误地)做的事情

byte[] datakeyByteArray = mySymmetricKey.getBytes();
SecretKey secretKey = new SecretKeySpec(datakeyByteArray, "AES_256");
Cipher cipher = Cipher.getInstance("AES_256/GCM/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);`

调用init函数时,很明显,抛出异常:java.security.InvalidKeyException: key must be 32 bytes

如何操作才能获得有效密钥?

谢谢。

您已经从@Saptarshi Basu 那里获得了 link,它大体展示了如何使用 AES GCM 加密数据。正如您在我的代码中看到的那样,这样做并没有什么真正的“神秘”,但是 运行 中有一些陷阱。

让我们从最重要的信息开始 - 加密密钥 是什么?从 Hashicorp,您收到了一个 44 字节长的字符串,它是纯 32 字节长的 AES GCM 密钥,但采用 Base64 编码。要获得可用于 Java 加密的密钥,您需要将密钥解码为字节数组,如下所示:

String keyBase64 = "VxJWkOYm2F5z1nF1th9zreS6ZAZMFkCq0c/Ik460ayw=";
byte[] key = Base64.getDecoder().decode(keyBase64);

我们确实需要的第二个信息是 AES 模式 - 您将其正确命名为 AES GCM 模式,并且当您提供 Java 一个 32 字节 = 256 位长的密钥时,它就是请求的 AES GCM 256 algorithm/mode.

AES GCM 加密需要第三个参数,它是 nonce(或有时称为初始化向量)。 Hashicorp 告诉您使用 96 位 = 12 字节长的随机数。出于安全原因,每次加密时使用不同的随机数非常重要,因此最好使用(安全的)随机生成的随机数:

byte[] nonceRandom = new byte[12];
SecureRandom secureRandom = new SecureRandom();
secureRandom.nextBytes(nonceRandom);

现在我们已准备好加密并将所有数据放在一起,执行“.doFinal”步骤,我们将收到一个包含密文的字节数组。但是停下来——我们需要用这种方式将使用过的随机数和密文连接成一个更大的密文WithNonce:

nonce | ciphertext

只需将随机数和密文复制到新的字节数组即可。这个“ciphertextWithNonce”然后被 Base64 编码为最终的 ciphertextBase64 并出于上传原因写入文件。

如果您将自己的密钥粘贴到程序的开头并运行它,您将收到一个名为“hashicorp_test.enc”的文件,该文件已准备好上传给您。

这是一个样本输出(你的会有所不同,因为有一个随机元素):

Hashicorp Vault AES GCM encryption
ciphertext: /YB+kfVlIhMowLrsnndD737o2CcyWMfr4xnAADnCBSNCSvMG25aR8UzU2ta8wLwdnHfcago/25KFJ2ky95wpFtsCNE63xRs=
ciphertext written to file: hashicorp_test.enc
used key: VxJWkOYm2F5z1nF1th9zreS6ZAZMFkCq0c/Ik460ayw=

如果您想在在线编译器中查看这段代码 运行ning,这里是 link: https://repl.it/@javacrypto/SoHashicorpVaultAesGcmEncryption

这是一个“概念证明”,一般性地展示了如何执行加密,但它缺少一些关键点,我懒得让你的工作:-)。

  1. 此示例将字符串加密为加密文件 - 您需要从文件中获取原始数据
  2. 如果文件很大,您可能会遇到“内存不足”错误,因为对数据的所有操作都在 你的堆 - 为了进行简单的计算,你需要 4.5 * 原始数据的可用内存,因为你使用了原始数据 到内存中,第二次将加密数据存储在内存中,第三次将加密数据复制到 ciphertextWithNonce 并在最后(数字 4)将所有数据编码为 base64 字符串。对于大型程序,您需要 切换到“块智能”加密,使用 CiphertextOutputStream
  3. 为了使完整数据的 Base64 写入更加方便,我建议额外使用 Apache 的 Base64OutputStream(可通过 Maven https://mvnrepository.com/artifact/commons-codec/commons-codec)。

安全警告:此代码无异常处理,仅供学习使用。

代码:

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;

public class Hashicorp_Aes_Gcm_encryption {
    public static void main(String[] args) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, IOException {
        System.out.println("Hashicorp Vault AES GCM encryption");
        // 
        // paste your key here:
        String keyBase64 = "VxJWkOYm2F5z1nF1th9zreS6ZAZMFkCq0c/Ik460ayw=";
        // filename with ciphertext for upload
        String filename = "hashicorp_test.enc";

        // my sample plaintext
        String plaintext = "The quick brown fox jumps over the lazy dog";

        // aes gcm encryption
        // decode key
        byte[] key = Base64.getDecoder().decode(keyBase64);
        // generate random nonce
        byte[] nonceRandom = new byte[12];
        SecureRandom secureRandom = new SecureRandom();
        secureRandom.nextBytes(nonceRandom);
        // calculate specs
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
        GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(16 * 8, nonceRandom);
        // initialize cipher
        Cipher cipher = Cipher.getInstance("AES/GCM/PKCS5Padding");//NOPadding
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, gcmParameterSpec);
        // encrypt
        byte[] ciphertext = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8));
        // concentenate iv + ciphertext
        int ciphertextWithNonceLength = nonceRandom.length + ciphertext.length;
        byte[] ciphertextWithNonce = new byte[ciphertextWithNonceLength];
        System.arraycopy(nonceRandom, 0, ciphertextWithNonce, 0, nonceRandom.length);
        System.arraycopy(ciphertext, 0, ciphertextWithNonce, nonceRandom.length, ciphertext.length);
        String ciphertextBase64 = Base64.getEncoder().encodeToString(ciphertextWithNonce);
        System.out.println("ciphertext: " + ciphertextBase64);
        // save encrypted data to a file
        Files.write(Paths.get(filename), ciphertextBase64.getBytes(StandardCharsets.UTF_8));
        System.out.println("ciphertext written to file: " + filename);
        System.out.println("used key: " + keyBase64);
    }
}