AES-256-GCM mode in cryptojs

Viewed 41

i am using cryptojs but it doesn't contain mode

AES-256-GCM

so is there any way to use AES-256-GCM in cryptojs? if no any alternative pure js library that will run in frontend for the same ?.

i tried CryptoJS.mode.CTRGladman and CryptoJS.mode.CTR but it gives wrong output

php code

$simple_string = "mytext";
$ciphering = "aes-128-gcm";
$options = 0;
$encryption_iv = '0123456789012345';
$encryption_key = "password";
$tag = null;
$encryption = openssl_encrypt($simple_string, $ciphering, $encryption_key, $options, $encryption_iv, $tag);

echo "Encrypted String: " . $encryption . " <br>"; //  o/p  kETIUq7k

js code

function encrypt(msg, pass, iv) {
    pass = CryptoJS.enc.Utf8.parse(pass.padEnd(32, '\0'));
    iv = CryptoJS.enc.Utf8.parse(iv);
    var encrypted = CryptoJS.AES.encrypt(msg, pass, { iv: iv, keySize: 256, mode: CryptoJS.mode.CTR, });
    var decrypted = CryptoJS.AES.decrypt(encrypted, pass, { iv: iv, keySize: 256, mode: CryptoJS.mode.CTR, });

    console.log(encrypted.toString()); // si/06lfF/ShIVT+0kNlqCA==
    console.log(decrypted.toString(CryptoJS.enc.Utf8)); // mytext
}
function encrypt2(msg, pass, iv) {
    pass = CryptoJS.enc.Utf8.parse(pass.padEnd(32, '\0'));
    iv = CryptoJS.enc.Utf8.parse(iv);
    var encrypted = CryptoJS.AES.encrypt(msg, pass, { iv: iv, keySize: 256, mode: CryptoJS.mode.CTRGladman, });
    var decrypted = CryptoJS.AES.decrypt(encrypted, pass, { iv: iv, keySize: 256, mode: CryptoJS.mode.CTRGladman, });

    console.log(encrypted.toString()); // KhM3efyU+wsVn8BURRSBDQ==
    console.log(decrypted.toString(CryptoJS.enc.Utf8)); // mytext
}
encrypt("mytext", "password", "0123456789012345");
encrypt2("mytext", "password", "0123456789012345");
0 Answers
Related