Created
December 9, 2020 07:09
-
-
Save VimanyuAgg/05a8c8273b0a080bb2423ec3fc0dbe9d to your computer and use it in GitHub Desktop.
aes_gcm_decryption java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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