Skip to content

Instantly share code, notes, and snippets.

@VimanyuAgg
Created December 9, 2020 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VimanyuAgg/05a8c8273b0a080bb2423ec3fc0dbe9d to your computer and use it in GitHub Desktop.
Save VimanyuAgg/05a8c8273b0a080bb2423ec3fc0dbe9d to your computer and use it in GitHub Desktop.
aes_gcm_decryption java
import javax.crypto.SecretKey;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Arrays;
import java.util.Base64;
class Main {
private final static int GCM_IV_LENGTH = 12;
private final static int GCM_TAG_LENGTH = 16;
private static String decrypt(String encrypted, SecretKey skey) throws Exception {
byte[] decoded = Base64.getDecoder().decode(encrypted);
byte[] iv = Arrays.copyOfRange(decoded, 0, GCM_IV_LENGTH);
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
GCMParameterSpec ivSpec = new GCMParameterSpec(GCM_TAG_LENGTH * Byte.SIZE, iv);
cipher.init(Cipher.DECRYPT_MODE, skey, ivSpec);
byte[] ciphertext = cipher.doFinal(decoded, GCM_IV_LENGTH, decoded.length - GCM_IV_LENGTH);
String result = new String(ciphertext, "UTF8");
System.out.println(result);
return result;
}
public static void main(String[] args) throws Exception {
SecretKey key = new SecretKeySpec("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".getBytes(), "AES");
decrypt("SyiPupJTBm8I5+l2+VIyedKNRjLkJIj12dTsO7cCocMPl7JP9qoHIA==",key); // Got encrypted text from nodejs client (base64 encoded)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment