• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

IllegalBlockSizeException while decrypting using Triple DES

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


While decrypting a message , which is alreay encrpted using TripleDES in .net , using Triple DES an
IllegalBlockSizeException is thrown.


javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
at com.sun.crypto.provider.SunJCE_h.b(DashoA12275)

And the key is encrpted using tripleDES as well on both encrpting j.net piece and decrypting java piece.

Any thoughts on this?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand how you would use an encrypted key to decrypt a message, but have you checked whether the input length is a multiple of 8?
 
Velayudhaperumal bhagavathikannu
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No the input length is not a multiple of 8.
 
Velayudhaperumal bhagavathikannu
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The input length must not be a multiple of 8.How do i encrpt this messge using triplpe des. My code is,

cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
final IvParameterSpec iv = new IvParameterSpec(new byte[]);
cipher.init(Cipher.DECRYPT_MODE, key, iv);
 
Velayudhaperumal bhagavathikannu
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is the handling of the input msg and is solved...
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am having the exact same problem and trying to figure out what is going wrong. Could you please explain or post how you solved it? Should the input string (encrypted string for decrypting) be "handled" in the sense padded up?

how do I pad up an encrypted string before decryption? Is it a problem with the key padding or the string length.

Thanks
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raghu Sitarama wrote:
I am having the exact same problem and trying to figure out what is going wrong.



You mean you are also getting an IllegalBlockSizeException.

Could you please explain or post how you solved it?



The solution always comes from finding out why you have and illegal block size. The chances are the problem is one of following-

a) You are using the wrong algorithm for decrypt.
b) You are trying to decrypt HEX or Base64 encoded ciphertext without first removing the encoding.
c) Your ciphertext is corrupt.

but working blind as we are makes a definitive list difficult to create.

Should the input string (encrypted string for decrypting) be "handled" in the sense padded up?
how do I pad up an encrypted string before decryption? Is it a problem with the key padding or the string length.



Cleartext padding is applied before encryption and removed after basic decryption to adjust the cleartext length to meet the requirements of the cipher algorithm being use.. Key padding is not a term I recognise.

For any more specific help you are going to need to show your encryption and decryption code (Java, .NET, PHP, Python etc etc etc). The detail matters.
 
Raghu Sitarama
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks James.

I am sorry, if I was cryptic there.

I am writing my code in java.

I am having basically 2 problems. one is key generation and the second one is getting the desede to work (decrypt an encrypted string). It is a Password based encryption (I think you can call it so). Basically there is a plain text string (like a password) that the calling application and my application knows. the calling application uses the plain text string and generates a 168 bit key and then uses that to to do a DESede/CBC/PKCS5Padding encryption and send that encrypted information over and my application using the same plain text string should generate my key and then use that to decrypt that information and use that information for further processing.

problem 1: key generation. The other application is a windows / .net based and they are using CryptDeriveKey function from Microsoft based provider. My application is a java servlet and Oracle App Server (so a fully java env). possibly we have the sun based provider. So, I did a lot of study and finally did my own implementation of CryptDeriveKey in java using the same algorithm but implemented in java. (basically takes the SHA-1 digest of the plain text string and then do the array filling with particular bits with XOR bit operation etc. etc.) I am not getting into the details of that here for brevity. But got a 168 bit (21 bytes) key as desired.

