AES_GCM_256加密

Posted 非花非雾--

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AES_GCM_256加密相关的知识,希望对你有一定的参考价值。

final class AesGcmUtils 
    private static final String ALG_AES = "AES";
    private static final String ALGORITHM = "AES/GCM/PKCS5Padding";
    private static final int TAG_LENGTH_BIT = 120;//must be one of 128, 120, 112, 104, 96

    /**
     * Get encrypted object
     * @param key It must be 32-bit
     * @param iv It must be 16-bit
     * @return
     */
    private static Cipher getCipher(@NonNull byte[] key, @NonNull byte[] iv) 
        SecretKeySpec keySpec = new SecretKeySpec(key, ALG_AES);
        Cipher cipher = null;
        try 
            cipher = Cipher.getInstance(ALGORITHM);
            GCMParameterSpec params = new GCMParameterSpec(TAG_LENGTH_BIT, iv);
            cipher.init(Cipher.DECRYPT_MODE, keySpec, params);
         catch (NoSuchAlgorithmException e) 
            e.printStackTrace();
         catch (NoSuchPaddingException e) 
            e.printStackTrace();
         catch (InvalidAlgorithmParameterException e) 
            e.printStackTrace();
         catch (InvalidKeyException e) 
            e.printStackTrace();
        

        return cipher;
    

    public static byte[] encrypt(@NonNull byte[] content, @NonNull byte[] key, @NonNull byte[] iv) throws Exception 
        Cipher cipher = getCipher(key,iv);
        if(cipher == null)
            throw new NullPointerException("Failed to get encryption method");
        
        return cipher.doFinal(content);
    

    public static byte[] decrypt(@NonNull byte[] content, @NonNull byte[] key, @NonNull byte[] iv) throws Exception 
        SecretKeySpec keySpec = new SecretKeySpec(key, ALG_AES);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        GCMParameterSpec params = new GCMParameterSpec(TAG_LENGTH_BIT, iv);
        cipher.init(Cipher.DECRYPT_MODE, keySpec, params);
        return cipher.doFinal(content);
    

    public static byte[] buildIv(String ivString) 
        byte[] derivative = ivString.getBytes();
        byte[] iv = new byte[16];
        System.arraycopy(derivative, 0, iv, 0, iv.length);
        return iv;
    

测试用例代码:

@RunWith(RobolectricTestRunner.class)
public class AesGcmUtilsTest 
    static String KEY = "NxQHusZRgJ4kEsGIv2tRDxs";
    static String IV = "123456781122321321321321313231213";
    static String CONTENT = "Test aes_gcm encrypt data!";
    static Base64.Decoder decoder = Base64.getDecoder();
    static Base64.Encoder encoder = Base64.getEncoder();

    @Test
    public void encode() 
        try 
            assertThat(encoder.encodeToString(AesGcmUtils.encrypt(CONTENT.getBytes(),
                    encoder.encode(KEY.getBytes())
                    , AesGcmUtils.buildIv(IV))))
                    .isEqualTo("JjbKFck2UqZw3dQ0O3wSPsYyiLRWRlKgxyzH22B+ig4253d/rWsKHjA=");
         catch (Exception e) 
            e.printStackTrace();
        
    

    @Test
    public void decode() 
        try 
            assertThat(new String(
                    AesGcmUtils.decrypt(decoder.decode("JjbKFck2UqZw3dQ0O3wSPsYyiLRWRlKgxyzH22B+ig4253d/rWsKHjA="),
                            encoder.encode(KEY.getBytes())
                    , AesGcmUtils.buildIv(IV))))
                    .isEqualTo(CONTENT);
         catch (Exception e) 
            e.printStackTrace();
        
    

以上是关于AES_GCM_256加密的主要内容,如果未能解决你的问题,请参考以下文章

AES128_CBC_NoPading加密、sha256withRSA签名

使用 aes_256_cbc 密码加密时的默认 IV 是啥?

“sha256_password 或caching_sha2_password 需要加密”

解密在 PHP 中使用 MCRYPT_RIJNDAEL_256 加密的 Python 字符串

PHP 中的 AES-256 加密

Flask 学习-24.用户注册(sha256_crypt对密码加密)