You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
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);
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;
// logger.error("encrypt error", 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 !
The text was updated successfully, but these errors were encountered: