Click here to Skip to main content
15,891,184 members
Home / Discussions / Java
   

Java

 
AnswerRe: Java Scanner Pin
Richard MacCutchan5-Jun-13 23:34
mveRichard MacCutchan5-Jun-13 23:34 
QuestionHow to send Email using VB 6.0 Pin
Saranya-from-Tamil-Nadu-India4-Jun-13 1:50
Saranya-from-Tamil-Nadu-India4-Jun-13 1:50 
SuggestionRe: How to send Email using VB 6.0 Pin
AlphaDeltaTheta4-Jun-13 5:36
AlphaDeltaTheta4-Jun-13 5:36 
QuestionParallel processing of two methods in main class Pin
Sachin k Rajput 1-Jun-13 0:30
Sachin k Rajput 1-Jun-13 0:30 
AnswerRe: Parallel processing of two methods in main class Pin
Richard MacCutchan1-Jun-13 4:28
mveRichard MacCutchan1-Jun-13 4:28 
QuestionPut and order values in a List Pin
Ghandil31-May-13 8:36
Ghandil31-May-13 8:36 
AnswerRe: Put and order values in a List Pin
Prasad Khandekar3-Jun-13 3:45
professionalPrasad Khandekar3-Jun-13 3:45 
QuestionCipherInputStream.read(byte[]) Dropping Final Block Pin
Skippums31-May-13 6:40
Skippums31-May-13 6:40 
I am writing test code that prints an encrypted string to an underlying ByteArrayOutputStream, then code to decrypt the bytes from that stream into another string. Here is my code:
Java
byte[] keyBytes, ivBytes;
// Populate keyBytes and ivBytes with byte arrays of length 16 bytes (128 bits)

final SecretKey key = new SecretKeySpec(keyBytes, "AES");
final IvParameterSpec iv = new IvParameterSpec(ivBytes);

final Cipher encrypt = Cipher.getInstance("AES/CBC/PKCS5Padding");
final Cipher decrypt = Cipher.getInstance("AES/CBC/PKCS5Padding");

encrypt.init(Cipher.ENCRYPT_MODE, key, iv);
decrypt.init(Cipher.DECRYPT_MODE, key, iv);

final ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
final CipherOutputStream out = new CipherOutputStream(outBytes, encrypt);

out.write("This is a test, of course.".getBytes());
out.close();

final byte[] encryptedBytes = outBytes.toByteArray();
final ByteArrayInputStream inBytes = new ByteArrayInputStream(encryptedBytes);
final CipherInputStream in = new CipherInputStream(inBytes, decrypt);

final byte[] result = new byte[100];
final int byteCount = in.read(result);

System.out.print('"');
System.out.print(new String(result, 0, byteCount));
System.out.println('"');

For the above code, I expected the output to be "This is a test, of course.". However, I am getting the string, "This is a test, " (the final block is not being read). If I continue calling in.read(), I am able to get the remaining bytes out of the input stream, one byte at a time. This indicates (I think) that CipherInputStream is not simply catching and ignoring an invalid padding exception, which is what a few other posts I have found mentioned might be happening.
When I run the above code, the following two properties hold:
1. encryptedBytes.length == 32,
2. new String(decrypt.doFinal(encryptedBytes)) creates a string with the expected value

My question is, how in the heck to I get the CipherInputStream to read these bytes, without reading them one at a time? Is this expected behavior?
Sounds like somebody's got a case of the Mondays

-Jeff

SuggestionRe: CipherInputStream.read(byte[]) Dropping Final Block Pin
AlphaDeltaTheta31-May-13 21:51
AlphaDeltaTheta31-May-13 21:51 
AnswerRe: CipherInputStream.read(byte[]) Dropping Final Block Pin
AlphaDeltaTheta31-May-13 22:06
AlphaDeltaTheta31-May-13 22:06 
GeneralRe: CipherInputStream.read(byte[]) Dropping Final Block Pin
Skippums3-Jun-13 7:26
Skippums3-Jun-13 7:26 
AnswerRe: CipherInputStream.read(byte[]) Dropping Final Block Pin
Skippums3-Jun-13 7:33
Skippums3-Jun-13 7:33 
SuggestionRe: CipherInputStream.read(byte[]) Dropping Final Block Pin
AlphaDeltaTheta3-Jun-13 17:05
AlphaDeltaTheta3-Jun-13 17:05 
GeneralRe: CipherInputStream.read(byte[]) Dropping Final Block Pin
Skippums4-Jun-13 4:47
Skippums4-Jun-13 4:47 
AnswerRe: CipherInputStream.read(byte[]) Dropping Final Block Pin
AlphaDeltaTheta4-Jun-13 5:30
AlphaDeltaTheta4-Jun-13 5:30 
GeneralRe: CipherInputStream.read(byte[]) Dropping Final Block Pin
Skippums4-Jun-13 6:33
Skippums4-Jun-13 6:33 
AnswerRe: CipherInputStream.read(byte[]) Dropping Final Block Pin
AlphaDeltaTheta4-Jun-13 6:41
AlphaDeltaTheta4-Jun-13 6:41 
QuestionNew in Java Pin
Garde33330-May-13 18:47
Garde33330-May-13 18:47 
QuestionRe: New in Java Pin
AlphaDeltaTheta30-May-13 20:42
AlphaDeltaTheta30-May-13 20:42 
AnswerRe: New in Java Pin
Garde33331-May-13 17:57
Garde33331-May-13 17:57 
AnswerRe: New in Java Pin
dusty_dex30-May-13 20:56
dusty_dex30-May-13 20:56 
AnswerRe: New in Java Pin
Richard MacCutchan30-May-13 21:18
mveRichard MacCutchan30-May-13 21:18 
GeneralRe: New in Java Pin
Garde33331-May-13 17:45
Garde33331-May-13 17:45 
GeneralRe: New in Java Pin
Richard MacCutchan31-May-13 21:41
mveRichard MacCutchan31-May-13 21:41 
GeneralRe: New in Java Pin
Pujara Jignesh2-Jun-13 0:12
Pujara Jignesh2-Jun-13 0:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.