Java AES-GCM和BouncyCastle投掷;GCM中的mac检查失败”;与IV一起使用时

Java AES-GCM和BouncyCastle投掷;GCM中的mac检查失败”;与IV一起使用时,java,encryption,bouncycastle,initialization-vector,aes-gcm,Java,Encryption,Bouncycastle,Initialization Vector,Aes Gcm,我对开发加密技术还比较陌生。现在我正在尝试编写一个类,它使用BouncyCastle和AES-GCM对字符串进行加密和解密。我读到了在实现加密时必须考虑的事情。其中之一是,你应该始终使用随机静脉注射。 问题是,每次我尝试用IV初始化密码时,它都无法正确解密我的文本。 它只是抛出以下异常: javax.crypto.AEADBadTagException: mac check in GCM failed at sun.reflect.NativeConstructorAccessorImpl.ne

我对开发加密技术还比较陌生。现在我正在尝试编写一个类,它使用BouncyCastle和AES-GCM对字符串进行加密和解密。我读到了在实现加密时必须考虑的事情。其中之一是,你应该始终使用随机静脉注射。 问题是,每次我尝试用IV初始化密码时,它都无法正确解密我的文本。
它只是抛出以下异常:

javax.crypto.AEADBadTagException: mac check in GCM failed
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher$AEADGenericBlockCipher.doFinal(Unknown Source)
at org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineDoFinal(Unknown Source)
at javax.crypto.Cipher.doFinal(Cipher.java:2165)
at BouncyCastleEX.decrypt(BouncyCastleEX.java:78)
at BouncyCastleEX.main(BouncyCastleEX.java:43)
我使用以下方法来加密和解密我的数据

private static final String fallbackSalt = "ajefa6tc73t6raiw7tr63wi3r7citrawcirtcdg78o2vawri7t";
private static final int iterations = 2000;
private static final int keyLength = 256;
private static final SecureRandom random = new SecureRandom();

public byte[] encrypt(String plaintext, String passphrase, String salt)
        throws Exception {
    SecretKey key = generateKey(passphrase, salt);
    Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", "BC");
    cipher.init(Cipher.ENCRYPT_MODE, key, generateIV(cipher),random);
    return cipher.doFinal(plaintext.getBytes());
}

public String decrypt(byte[] encrypted, String passphrase, String salt)
        throws Exception {
    SecretKey key = generateKey(passphrase, salt);
    Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", "BC");
    cipher.init(Cipher.DECRYPT_MODE, key, generateIV(cipher),random);
    return new String(cipher.doFinal(encrypted));
}

private SecretKey generateKey(String passphrase, String salt)
        throws Exception {
    PBEKeySpec keySpec = new PBEKeySpec(passphrase.toCharArray(),
            salt.getBytes(), iterations, keyLength);
    SecretKeyFactory keyFactory = SecretKeyFactory
            .getInstance("PBEWITHSHA256AND256BITAES-CBC-BC");
    return keyFactory.generateSecret(keySpec);
}

private IvParameterSpec generateIV(Cipher cipher) throws Exception {
    byte[] ivBytes = new byte[cipher.getBlockSize()];
    random.nextBytes(ivBytes);
    return new IvParameterSpec(ivBytes);
}
如果我从cipher.init(…)中删除“generateIV(cipher)”,一切工作都会完美无瑕。但据我所知,它极大地削弱了加密

是的,我不知道这是代码中的一个小错误还是其他我一无所知的东西


我真的很感谢你的帮助,非常感谢

您必须使用相同的IV进行加密和解密。它不必是秘密的,但仅对AES-GCM是唯一的(从技术上讲,它是暂时的)。一种常见的方法是在密文前面加上IV,并在解密之前将其删除


使用消息计数器而不是随机生成IV也是很常见的。如果更改了键,则应将IV重置为初始值并再次开始计数。在某些数量的消息中,您需要一个新密钥,因为AES-GCM的安全保证失效。这个数字介于248到264条消息之间。

这是我在Artjom的帮助下编写的代码的最终版本。 这似乎工作得很好,但如果你发现任何错误或事情,削弱了安全性,请让我知道

import java.security.SecureRandom;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.Arrays;
import com.sun.org.apache.xml.internal.security.utils.Base64;

public class BouncyCastleEX {

private static final int iterations = 2000;
private static final int keyLength = 256;
private static final SecureRandom random = new SecureRandom();
private static BouncyCastleEX instance = null;

public String encryptString(String plaintext, String passphrase, String salt)
        throws Exception {
    return Base64.encode(encrypt(plaintext, passphrase, salt));
}

public String decryptString(String encrypted, String passphrase, String salt)
        throws Exception {
    return decrypt(Base64.decode(encrypted), passphrase, salt);
}

private BouncyCastleEX() {
    Security.addProvider(new BouncyCastleProvider());
}

public static BouncyCastleEX getInstance() {
    if (instance == null) {
        instance = new BouncyCastleEX();
    }
    return instance;
}

private byte[] encrypt(String plaintext, String passphrase, String salt)
        throws Exception {
    SecretKey key = generateKey(passphrase, salt);
    Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", "BC");
    byte[] ivBytes = generateIVBytes(cipher);
    cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(ivBytes),
            random);
    return Arrays
            .concatenate(ivBytes, cipher.doFinal(plaintext.getBytes()));
}

private String decrypt(byte[] encrypted, String passphrase, String salt)
        throws Exception {
    SecretKey key = generateKey(passphrase, salt);
    Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", "BC");
    cipher.init(Cipher.DECRYPT_MODE, key,
            new IvParameterSpec(Arrays.copyOfRange(encrypted, 0, 12)),
            random);
    return new String(cipher.doFinal(Arrays.copyOfRange(encrypted, 12,
            encrypted.length)));
}

private SecretKey generateKey(String passphrase, String salt)
        throws Exception {
    PBEKeySpec keySpec = new PBEKeySpec(passphrase.toCharArray(),
            salt.getBytes(), iterations, keyLength);
    SecretKeyFactory keyFactory = SecretKeyFactory
            .getInstance("PBEWITHSHA256AND256BITAES-CBC-BC");
    return keyFactory.generateSecret(keySpec);
}

private byte[] generateIVBytes(Cipher cipher) throws Exception {
    byte[] ivBytes = new byte[12];
    random.nextBytes(ivBytes);

    return ivBytes;
}

}

这不是代码中的错误,解密时必须使用与加密时相同的IV。静脉注射是否可以打开取决于人们想要静脉注射的效果。现在一切都很好。非常感谢。如果有人有同样的问题,我会使用BouncyCastle中的Arrays.concatenate(…)和Arrays.copyOfRange(…)方法。只需确保结果数组中不包含范围的第二个索引。这意味着您的第二个数组是从索引12到数组大小!要到1号才可以。对不起。我是新来的。我刚刚发布了我的最终代码。它似乎工作得很好。您可能应该随机生成salt,并在密文前面写入它。基本上,盐的处理方法应该与静脉注射完全相同。