简体   繁体   English

需要javascript中的AES解密

[英]need AES Decryption in javascript

I have the following code for decryption in java, I want that to be implemented in angular4. 我有以下用于解密的Java代码,我希望在angular4中实现。

public synchronized InputStream getInputStream(String src) {
    KeyEntry entry = keysMap.get(src);
    try {
        String destPath = rootFolder + "/" + entry.destination;
        FileInputStream is = new FileInputStream(destPath);
        if (entry.key.isEmpty()) return is;
        byte[] encKey = Base64.decode(entry.key, Base64.DEFAULT);
        SecretKeySpec secretKeySpec = new SecretKeySpec(encKey, AES_ALGORITHM);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(encKey);
        Cipher cipher = Cipher.getInstance(AES_TRANSFORMATION);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
        return new CipherInputStream(is, cipher);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

currently doing it, something like below, not working 目前正在这样做,如下所示,无法正常工作

decryptContent(ciphertext, base64Key) {
    const key = CryptoJS.enc.Base64.parse(base64Key);

    const decryptedData = CryptoJS.AES.decrypt( ciphertext, key, {
        iv: CryptoJS.lib.WordArray.random(128 / 8),
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.NoPadding
    });

    const decryptedText = decryptedData.toString( CryptoJS.enc.Utf8 );    
}

AES_TRANSFORMATION = "AES/CFB8/NoPadding"; AES_TRANSFORMATION =“ AES / CFB8 / NoPadding”;

Then you need to use CFB mode as well for decryption 然后您还需要使用CFB模式进行解密

 mode: CryptoJS.mode.CFB

I don't understand how to get the IV in javascript as in java, that is where I am majorly struck 我不明白如何像Java一样在javascript中获得IV,这就是我最受打击的地方

IvParameterSpec ivParameterSpec = new IvParameterSpec(encKey);

In Java the encryption key is used as IV as well. 在Java中,加密密钥也用作IV。 To decrypt the data you should use the key as IV too. 要解密数据,您也应该将密钥用作IV。

Please note using key as IV is bad practice and it may create security weakness. 请注意,将密钥用作IV是不好的做法,它可能会造成安全漏洞。 IV allows reusing the same key for multiple encryptions without creating an opening for the "two-time pad attack". IV允许对多个加密重用相同的密钥,而无需为“两次填充攻击”创造机会。 If the key is reused for multiple encryptions, IV needs to be unique for each encryption for the same key. 如果密钥被重复用于多种加密,则对于同一密钥的每种加密,IV必须是唯一的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM