首页  编辑  

AES GCM 加密算法

Tags: /Java/   Date Created:
AES/CBC/PKCS5Padding 加密算法, 在SonarQube扫描的时候,会提示:
Use another cipher mode or disable padding.
Encryption algorithms should be used with secure mode and padding secheme. java:S5542
可以用AES/GCM/NoPadding来整改。

能通过 Sonar 扫描的 AES GCM 加密代码
  • 支持任意长度密码,通过密码来派生加密密钥
    • 先生成8字节长度salt,然后利用 EVP KDF 算法,结合密码,Salt, MD5算法进行一轮迭代生成派生密钥
    • 用派生密钥结合AES算法进行加密得到加密后的密文数据
    • 按常量 "Salted__" + Salt + 密文数据 组装数据,然后用Base64编码得到输出
  • 支持与前端加解密(CryptoJS)
import javax.crypto.*;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.util.Arrays;
import java.util.Base64;


/**
 * Conforming with CryptoJS AES method
 */
public class AESUtil {
  private static final int KEY_SIZE = 256;
  private static final int IV_SIZE = 128;
  private static final String HASH_CIPHER = "AES/GCM/NoPadding";
  private static final String AES = "AES";

  private AESUtil() {
  }

  public static String encrypt(String password, String plainText, boolean urlEncoder) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    SecureRandom secureRandom = new SecureRandom();

    byte[] saltBytes = new byte[8];
    secureRandom.nextBytes(saltBytes);

    byte[] iv = new byte[IV_SIZE / 8];
    secureRandom.nextBytes(iv);

    byte[] key = new byte[KEY_SIZE / 8];
    evpKDF(password.getBytes(StandardCharsets.UTF_8), KEY_SIZE, IV_SIZE, saltBytes, key, iv);

    SecretKey secretKey = new SecretKeySpec(key, AES);
    Cipher cipher = Cipher.getInstance(HASH_CIPHER);
    cipher.init(Cipher.ENCRYPT_MODE, secretKey, new GCMParameterSpec(128, iv));
    byte[] cipherText = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));

    byte[] header = "Salted__".getBytes(StandardCharsets.UTF_8);
    byte[] buffer = new byte[header.length + saltBytes.length + cipherText.length];
    System.arraycopy(header, 0, buffer, 0, header.length);
    System.arraycopy(saltBytes, 0, buffer, header.length, saltBytes.length);
    System.arraycopy(cipherText, 0, buffer, header.length + saltBytes.length, cipherText.length);

    if (urlEncoder) {
      return Base64.getUrlEncoder().withoutPadding().encodeToString(buffer);
    } else {
      return Base64.getEncoder().encodeToString(buffer);
    }
  }

  public static String decrypt(String password, String cipherText, boolean urlEncoder) throws NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, InvalidKeyException {
    byte[] ctBytes = urlEncoder ? Base64.getUrlDecoder().decode(cipherText) : Base64.getDecoder().decode(cipherText);
    byte[] salt = Arrays.copyOfRange(ctBytes, 8, 16);
    byte[] ciphertextBytes = Arrays.copyOfRange(ctBytes, 16, ctBytes.length);
    byte[] key = new byte[KEY_SIZE / 8];
    byte[] iv = new byte[IV_SIZE / 8];

    evpKDF(password.getBytes(StandardCharsets.UTF_8), KEY_SIZE, IV_SIZE, salt, key, iv);
    Cipher cipher = Cipher.getInstance(HASH_CIPHER);
    SecretKey secretKey = new SecretKeySpec(key, AES);
    cipher.init(Cipher.DECRYPT_MODE, secretKey, new GCMParameterSpec(128, iv));
    byte[] plainText = cipher.doFinal(ciphertextBytes);
    return new String(plainText);
  }

  private static byte[] evpKDF(byte[] password, int keySize, int ivSize, byte[] salt, byte[] resultKey, byte[] resultIv) throws NoSuchAlgorithmException {
    return evpKDF(password, keySize, ivSize, salt, "MD5", 1, resultKey, resultIv);
  }

  private static byte[] evpKDF(byte[] password, int keySize, int ivSize, byte[] salt, String hash, int iterations, byte[] resultKey, byte[] resultIv) throws NoSuchAlgorithmException {
    keySize = keySize / 32;
    ivSize = ivSize / 32;
    int targetKeySize = keySize + ivSize;
    byte[] derivedBytes = new byte[targetKeySize * 4];
    int numberOfDerivedWords = 0;
    byte[] block = null;
    MessageDigest hasher = MessageDigest.getInstance(hash);
    while (numberOfDerivedWords < targetKeySize) {
      if (block != null) {
        hasher.update(block);
      }
      hasher.update(password);
      block = hasher.digest(salt);
      hasher.reset();

      // Iterations
      for (int i = 1; i < iterations; i++) {
        block = hasher.digest(block);
        hasher.reset();
      }

      System.arraycopy(block, 0, derivedBytes, numberOfDerivedWords * 4,
          Math.min(block.length, (targetKeySize - numberOfDerivedWords) * 4));

      numberOfDerivedWords += block.length / 4;
    }

    System.arraycopy(derivedBytes, 0, resultKey, 0, keySize * 4);
    System.arraycopy(derivedBytes, keySize * 4, resultIv, 0, ivSize * 4);

    return derivedBytes; // key + iv
  }

}