Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Cipher object reusable?

Can I use the same Cipher object across multiple methods since the method arguments to getInstance and init do not change?

For example, assume multiple parts of the application use the decrypt method in a utility class. All the encrypted values passed are generated using the same key and algorithm. So, can I reuse the same Cipher object?

Is it really worth worrying about multiple creations of Cipher (which may result in creation of KeySpec, SecretKey objects)?

like image 542
Firefox Avatar asked Jan 27 '11 12:01

Firefox


People also ask

What does cipher DoFinal return?

Returns the length in bytes that an output buffer would need to be in order to hold the result of the next update or doFinal operation, given the input length inputLen (in bytes). AlgorithmParameters. getParameters() Returns the parameters used with this cipher. Provider.

What is cipher DoFinal?

DoFinal(ByteBuffer, ByteBuffer) Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation. DoFinal(Byte[], Int32) Finishes a multiple-part encryption or decryption operation, depending on how this cipher was initialized.

Which of the following method of the Cipher class completes the encryption operation?

Encrypting and Decrypting Data Example After initializing the Cipher object, we call the doFinal() method to perform the encryption or decryption operation. This method returns a byte array containing the encrypted or decrypted message.


1 Answers

Yes.

As mentioned by the documentation:

Upon finishing, this method resets this cipher object to the state it was in when previously initialized via a call to init. That is, the object is reset and available to encrypt or decrypt (depending on the operation mode that was specified in the call to init) more data.

like image 96
Vic Seedoubleyew Avatar answered Sep 18 '22 18:09

Vic Seedoubleyew