Java encryption

J

JavaEnquirer

I'm reading textual data out of an SQL Server 2005 database. This works

fine as you'd expect, however, when I attempt to decrypt encrypted
String data
read out of the database using the Java cryptography classes I get the
following error:

javax.crypto.IllegalBlockSizeException: Input length must be multiple
of 8 when decrypting with padded cipher

When I manipulate the Strings locally, encrypting and decrypting them
back, everything is fine. The seems to be some hidded problem with the
Strings extracted from the database.

Any one got any ideas? One theory I have concerns the fact that SQL
Server 2005 uses USC-2 encoding whereas the decrypter expects UTF-8.
Could this be at the heart of the problem? If so, how would I go about
converting a USC-2 String into a UTF-8 encoded String?
 
C

Chris Uppal

JavaEnquirer said:
I'm reading textual data out of an SQL Server 2005 database. This works

fine as you'd expect, however, when I attempt to decrypt encrypted
String data
read out of the database using the Java cryptography classes I get the
following error:

Given that encrypting a string produces binary data, why are you storing it as
text in your database ?

And, given that apparently you /are/ doing so, why do you expect it to work ?
Arbitrary binary data will not conform to the rules of any character encoding
such as UTF-16 or UTF-8 and is likely to be mangled (if not rejected outright)
if treated as such. The only character encoding in which it might be safe to
pretend the binary data was "really" character-encoded text would be something
like ISO-8859-1 where each of 0..255 corresponds to exactly one character.

If you /must/ save binary data as text then it is almost certainly better to
re-encode it /as/ text, using something like base-64 encoding -- before saving
it.

-- chris
 
J

JavaEnquirer

Apologies for wasting bandwidth. This is actually a non problem. SQL
Server 2005 gives no problems what so ever with storing Java encrypted
Strings and allowing you to read them out an decrypt them. ( Assuming
you do eveything sensibly like using BASE64 encoding )
I fell into the classic mistake of over thinking the problem. My data
inputting stored procedure was using a varchar of a size too small and
was consequently truncating the encrypted string I was passing in,
hence causing the decryption to fail. Damd those typos.
 
J

JavaEnquirer

Chris said:
Given that encrypting a string produces binary data, why are you storing it as
text in your database ?

And, given that apparently you /are/ doing so, why do you expect it to work ?
Arbitrary binary data will not conform to the rules of any character encoding
such as UTF-16 or UTF-8 and is likely to be mangled (if not rejected outright)
if treated as such. The only character encoding in which it might be safe to
pretend the binary data was "really" character-encoded text would be something
like ISO-8859-1 where each of 0..255 corresponds to exactly one character.

If you /must/ save binary data as text then it is almost certainly better to
re-encode it /as/ text, using something like base-64 encoding -- before saving
it.

-- chris

Cheers, I ommited to mention that I was using BASE 64, something like:

byte[] utf8 = text.getBytes( encoding );

// Encrypt
byte[] enc = cipher.doFinal( utf8 );

// Encode bytes to base64 to get a string
encryptedString = new sun.misc.BASE64Encoder().encodeBuffer( enc );
 
C

Chris Uppal

JavaEnquirer said:
My data
inputting stored procedure was using a varchar of a size too small and
was consequently truncating the encrypted string I was passing in,
hence causing the decryption to fail. Damd those typos.

Oh dear! You have my sympathy....

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Character encoding 1
Java string encryption/decryption 12
Encryption 11
encryption question 1
Java Encryption newbie 5
Encrypt and Decrypt 3
Encryption Problems 2
encryption in Oracle, decryption in Java 1

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top