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

Problem in encrypting/decrypting multiple values at the same time #47

Open
alizarei95 opened this issue Jun 5, 2024 · 0 comments
Open

Comments

@alizarei95
Copy link

alizarei95 commented Jun 5, 2024

Hi,
When using library in single thread, everything works well but when I'm using it in multi thread condition, results are not expected.
In both encryption and decryption I'm getting wrong results.
Is there any workaround?

import {AES} from "../libs/crypto-es/aes.js";
import {Hex, WordArray} from "../libs/crypto-es/core.js";
import * as CryptoESCore from "../libs/crypto-es/cipher-core.js";
import {NoPadding} from "../libs/crypto-es/pad-nopadding.js";
import {ZeroPadding} from "../libs/crypto-es/pad-zeropadding.js";

/**
 * Encrypt data using AES/CBC/ZEROPADDING
 * @param key {ArrayBuffer} Bytes of encryption key
 * @param iv {ArrayBuffer} Bytes of iv
 * @param content {ArrayBuffer} Bytes of plain content
 * @returns {string} Encrypted content in hex format
 */
export function encryptAES_CBC_ZEROPADDING(key, iv, content) {
    return AES.encrypt(WordArray.create(content), WordArray.create(key), {
        iv: WordArray.create(iv),
        mode: CryptoESCore.CBC,
        padding: ZeroPadding
    }).ciphertext.toString()
}

/**
 * Decrypt data using AES/CBC/ZEROPADDING
 * @param key {ArrayBuffer} Bytes of decryption key
 * @param iv {ArrayBuffer} Bytes of iv
 * @param cipherHex {string} Cipher text in HEX format
 * @return {string} Decrypted content in hex format
 */
export function decryptAES_CBC_ZEROPADDING(key, iv, cipherHex) {
    let cipherParams = CryptoESCore.CipherParams.create({
        ciphertext: Hex.parse(cipherHex)
    })
    return AES.decrypt(cipherParams, WordArray.create(key), {
        iv: WordArray.create(iv),
        mode: CryptoESCore.CBC,
        padding: ZeroPadding
    }).toString()
}
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