Ionut Bogdan
Ionut Bogdan

Reputation: 249

JAVA - Decipher "DES" encrypted random number

I would like to know how i could decipher a random number DES/CBC-enciphered with a specific Key.

My protocol states the following: I am sending a KeyNo (eg. 0x00) After the KeyNo is sent i get a 8byte (DES) random number. This random number is enciphered with the selected key.

My question would be how do i decipher the data i receive, to find the random number using Cipher

Thank you.

Upvotes: 1

Views: 566

Answers (1)

Denys Séguret
Denys Séguret

Reputation: 382404

To decrypt a DES encrypted stream, simply do :

Key key = SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(bytesOfThe Key)); // bytesOfTheKey should be 8 bytes long
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
return new CipherInputStream(inputStream, cipher);

You may also be interested in the doFinal method which works on byteBuffers.

Upvotes: 3

Related Questions