What does cipher.update do in java?

一个人想着一个人 提交于 2020-01-13 06:42:48

问题


I am implementing DES - CBC. I am confused as to what cipher.init, cipher.update and cipher.dofinal do. I just use init to set the key and dofinal to get the result. I don't use update. Is that correct?

Also whats the difference to the result when using UTF-8 and ASCII encodings?

Here is my code:

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"));

回答1:


The Javadoc for Cipher.doFinal(byte[]) says (in part with emphasis added),

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.

This is done so you don't have to read all of a file (for example) into memory in order to encrypt it.



来源:https://stackoverflow.com/questions/26824262/what-does-cipher-update-do-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!