cipher.update在Java中做什么? [英] What does cipher.update do in java?

查看:538
本文介绍了cipher.update在Java中做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实施DES-CBC。我对 cipher.init cipher.update cipher.dofinal 做。我只是使用init来设置密钥,并使用 dofinal 来获取结果。我不使用更新。

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?

使用 UTF-8 ASCII 编码时,结果有何不同?

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

这是我的代码:

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 [] ) 说(部分强调),

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.

输入缓冲区中的字节以及可能在输入过程中被缓冲的所有输入字节处理先前的更新操作,并使用填充(如果需要)。如果正在使用AEAD模式(例如GCM / CCM),则在加密的情况下会添加身份验证标签,而在解密的情况下会验证身份标签。结果存储在新缓冲区中。

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.

这篇关于cipher.update在Java中做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