0

Related to this question: Cannot decrypt AES-256 GCM with Java

The Java decrypt issue seems to only be fixed if the encrypted message is short, i.e. two words or so. I've tried with the words, "hello" and "short string", and both of these words were decrypted fine. When I tried something like,

Alphanumeric string test1 with more numbers such as 5, 4, 3, 2, 1

AEADBadTagException came up again.

EDIT:

This issue is directly related to how long the encrypted message is. Two words is a bit of an exaggeration, but as long as the encrypted message is about as long as this or longer then Java will run into the exception.

Encrypted message sample:

d+nyOuSfH3wup+5KHJRQyVwVHE0nn7dOfLQsSxb2LsR1LuogHxmVobHoQSTbdyqupd/UvwGfbhkUQz+8CjIBSd7FoEVpgpYv9dAQ3GGUr3AtA+rJJrFHo/EM443sQlSOG4cIBQ7trF7udmrIhtiZ9wMchaBEJFmDBL5Jwl8ZMM0ath8VNWqfyyhghPW8U2NiORAy5mw6v07o7v3UT2 lBzJThBsM=

Decrypted with node:

this is a longer string to make the encrypted message longer than before

EDIT 2:

Java code:

package decryption;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class DecryptAES256 {

    private static String salt;
    private static byte[] iv;
    private static byte[] encryptedMessageAndTag;
    private static byte[] key;

    public static void main(String[] args) {
        String key = "123456789aabbccddeefffffffffffff";
        String sourceText = "zMX8Xp8lCLGP3FsF7dy1uEODFG0+lhpoWR+xZPpNAXm2D39+CJUK5Kk0z4NbDfb/WbP8lHVWcTOuXf8hRA1AmtEV2G5kP3SH3mrGbyf4QthR4aOTqEQQAvt1T8LlIkBlgx32gehP/nwwm3DYyJV+NnN21Ac17L4=";
        System.out.println(decrypt(key, sourceText));
    }

    public static String decrypt(String masterkey, String encryptedText) {
        // decode encryptedText 
        encryptedText = new String(Base64.getDecoder().decode(encryptedText.getBytes()));

        // extract the different parts
        byte[] parts = encryptedText.getBytes();
        salt = new String(Arrays.copyOfRange(parts, 0, 64)); // not using for testing purposes
        iv = Arrays.copyOfRange(parts, 64, 76);
        encryptedMessageAndTag = Arrays.copyOfRange(parts, 76, parts.length);
        try {
            key = masterkey.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            // not going to reach here
        }

        // call helper method to decrypt
        byte[] decipheredText = decodeAES_256_CBC();
        return new String(decipheredText);
    }

    private static byte[] decodeAES_256_CBC() {
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
            Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
            GCMParameterSpec params = new GCMParameterSpec(128, iv, 0, iv.length);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, params);
            return cipher.doFinal(encryptedMessageAndTag);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("Failed to decrypt");
        }
    }
}

EDIT 3:

Cleaned up Java code for readability

bitscuit
  • 976
  • 1
  • 11
  • 26
  • I don't understand the issue with long messages, since AES typically works with 16 bytes at a time, using padding on the last block of 16 bytes if needed. [wiki article](https://en.wikipedia.org/wiki/Advanced_Encryption_Standard) . – rcgldr Sep 19 '17 at 18:56
  • I don't know why it's failing to decrypt with long encrypted messages either. It's just what I have seen from testing. – bitscuit Sep 19 '17 at 19:01
  • Can you write the whole Java code please? (the version corrected after the original question) – Alexandre Fenyo Sep 19 '17 at 19:09
  • @AlexandreFenyo edited in java code – bitscuit Sep 19 '17 at 19:14
  • @gusto2 I did decode it first ```encryptedText = new String(Base64.getDecoder().decode(encryptedText.getBytes()));```, but you are right, I should not be switching back and forth between String and byte[] – bitscuit Sep 19 '17 at 20:38
  • Don't modify `encryptedText` like that; it's completely wrong. Replace those lines with with `byte[] parts = Base64.getDecoder().decode(encryptedText);` If you still have trouble update with new code and results. Otherwise, delete your post, because this is a common mistake that is covered better in many other questions. – erickson Sep 19 '17 at 21:21
  • @erickson Looks like that was the cause. If you don't mind, could you point me to some of those questions or what I should be searching for regarding the string replacement? – bitscuit Sep 19 '17 at 21:28

0 Answers0