Skip to content

Commit

Permalink
Added Java decryption example - closes #203
Browse files Browse the repository at this point in the history
Co-authored-by: Marcell-Roos
  • Loading branch information
ankane committed Nov 15, 2024
1 parent bfb2816 commit 2b9b3b8
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions docs/Compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Here’s how to decrypt in other languages. For files, skip Base64 decoding the
- [Rust](#rust)
- [Elixir](#elixir)
- [PHP](#php)
- [Java](#java)

Pull requests are welcome for other languages.

Expand Down Expand Up @@ -104,3 +105,28 @@ $ciphertext = substr($ciphertext, 12, -16);

$plaintext = openssl_decrypt($ciphertext, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $nonce, $tag);
```

## Java

```java
import java.util.Base64;
import java.util.HexFormat;
import javax.crypto.Cipher;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class Example
{
public static void main(String[] args) throws Exception {
String key = "61e6ba4a3a2498e3a8fdcd047eff0cd9864016f2c83c34599a3257a57ce6f7fb";
String ciphertext = "Uv/+Sgar0kM216AvVlBH5Gt8vIwtQGfPysl539WY2DER62AoJg==";

byte[] keyBytes = HexFormat.of().parseHex(key);
byte[] ciphertextBytes = Base64.getDecoder().decode(ciphertext);

Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyBytes, "AES"), new GCMParameterSpec(128, ciphertextBytes, 0, 12));
String plaintext = new String(cipher.doFinal(ciphertextBytes, 12, ciphertextBytes.length - 12));
}
}
```

0 comments on commit 2b9b3b8

Please sign in to comment.