Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Other Java > BOOK: Beginning Cryptography with Java
|
BOOK: Beginning Cryptography with Java
This is the forum to discuss the Wrox book Beginning Cryptography with Java by David Hook; ISBN: 9780764596339
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Cryptography with Java section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 4th, 2011, 08:03 PM
Authorized User
 
Join Date: Jun 2011
Posts: 38
Thanks: 9
Thanked 0 Times in 0 Posts
Default Exception in thread "main" javax.crypto.BadPadd?ingException: pad block corrupted

Why I am receiving this exception? How it is possible to encrypt with different key and decrypt with different key in Symmetric algorithm?

package chatper2symmertickeycryptography;

import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
* Example of using PBE with a PBEParameterSpec
*/
public class PBEWithParamsExample {
public static void main(String[] args) throws Exception
{
byte[] input = new byte[] {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
byte[] keyBytes = new byte[] {
0x73, 0x2f, 0x2d, 0x33, (byte)0xc8, 0x01, 0x73,
0x2b, 0x72, 0x06, 0x75, 0x6c, (byte)0xbd, 0x44,
(byte)0xf9, (byte)0xcl, (byte)0xc1, 0x03, (byte)0xdd,
(byte)0xd9, 0x7c, 0x7c, (byte)0xbe, (byte)0x8e };
byte[] ivBytes = new byte[] {
(byte)0xb0, 0x7b, (byte)0xf5, 0x22, (byte)0xc8,
(byte)0xd6, 0x08, (byte)0xb8 };

// encrypt the data using precalculated keys
Cipher cEnc = Cipher.getInstance ("DESede/CBC/PKCS7Padding", "BC");
cEnc.init (Cipher.ENCRYPT_MODE,
new SecretKeySpec(keyBytes, "DESede"),

new IvParameterSpec(ivBytes));
byte[] out =cEnc. doFinal(input);
// decrypt the data using PBE
char[] password = "password".toCharArray();
byte[] salt = new byte[] {
0x7d, 0x60, 0x43, 0x5f,
0x02, (byte) 0xe9, (byte)0xe0, (byte)0xae };
int iterationCount = 2048;
PBEKeySpec pbeSpec = new PBEKeySpec(password);
SecretKeyFactory keyFact =
SecretKeyFactory.getInstance("PBEWithSHAAnd3KeyTri pleDES", "BC");
Cipher cDec = Cipher.getInstance ("PBEWithSHAAnd3KeyTripleDES", "BC");
Key sKey = keyFact.generateSecret(pbeSpec);
cDec.init(Cipher.DECRYPT_MODE,
sKey, new PBEParameterSpec(salt, iterationCount));
System.out.println ("cipher : " + Utils.toHex(out)) ;
System.out.println ("gen key: " + Utils.toHex(sKey.getEncoded()));
System.out.println ("gen iv : " + Utils.toHex(cDec.getIV()));
System.out.println ("plain : " + Utils. toHex(cDec.doFinal (out) )) ;
}
}
 
Old June 4th, 2011, 08:28 PM
Authorized User
 
Join Date: Jun 2011
Posts: 38
Thanks: 9
Thanked 0 Times in 0 Posts
Default

I resolved the code issue by downloading and running the latest code.
But how come it is possible to encrypt and decrypt using different keys in Symmetric algorithm?
 
Old June 4th, 2011, 08:35 PM
Authorized User
 
Join Date: Jun 2011
Posts: 38
Thanks: 9
Thanked 0 Times in 0 Posts
Default

Unfortunately I cannot understand the description given here in the book-
I have very sound understanding of java programming language. Then why does the below statement makes no sense to me?

How It Works
When the preprocessed key is passed to the Cipher.init() method, the extra information in the PBEParameterSpec object is used and the password is mixed into a password generation function and used to create the actual key used for encryption. As you will see in the next section, the actual key generated bears no apparent resemblance to the password that was used to create it.
This need for the PBEParameterSpec can be unfortunate because it means, in the event you need to pass it on to an outside application, you have no way of working out what the actual key used by the PBE is. The newer method of using PBEKeySpec addresses this drawback, but first it would be worth having a look at the classes introduced by the example in more detail.
 
Old June 4th, 2011, 11:37 PM
dgh dgh is offline
Wrox Author
 
Join Date: Aug 2005
Posts: 206
Thanks: 0
Thanked 20 Times in 20 Posts
Default

The concepts of cryptography can take a bit of time to get your head around if they are not familiar, as such, if you are new to the area, you need to read the introductory material associated with the examples carefully first. The "How It Works" sections of the book are written on the basis that this material has been read. In this case you will find the detail on pages 43 to 45.

Regards,

David
The Following User Says Thank You to dgh For This Useful Post:
boy18nj (June 5th, 2011)
 
Old June 5th, 2011, 09:10 AM
Authorized User
 
Join Date: Jun 2011
Posts: 38
Thanks: 9
Thanked 0 Times in 0 Posts
Default

Thanks for reply.

But how come is it possible to encrypt and decrypt using different keys in Symmetric algorithm? Unless the encrypting key bytes are equivalent to decrypting String "password" key.
 
Old June 5th, 2011, 07:44 PM
dgh dgh is offline
Wrox Author
 
Join Date: Aug 2005
Posts: 206
Thanks: 0
Thanked 20 Times in 20 Posts
Default

I'm sorry but you will need to clarify your question. I do not understand it.

Thanks,

David
 
Old June 5th, 2011, 09:27 PM
Authorized User
 
Join Date: Jun 2011
Posts: 38
Thanks: 9
Thanked 0 Times in 0 Posts
Default

Sorry if I'm not clear in my question.

When encrypting, keyBytes is used, and when decrypting sKey is used.
keyBytes and sKey are totally different. Still how come, the decryption worked perfect? As per my understanding, the key used to encrypt should be used to decrypt also(Symmetric Algorithm).
Code:
cEnc.init (Cipher.ENCRYPT_MODE,
new SecretKeySpec(keyBytes, "DESede"),

cDec.init(Cipher.DECRYPT_MODE,
sKey, new PBEParameterSpec(salt, iterationCount));
 
Old June 5th, 2011, 09:47 PM
dgh dgh is offline
Wrox Author
 
Join Date: Aug 2005
Posts: 206
Thanks: 0
Thanked 20 Times in 20 Posts
Default

Ah, okay. The PBE algorithm calculates a set of bytes based on the salt, the iteration count and the password.

In this case it generates the same values as in keyBytes, so the key ends up being the same. They are not different symmetric keys. Demonstrating this is actually the point of the example.

Regards,

David
 
Old June 5th, 2011, 10:10 PM
Authorized User
 
Join Date: Jun 2011
Posts: 38
Thanks: 9
Thanked 0 Times in 0 Posts
Default

Thanks for reply.

I think you hacked here, to produce the same decryption key using password, salt and iterationCount.

Since I don't know the internal working of sKey and PBEPArameterSpec passed parameters, how they are manipulated in order to satisfy the decryption.

Code:
Key sKey = keyFact.generateSecret(pbeSpec);
cDec.init(Cipher.DECRYPT_MODE,
sKey, new PBEParameterSpec(salt, iterationCount));
I'm doomed to ask, How did you calculate or came out with password value, salt value and iteration count? I guess you must have done some calculations using the password, salt and iterationCount which results in same value as keyBytes.

Code:
char[] password = "password".toCharArray();
byte[] salt = new byte[] {
0x7d, 0x60, 0x43, 0x5f,
0x02, (byte) 0xe9, (byte)0xe0, (byte)0xae };
int iterationCount = 2048;
 
Old June 6th, 2011, 12:50 AM
dgh dgh is offline
Wrox Author
 
Join Date: Aug 2005
Posts: 206
Thanks: 0
Thanked 20 Times in 20 Posts
Default

As mentioned on page 43, the details of the scheme are found in PKCS#5. There are references for this in the bibliography at the end of the book.

Regards,

David





Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception error "Collection was modified; enumeration operation may not execute" caof2005 BOOK: Professional ASP.NET MVC 2 1 April 6th, 2011 07:26 PM
MSXSL gives error message for "for" inside "select" ilyaz XSLT 1 December 9th, 2010 05:02 PM
How to theme the "Browse" button of "FileUpload" control? varunbwj BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 October 14th, 2009 01:22 AM
Add a CheckBox DataColumn to my DataGridView, Null format: "" or "True" but Error: F ismailc C# 2005 0 September 25th, 2009 04:56 AM
Exception in thread "main" javax.xml.transform.Tra XSLTUser XSLT 1 December 29th, 2007 04:59 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.