Now my first problem here was getting that 168 bit key to work with my DESede code. it would complain as InvalidKeyException. because it was expecting 24 bytes (I don't know why) but neglecting the parity bits. But because, I am not using the standard key generator and defining my own key and defining a keyspec etc. there was some issue there and it was complaining. so, I was told to massage the key and add the parity bits. I then wrote the method to carefully add the 8th parity bits (luckily some gracious good programmer had put up a nice code to add this for DES keys on the net). Now, the key is going through, but the cipher do final is creating problems. I am pasting my code for the key generation as well as for the decryption.

problem 2: decryption. I am having problems here and would love help. I am attaching the code below. the encrypted string is some funky looking string with characters of all shapes how do I do the padding of it? also, I have already specified PKCS5Padding in the algorithm spec. isn't that supposed to do the padding? why is it expecting the input string to be exactly 8 byte blocks? now, I tried the cipher.update() method and did a println before I did the cipher.doFinal() as the doFinal was what was throwing the IllegalBlockSizeException. but could not figure if the intermediate output was ok. Basically need help here.


Key generation code:




Decryption code:

 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raghu Sitarama wrote:Thanks James.

I am sorry, if I was cryptic there.

I am writing my code in java.

I am having basically 2 problems. one is key generation and the second one is getting the desede to work (decrypt an encrypted string). It is a Password based encryption (I think you can call it so). Basically there is a plain text string (like a password) that the calling application and my application knows. the calling application uses the plain text string and generates a 168 bit key and then uses that to to do a DESede/CBC/PKCS5Padding encryption and send that encrypted information over and my application using the same plain text string should generate my key and then use that to decrypt that information and use that information for further processing.



A DESede Java key is packed as 24 bytes with the least significant bit of each byte (the parity bit) being unused. If one generates such a 24 byte key with the parity bit set to any value then it is accepted but when one subsequently extracts the bytes of the key using the Secretkey.getEncoded() method one finds that the parity bits are correct. I have never been happy with this since the whole point of parity bits is to detect bad ... parity!


problem 1: key generation. The other application is a windows / .net based and they are using CryptDeriveKey function from Microsoft based provider. My application is a java servlet and Oracle App Server (so a fully java env). possibly we have the sun based provider. So, I did a lot of study and finally did my own implementation of CryptDeriveKey in java using the same algorithm but implemented in java. (basically takes the SHA-1 digest of the plain text string and then do the array filling with particular bits with XOR bit operation etc. etc.) I am not getting into the details of that here for brevity. But got a 168 bit (21 bytes) key as desired.



I went though all of this about 4 years ago. Assuming you have implemented the CryptDeriveKey functionality correctly (the people who wrote the .NET version for MONO didn't - it has a serious flaw) then you need to extract the 24 bytes of key material and not 21 bytes.


Now my first problem here was getting that 168 bit key to work with my DESede code. it would complain as InvalidKeyException. because it was expecting 24 bytes (I don't know why)



:-) For the reason I gave above.


but neglecting the parity bits. But because, I am not using the standard key generator and defining my own key and defining a keyspec etc. there was some issue there and it was complaining. so, I was told to massage the key and add the parity bits. I then wrote the method to carefully add the 8th parity bits (luckily some gracious good programmer had put up a nice code to add this for DES keys on the net). Now, the key is going through, but the cipher do final is creating problems. I am pasting my code for the key generation as well as for the decryption.

problem 2: decryption. I am having problems here and would love help. I am attaching the code below. the encrypted string is some funky looking string with characters of all shapes how do I do the padding of it? also, I have already specified PKCS5Padding in the algorithm spec. isn't that supposed to do the padding?



YOU don't have to worry about the padding - the PKCS5Padding is applied and removed automatically in the Java. You MUST MUST MUST make sure that the .NET code also does PKCS5Padding and not some other padding. I can't remember the default in .NET but I seem to remember it is padded with zeros and does not use PKCS5 padding (correct me if I am wrong). In .NET one gets the equivalent of PKCS5 padding using their PKCS7 padding since they will be equivalent for your 8 byte block size.


why is it expecting the input string to be exactly 8 byte blocks?



DES (and therefor DESede) is a block cipher with a block size of 8 bytes which means it encrypts 8 bytes at a time. The PKCS5 padding removes the 8 byte requirement by reversibly adding some padding to make the total number of bytes up to a multiple of 8.


now, I tried the cipher.update() method and did a println before I did the cipher.doFinal() as the doFinal was what was throwing the IllegalBlockSizeException. but could not figure if the intermediate output was ok. Basically need help here.


Key generation code:




Decryption code:



There are some anomalies in the code but far too much is missing for any diagnosis.

The main problem I have with your code is that I only see one part of side! I see SOME of your Java but NONE of your .NET at all. Since, as you are finding, the Devil is in the detail it is important to see all code ESPECIALLY the .NET which you are trying to be compatible with. Without this I can't help.

 
This tiny ad is wafer thin:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic