Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 651
  • Last Modified:

Converting java base64 to oracle raw value

We have a encrypted byte array which has been encrypted using des3, we have converted this to  base 64 string

We now want to decrypt this string in oracle.
To do this we use the oracle 9i functions
we convert base64 string to a raw value, then we decode the raw value and then we decrypt the raw value in oracle 9i using des3.

We are not getting the same value?  Any ideas what we are doing wrong?

The main thing is we are having problems trying to convert data between oracle and java.
In oracle the encrypted value is stored as a raw value in a oracle database.
We want to be able to encrypt the data in java in a way that the data can be stored in this raw field.

To do this we encrypt the data in java using des3, this returns a byte array, we convert this to a base64 encoded string so we can write this to a file.
We then read this value in oracle.

0
inzaghi
Asked:
inzaghi
  • 11
  • 6
  • 6
2 Solutions
 
objectsCommented:
can u post how you do the conversion
though base64 seems unnecesary and just complicates the situation.

0
 
objectsCommented:
which conversion is giving the wrong result, the base64 or the decryption?
0
 
inzaghiAuthor Commented:
String base64 = new Base64Encoder(b).processString();
where b is the encrypted des3 byte array.

I need to write the data to the a file so I encoded it has a base 64 string.
ie has before

username+fileseperator+encryptedbase64string

Oracle stores the encrypted data has a raw value so it needs to do the necesary conversions.  

At the moment I don't think we may not be using the same mode and padding.
In java I have specified no padding and mode so it uses /ECB/PKCS5Padding.

I need to check what mode and padding is used in oracle.

Thanks for the help


0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
CEHJCommented:
Can you confirm that the base64 parts en/decode to the same value?
0
 
objectsCommented:
> I need to write the data to the a file so I encoded it has a base 64 string.

as i explained to you before thats unnecessary, you can write the encode bytes directly.
0
 
objectsCommented:
you need to compare the data at each step to determine which state is causing the error.
0
 
inzaghiAuthor Commented:
I think the main problem is with the mode and padding is different in both
platforms

In java I am encrypting with
the following

Algorithm: DESede
mode : ECB
padding: PKCS5Padding

How do I set this in oracle?
0
 
CEHJCommented:
Why do you have to en/decode in both Java *and* Oracle?
0
 
inzaghiAuthor Commented:
Just have to, main thing is to encrypt in java and decrypt in oracle
0
 
objectsCommented:
> How do I set this in oracle?

You just need to store the bytes in Oracle, it does not need to worry about encrypting/decrypting it.
Thats the job of the clienbt accessing the data.

0
 
objectsCommented:
Check that you're using the correct character encoding whenever switching between strings and bytes.
0
 
inzaghiAuthor Commented:
Ok, des3 encrypts data in java and returns a byte array.

If we now want to decrypt this data in oracle.  Des3 in oracle expects a raw value
what form should we store this data in java so we can decrypt in oracle.
0
 
objectsCommented:
byte array
0
 
CEHJCommented:
>>what form should we store this data in java

How do you mean - java doesn't store anything, other than in class files?
0
 
objectsCommented:
and if using strings make sure you use the same encoding when going back and forth.
0
 
CEHJCommented:
As far as i can remember fromyour earlier qs, you are storing the encrypted data in files too (not sure why). You could store a byte array in those files, but since you want to store other info along with the encrypted bytes, you will have to do one of two things:

a. use a text file
b. use fixed width fields

or you won't be able to delimit the encrypted bytes from your other info
0
 
inzaghiAuthor Commented:
cant I convert the enrypted byte array to a string using
new String(encryptedbyteArray, "UTF-8)
0
 
inzaghiAuthor Commented:
How do I convert a raw value from oracle into a byte array in java
0
 
CEHJCommented:
>>cant I convert the enrypted byte array to a string using ...

No. Why should an arbitrary array of bytes make sense as a String?

>>How do I convert a raw value from oracle into a byte array in java

byte[] encryptedBytes = resultSet.getBytes(cryptCol);
0
 
objectsCommented:
> How do I convert a raw value from oracle into a byte array in java

No conversion should be necessary. You should be reading it as a byte array from the db.
0
 
CEHJCommented:
8-)
0
 
objectsCommented:
Thanks mate :)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 11
  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now