Skip to content

JsCrypto AES GCM Encrypted String not decrypted in Java code. #13

@ashuw20001

Description

@ashuw20001

Javascript code

var salt = JsCrypto.Utf8.parse("abcd");
var iv = JsCrypto.Utf8.parse("DuWObJpjGijl6mxB");

var password = "0Usd2xqrLnjXJjcMKFb2T6SrWTag8FSXzAIfnHCIY24="
var plainText = "test";

var keySize = 256;
var iterations = 1;
var options = {
iv: iv,
mode: JsCrypto.mode.GCM,
padding: JsCrypto.pad.Pkcs7,
}

function generateKey(passphrase, salt) {
var key = JsCrypto.PBKDF2.getKey(
passphrase ,
salt,
{ keySize: keySize, iterations: iterations }
);
return key;
}

function encrypt(word) {
var key1= generateKey(password,salt);
var encrypted = JsCrypto.AES.encrypt(word, key1, options);
return encrypted.cipherText.toString(JsCrypto.Base64);
// return ciphertext.toString();
}

function decrypt(word) {
var key1= generateKey(password,salt);
var decrypt = JsCrypto.AES.decrypt(word, key1, options);
return JsCrypto.Utf8.stringify(decrypt).toString();
}

var encryptTxt = encrypt(plainText);
console.log(encryptTxt)

var decryptTxt = decrypt(encryptTxt);

console.log(decryptTxt)

Output
nj0XaL1LnSgQHw/bCFW/2A==
test

Java Code
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.spec.KeySpec;
import java.util.Base64;

public class NewGcmTry {
private static final String DEFAULT_CIPHER_ALGORITHM = "AES/GCM/NoPadding";
private static final int GCM_TAG_LENGTH = 128;
private static final int GCM_IV_LENGTH = 12;
private static final int SALT_LENGTH = 16;
private static final Charset UTF_8 = StandardCharsets.UTF_8;

public static String encrypt(byte[] pText, String password) {
    try {
        byte[] salt = "abcd".getBytes();
        byte[] iv = "DuWObJpjGijl6mxB".getBytes();

      SecretKey aesKeyFromPassword = getSecretKey(password.toCharArray(),salt);

        Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
        GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH, iv);
        cipher.init(Cipher.ENCRYPT_MODE,aesKeyFromPassword ,gcmParameterSpec);
        byte[] result = cipher.doFinal(pText);
        return Base64.getEncoder().encodeToString(result);
    } catch (Exception ex) {
        System.out.println(ex);

// logger.error("encrypt error", ex);
}
return null;
}

public static void main(String[] args) {
     String password = "0Usd2xqrLnjXJjcMKFb2T6SrWTag8FSXzAIfnHCIY24=";
     String plainText = "test";
    String encrypt = NewGcmTry.encrypt(plainText.getBytes(StandardCharsets.UTF_8), password);
    System.out.println(encrypt);
}


private static SecretKey getSecretKey(char[] password, byte[] salt) {
    try {

        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
        KeySpec spec = new PBEKeySpec(password, salt, 1, 256); //65536
        SecretKey secret = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES");
        return secret;
    } catch (Exception ex) {
        System.out.println(ex);
    }
    return null;
}

}

Output
BYY3sot+nIXRX0x3a52OW55/kVg=

Kindly help to find similar code for java also.

I wanted to created encrypted text in Javascript and decrypt in Java.

Thanks in advance !

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions