我已经使用AES / GCM / NoPadding在Java中加密了字符串,现在我想在Angular6中解密。我发现的所有示例都使用AES-CBC,我想使用AES-GCM解密。
这是我的Java代码。
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
public class AESGCMEncryptionUtils {
private static final String SALT = "some salt";
private static final int AES_KEY_SIZE = 256;
private static final int GCM_NONCE_LENGTH = 12; // IV
private static final int GCM_TAG_LENGTH = 16;
private static final String AES_GCM_ALGORITHM = "AES/GCM/NoPadding";
private static final String PBKDF2 = "PBKDF2WithHmacSHA256";
private static final String ALGORITHAM = "AES";
public static SecretKeySpec generateKey() throws Exception
{
PBEKeySpec spec = new PBEKeySpec("some password".toCharArray(),SALT.getBytes(),10000, AES_KEY_SIZE);
SecretKey key = SecretKeyFactory.getInstance(PBKDF2).generateSecret(spec);
SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), ALGORITHAM);
return keySpec;
}
public static String encrypt(String strToEncrypt)
{
try
{
byte[] nonce = new byte[GCM_NONCE_LENGTH];
Cipher cipher = Cipher.getInstance(AES_GCM_ALGORITHM);
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8 , nonce);
cipher.init(Cipher.ENCRYPT_MODE, generateKey(), gcmParameterSpec);
return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
}
catch (Exception e)
{
log.info("Error while encrypting: " + e.toString());
return null;
}
}
}
我在Angular6中需要等效的解密逻辑。请帮助我。