Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ashuw20001 opened this issue Jul 26, 2022 · 0 comments
Open

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

ashuw20001 opened this issue Jul 26, 2022 · 0 comments

Comments

@ashuw20001
Copy link

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 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant