CipherInputStream/CipherOutputStream



Hi!

Im currently struggeling with encryption of streams using
CipherInputStream and CipherOutputStream. For some reason, the last 8
bytes when reading encrypted data seem to be missing (see example
below). Anyone have any idea why this occurs??

public void testCipherStreams() throws Exception {
int count = 80;
byte[] key = new byte[]{1, 2, 3, 4, 5, 6, 7, 8};
Key serverEncKey =
SecretKeyFactory.getInstance("DES").generateSecret(new
DESKeySpec(key));

// Create Ciphers
Cipher encCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
encCipher.init(Cipher.ENCRYPT_MODE, serverEncKey);
Cipher decCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
decCipher.init(Cipher.DECRYPT_MODE, serverEncKey);

// Create encryption-stream
ByteArrayOutputStream output = new ByteArrayOutputStream();
OutputStream encryptedOutput = new CipherOutputStream(output,
encCipher);

// Create a random buffer
Random rand = new Random();
byte[] buf = new byte[count];
rand.nextBytes(buf);

// Run it through the stream
encryptedOutput.write(buf);
encryptedOutput.flush();
output.flush();
byte[] encrypted = output.toByteArray();

// Create decryption-stream
ByteArrayInputStream input = new ByteArrayInputStream(encrypted);
InputStream decryptedInput = new CipherInputStream(input,
decCipher);

// Get decrypted output
byte[] decrypted = new byte[count];
decryptedInput.read(decrypted);
// Compare!
for (int i=0; i < count; i++) {
assertEquals("byte " + i + " did not match", buf[i], decrypted[i]);
}
}


Thx =)

.



Relevant Pages

  • Re: Decrypt Filestream of MS Word Password protected File
    ... I am no expert in this, but, in theory, if you break the file into individual streams you can determine the encryption provider used and may be able to decrypt individual streams and put the document back together. ... Is it possible to create a filestream of the word document and then ... MS Word file without password protection. ...
    (microsoft.public.word.docmanagement)
  • Re: Rijndael class, Padding bug?, Ivan Medvedev
    ... You'll need to call TransformFinalBlock for the final block of the ... The streams are nice as they handle of this for you. ... > CreateDecryptor methods on the Rijndael class and used the TransformBlock ... Encryption works fine. ...
    (microsoft.public.dotnet.security)
  • Re: encryption question
    ... one big lesson I learned about public key encryption is that it is ... very slow and basically unusable for large streams, ...
    (borland.public.delphi.thirdpartytools.general)