gpt4 book ai didi

java - cipher.update在java中做什么?

转载 作者:太空宇宙 更新时间:2023-11-04 06:25:43 33 4
gpt4 key购买 nike

我正在实现 DES - CBC。我对 cipher.init 、 cipher.update 和 cipher.dofinal 的作用感到困惑。我只是使用 init 来设置 key 并使用 dofinal 来获取结果。我不使用更新。这是正确的吗?

另外,使用UTF-8ASCII编码时的结果有什么区别?

这是我的代码:

byte[] ciphertext;

Cipher enc = Cipher.getInstance("DES/CBC/PKCS5Padding");

enc.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "DES"), new IvParameterSpec(vector));

// Is this the complete ciphertext?
ciphertext = encrypt.doFinal(data.getbytes("UTF-8"));

最佳答案

Cipher.doFinal(byte[]) 的 Javadoc说(部分强调),

Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation. The data is encrypted or decrypted, depending on how this cipher was initialized.

The bytes in the input buffer, and any input bytes that may have been buffered during a previous update operation, are processed, with padding (if requested) being applied. If an AEAD mode such as GCM/CCM is being used, the authentication tag is appended in the case of encryption, or verified in the case of decryption. The result is stored in a new buffer.

这样做是为了让您不必将文件的全部内容(例如)读入内存来对其进行加密。

关于java - cipher.update在java中做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26824262/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com