wood burning stoves*
The moose likes Security and the fly likes Decryption returning wrong value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » Security
Bookmark "Decryption returning wrong value" Watch "Decryption returning wrong value" New topic
Author

Decryption returning wrong value

Lynn Owens
Greenhorn

Joined: Aug 24, 2012
Posts: 19
Hi,

I am trying to decrypt a value using a key stored in a keystore. It's not throwing any exceptions but the "decrypted" value is wrong.

It's spitting out things like: [B@1befab0

This is what I've got.



Any ideas?
Lynn Owens
Greenhorn

Joined: Aug 24, 2012
Posts: 19
After banging on this for some time, I'm still getting basically the same result with significantly different code.



Outputs: [B@10ef90c

There's somethign fundamental I'm missing here...
Lynn Owens
Greenhorn

Joined: Aug 24, 2012
Posts: 19
Ok yup it was fundamental, duh, I was printing a byte[]. lol

But unfortunately the problem is not truly solved. By using the following I converted the byte[] to a String and printed it, but I'm still getting the wrong values.



Now it's outputting:

???L ?A??=?@+?A?h?B???F?
be??????4?????6w???>(???i?&?>?{d?ib)+?es?m??E'??4 T/?????_?
i]??v<\???g?RN?d?/?}?N?+w+a??L???-?8???a??eS???6?*?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Well, that's just the standard way of outputting an array of bytes. The code you've written uses the toString() method of an object to convert it to a String for display, but since arrays don't implement that method, you get the default toString() method of the Object class. Which produces something which starts with the class name ("[B" for byte array) followed by something which sort of represents the object's address. This isn't particularly useful if you expected it to tell you something about the array's contents, because it doesn't do that.

If you want something more meaningful (e.g. something which actually tells you what bytes are in the array) then you'll have to write that code yourself. Or if perhaps you meant to convert that byte array to a String, on the grounds that it actually encodes some text, then



would be the way to do that.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Ah, I see you've already figured out what I was saying.

But I don't understand your code. It looks like you're encrypting something and producing an array of bytes, but then when you decrypt you don't decrypt the whole thing, you try to decrypt only part of it. I'm no security expert but I wouldn't expect that to work.

Although it's possible I didn't understand what you were trying to do (see the part about not being an expert).
Lynn Owens
Greenhorn

Joined: Aug 24, 2012
Posts: 19
Although I'm not encrypting anything, I am doing a Base64 decode before decrypting. Then I believe the value translates to a DERObject that encapsulates the actual content. I try to extract the content, decrypt it, and display it.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Well, if line 6 (un-redacted) is supposed to represent encrypted data in some way, then yes, it must have had something done to it to convert the raw bytes into readable text. Perhaps that was Base-64 encoding, but I don't think that has anything to do with the actual process which produced that encrypted data.

I guess that Base-64 is the most likely thing to have been used, but you'd have to check with whoever gave you that encrypted data.

Lynn Owens
Greenhorn

Joined: Aug 24, 2012
Posts: 19
Here's the un-redacted encrypted value ...

MIHuBgkqhkiG9w0BBwaggeAwgd0CAQAwgdcGCSqGSIb3DQEHATAKBggqhkiG9w0DBICBvcs6iLO2VGfU7KzJmpU0y5lBsRYAs1tOdZKW6fmJ4XXQUgUyl7rcYUGSa5hu+XfbAFGRHwdLS4LSClP34srZ8aF8KtxXxdYbvxZ7s5xjnleHN8GBneh1ocoofaXLWmoFUa/WS47oQXGY80jSJ/Dm1B28Pt+VerKQtkr8sTRoSwtXWf5RuspfCGFvMddea2xuO5PX+14A+/8VfYuCRjjxMOMWE5suKfBunQICws86QkewfqxbTmOVKsB7kgXKag==

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41052
    
  43
The "==" at the end indicates that it is indeed base-64 encoded.


Ping & DNS - my free Android networking tools app
Lynn Owens
Greenhorn

Joined: Aug 24, 2012
Posts: 19
After days of sticking with it... here is the solution. The problem was apparently confusion between Base64 from two different packages.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Decryption returning wrong value
 
Similar Threads
How to encrypt & decrypt Random DES key with DES?
Encryption/Decryption using Blowfish algorithm
need help abt 64 encoder/decoder
AES Encryption Service
What broke between 1.4 and 1.6 for decryption?