Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java AES如何使用不同的IV解密?_Java_Encryption_Cryptography - Fatal编程技术网

Java AES如何使用不同的IV解密?

Java AES如何使用不同的IV解密?,java,encryption,cryptography,Java,Encryption,Cryptography,我正在努力学习更多关于AES加密的知识。AES加密使用密钥和(IV)进行加密,但由于每个IV都不同,AES如何解密密文并返回明文 public static byte[]encrypt_cbc(SecretKey skey,字符串明文){ /*前提条件:skey有效;否则将抛出IllegalStateException*/ 试一试{ 字节[]密文=空; Cipher Cipher=Cipher.getInstance(“AES/CBC/PKCS5Padding”); final i

我正在努力学习更多关于AES加密的知识。AES加密使用密钥和(IV)进行加密,但由于每个IV都不同,AES如何解密密文并返回明文

public static byte[]encrypt_cbc(SecretKey skey,字符串明文){
/*前提条件:skey有效;否则将抛出IllegalStateException*/
试一试{
字节[]密文=空;
Cipher Cipher=Cipher.getInstance(“AES/CBC/PKCS5Padding”);
final int blockSize=cipher.getBlockSize();
byte[]initVector=新字节[blockSize];
(新SecureRandom()).nextBytes(initVector);
IvParameterSpec ivSpec=新的IvParameterSpec(初始向量);
cipher.init(cipher.ENCRYPT_模式,skey,ivSpec);
byte[]encoded=plaintext.getBytes(java.nio.charset.StandardCharsets.UTF_8);
ciphertext=新字节[initVector.length+cipher.getOutputSize(encoded.length)];
对于(int i=0;i
通常随机IV-CBC需要一个不可预测的IV-在密文前加上前缀并在解密前“删除”。我在引号中加了remove,因为删除它就像复制它一样,然后跳过它。原则上,它可以放在密文附近的任何地方。CBC模式的IV等于基础密码的块大小(
cipher#getBlockSize()
),即AES为16字节,因此大小是预先知道的

静脉注射不需要对攻击者保密

通常,IV的类型和安全性取决于加密模式。对于CBC,它需要是不可预测的,对于CTR(计数器模式),它不应该与另一个计数器值重叠,对于GCM,它需要是一个12字节的nonce


还有其他共享IV的方法。例如,对于CBC,可以在两侧保留一个计数器,并对该计数器进行加密以形成IV(当然,在将其编码为16字节后)。这样,IV就不需要包含在密文中。

我不明白的是,您展示的代码片段实际上已经在IV前面加了前缀,这是什么?我一直在阅读关于IV和key之间的区别。通常,如果重复使用同一密钥进行加密,建议使用不同的IV。那么,使用不同的IV如何帮助防止密文被破译呢?通常不会。第四部分主要是使密码具有非确定性;如果你想重复(部分)一条消息,你不想拥有与泄露信息相同的密文。重复的信息很常见。一个可能的例外是SIV(Synthetic IV)模式,其中IV兼作身份验证标签。因此,即使不解密密文,重用IV也会泄漏信息;在那里,重复使用IV几乎完全破坏了密码。密钥大小为128和256位的高级加密标准(AES)。对于流量,AES应与低带宽流量的计数器模式(CTR)或高带宽流量的Galois/计数器模式(GCM)操作模式(参见分组密码操作模式)一起使用–对称加密参考:嗯,我认识到我的异常处理,我想,但我永远不会在方法名中使用下划线,也不会删除catch块中的整个stacktrace。或者偶尔使用
final
。或者将
null
分配给密文。或者仍然使用CBC。当涉及到代码实践时,并不是一个很好的例子。可能会更糟@你可以学习任何东西“AES如何用不同的IV解密?”-你能举一个例子吗?对于一次加密/解密,IV必须相同-cycle@ArtjomB. 如果这就是它的意思,那么它可能是前16个字节有点意义纯粹是因为运气,或者因为IV只是部分不同(它与纯文本是异或的,所以)。如果IV不同,则仅第一个块受影响。@ArtjomB。byte[]initVector=新字节[blockSize];(新SecureRandom()).nextBytes(initVector);IvParameterSpec ivSpec=新的IvParameterSpec(初始向量);cipher.init(cipher.ENCRYPT_模式,skey,ivSpec)@阿乔姆。new SecureRandom()不会生成唯一的IV吗?
public static byte[] encrypt_cbc(SecretKey skey, String plaintext) {
    /* Precondition: skey is valid; otherwise IllegalStateException will be thrown. */
    try {
        byte[] ciphertext = null;
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        final int blockSize = cipher.getBlockSize();
        byte[] initVector = new byte[blockSize];
        (new SecureRandom()).nextBytes(initVector);
        IvParameterSpec ivSpec = new IvParameterSpec(initVector);
        cipher.init(Cipher.ENCRYPT_MODE, skey, ivSpec);
        byte[] encoded = plaintext.getBytes(java.nio.charset.StandardCharsets.UTF_8);
        ciphertext = new byte[initVector.length + cipher.getOutputSize(encoded.length)];
        for (int i=0; i < initVector.length; i++) {
            ciphertext[i] = initVector[i];
        }
        // Perform encryption
        cipher.doFinal(encoded, 0, encoded.length, ciphertext, initVector.length);
        return ciphertext;
    } catch (NoSuchPaddingException | InvalidAlgorithmParameterException | ShortBufferException |
        BadPaddingException | IllegalBlockSizeException | InvalidKeyException | NoSuchAlgorithmException e)
    {
        /* None of these exceptions should be possible if the precondition is met. */
        throw new IllegalStateException(e.toString());
    }
}

public static String decrypt_cbc(SecretKey skey, byte[] ciphertext)
    throws BadPaddingException, IllegalBlockSizeException /* These indicate corrupt or malicious ciphertext */
{
    try {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        final int blockSize = cipher.getBlockSize();
        byte[] initVector = Arrays.copyOfRange(ciphertext, 0, blockSize);
        IvParameterSpec ivSpec = new IvParameterSpec(initVector);
        cipher.init(Cipher.DECRYPT_MODE, skey, ivSpec);
        byte[] plaintext = cipher.doFinal(ciphertext, blockSize, ciphertext.length - blockSize);
        return new String(plaintext);
    } catch (NoSuchPaddingException | InvalidAlgorithmParameterException |
        InvalidKeyException | NoSuchAlgorithmException e)
    {
        /* None of these exceptions should be possible if precond is met. */
        throw new IllegalStateException(e.toString());
    }
}