Cannot decrypt long AES-256 GCM message with Java

和自甴很熟 提交于 2019-12-05 11:12:39

问题


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

来源:https://stackoverflow.com/questions/46307621/cannot-decrypt-long-aes-256-gcm-message-with-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!