2

Here's a string :

String msg="dpbqNszFN2cpmtlzPi3myV9M1ctlkSIQD95ue+T+9rz13T+Pe/aLZ8Pd5geI+PhEM/b0UeRS1cAzKybQsKICTBhh3ke5Jjw6BHWGESJWBnUT54lAlTvzkgOxpQ5stBh2cPPSn3KLyKmXifr8ClbV5s1k3Gy5C7HitA5KLw7hRxAmIGSWQG7PaiLNEVRbgicNfJ7Ic7VIdGA/UA51vK8mpywIR2YQUDPv30ThGq4DuclaJ3X4aVWVj8VYChcfM+82sViVU8HO3DF9CCU4EIADNET503olxiDZBp7WMYmJvWq0KhhZXkLSY3QFmcSMX6IThtdKKCcZp6hu3TtC+7aP7Q=="; 

So the byte array is

[1] => 100 [2] => 112 [3] => 98 [4] => 113 [5] => 78 [6] => 115 [7] => 122 .. 

Which should be decoded to

[ I used PHP base64_encode() for encoding into the above form & base64_decode() for getting this output ]

[1] => 118 [2] => 150 [3] => 234 [4] => 54 [5] => 204 [6] => 197 7] => 55 ... 

But the problem I'm facing is that ( In android , Using Base64.decode(String) ) it's getting decoded to :

[118, -106, -22, 54, -52, -59, 55, 103, 41, -102, -39, 115, 62, 45, -26, -55, 95, 76, -43, -53, 101, -111, 34, 16, 15, -34, 110, 123, -28, -2, -10, -68, -11, -35, 63, -113, 123, -10, -117, 103, -61, -35, -26, 7, -120, -8, -8, 68, 51, -10, -12, 81, -28, 82, -43, -64, 51, 43, 38, -48, -80, -94, 2, 76, 24, 97, -34, 71, -71, 38, 60, 58, 4, 117, -122, 17, 34, 86, 6, 117, 19, -25, -119, 64, -107, 59, -13, -110, 3, -79, -91, 14, 108, -76, 24, 118, 112, -13, -46, -97, 114, -117, -56, -87, -105, -119, -6, -4, 10, 86, -43, -26, -51, 100, -36, 108, -71, 11, -79, -30, -76, 14, 74, 47, 14, -31, 71, 16, 38, 32, 100, -106, 64, 110, -49, 106, 34, -51, 17, 84, 91, -126, 39, 13, 124, -98, -56, 115, -75, 72, 116, 96, 63, 80, 14, 117, -68, -81, 38, -89, 44, 8, 71, 102, 16, 80, 51, -17, -33, 68, -31, 26, -82, 3, -71, -55, 90, 39, 117, -8, 105, 85, -107, -113, -59, 88, 10, 23, 31, 51, -17, 54, -79, 88, -107, 83, -63, -50, -36, 49, 125, 8, 37, 56, 16, -128, 3, 52, 68, -7, -45, 122, 37, -58, 32, -39, 6, -98, -42, 49, -119, -119, -67, 106, -76, 42, 24, 89, 94, 66, -46, 99, 116, 5, -103, -60, -116, 95, -94, 19, -122, -41, 74, 40, 39, 25, -89, -88, 110, -35, 59, 66, -5, -74, -113, -19] 

As you can see , some characters ( which are not readable though) , are not getting decoded correctly. Edit(2) : bytes are signed in java and unsigned in PHP . Thanks @Jon and @Tony for clearing that idea. But the bug remains in my code.

Edit(1):

Code Segment:

KeyFactory fact = KeyFactory.getInstance("RSA"); BigInteger e=new BigInteger("65537"); BigInteger n=new BigInteger("B7AC0C8F738305F8BDDF93EE25655A70FBDF9F074640C159E36914C227BE1E50A615A25E6706EBA08FB79216F02279420ED4C9DA310778601F6A3233EDADE2FF3775D29E5302C4FCB9E7879D9F3C814AE8F42759148D91CDB23E528241AE2E44F6DE9D4334494C103886B2333D5833EFEABD76205B8F4897BB908E71697A10F6494EBFB0392A831575F64672E0F915A88F46F7FC03E7F94EB56A8A3296840095CB53787EE6E71D4C297108EA5CDD31BD37B1C0A55A8B5FA78F88FC82AEF3C2C80C0FC2CC97A1AEC74ACE44F4AC1B111B727311DA4D1447899A15BA292BCA4E7E864DF55CCB903CD4109874AD475393E7F24FC60E2D7B88E9B4FB57FA54A9B817",16); RSAPublicKeySpec keySpec=new RSAPublicKeySpec(n,e); PublicKey pubKey = (PublicKey) fact.generatePublic(keySpec); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, pubKey ); String msg="dpbqNszFN2cpmtlzPi3myV9M1ctlkSIQD95ue+T+9rz13T+Pe/aLZ8Pd5geI+PhEM/b0UeRS1cAzKybQsKICTBhh3ke5Jjw6BHWGESJWBnUT54lAlTvzkgOxpQ5stBh2cPPSn3KLyKmXifr8ClbV5s1k3Gy5C7HitA5KLw7hRxAmIGSWQG7PaiLNEVRbgicNfJ7Ic7VIdGA/UA51vK8mpywIR2YQUDPv30ThGq4DuclaJ3X4aVWVj8VYChcfM+82sViVU8HO3DF9CCU4EIADNET503olxiDZBp7WMYmJvWq0KhhZXkLSY3QFmcSMX6IThtdKKCcZp6hu3TtC+7aP7Q=="; byte[] bytes=msg.getBytes("UTF-8"); byte[] enc_bytes= Base64.decode(bytes,Base64.DEFAULT); byte[] dt = cipher.doFinal(enc_bytes); String code=new String(dt,"UTF-8"); System.out.println(code); 

Cipher decrypted String comes out as ( At the second system.out )

05-10 21:05:27.501: I/System.out(11809): �ќ2`��H&��'Va�x�m��0G�����V�T�����)^����/|���BG,f_r fK�B7?�a��n������Jl�y�yL���}��΂Ճ������JZj�+�|�-#ș%u�1�z�c�G��nl�5����HELLO HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 H 

The Base64_encoded & signed data is "HELLO HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 HI1 H"


[Solved]

Huge thanks to @JonSkeet .

cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); removed all the junk data prefixed to it.

3
  • Verify PHP uses the same algorithm as Android. Commented May 10, 2012 at 15:26
  • Base64 is universal. It should be. Right?. Commented May 10, 2012 at 15:26
  • I would not assume that. It would be easy enough to check. But I think your issue is resolved. Commented May 10, 2012 at 15:30

3 Answers 3

6

All looks fine to me - you just need to remember that bytes are signed in Java. So a value of -106 in Java corresponds to an unsigned value of 150. Once you bear that in mind, I think you'll see that all the values in PHP correspond to the values in Java.

Note that you ought to distinguish between bytes and characters. So when you say:

As you can see , some characters ( which are not readable though) , are not getting decoded correctly.

You actually mean some bytes... and there's no such concept as a "readable" byte.

EDIT: I see no sign that you've actually decrypted the data at all.

You're converting the still encrypted result of the base64 decoding into text using the platform default encoding:

String tmpr=new String(baser); 

You shouldn't be doing that - you should be using baser as the cipher-text to decrypt.

It's not at all clear where the tmp value you're passing to doFinal comes from at all...

Sign up to request clarification or add additional context in comments.

25 Comments

What i meant was, the Base64 encoded data is not a readable string. It is an encrypted data.
@ngen: If the problem is decryption (as you've added in a comment later) then that suggests the problem is with the encryption or decryption code, which you haven't shown us. The base64 part seems to be fine here. But it's important to understand that the decoded base64 isn't a string at all, readable or unreadable - it's just bytes.
Perhaps he means it was encrypted, then BASE-64'd?
@TonyEnnis: Yes, having seen another comment that makes sense. But it's still a mistake to talk about the decoded data as a "string" at all.
@ngen: Ah - in that case it may well just be the CBC/padding modes (or initialization vector etc) involved. You should investigate that.
|
1

Your issue is one of sign. The first sample 'the should' is treating the output bytes as 8-bit unsigned. The Android version is treating them as signed bytes.

Is your code actually failing?

Edit

cipher.final() seems to be designed to finish decryption. Did you call cipher.update() first? See the example here, and look for update(...) stackoverflow.com/questions/3150830/android-encryption

Here's the Android Cipher documentation, for your convenience.

10 Comments

Yes, It is failing when the 'signed' byte array goes to cipher decryption.
What input type does the decryption expect? If it expects an array of unsigned, you may have to convert the byte array to an unsigned.
It's the Android's Cipher.doFinal(byte[]) . It is expecting bytes . But it's failing to decrypt the data and is adding a lot of junk in front of the actual data.
I think it is Cipher.doFinal(byte[])
I'm totally confused about the bug , I'm sorry . I'll be more clear & specific. Cipher.doFinal() accepts byte array and i'm trying it correctly, it's even decrypting. But the issue like i have mentioned in the edit is that, its producing Illegible string . Which I'm assuming now , is due to signed/unsigned bytes ?. Java supports only signed bytes?.
|
-2

Try this on your byte array

byte[] bytes = Base64.decode(data, Base64.DEFAULT); String string = new String(bytes, "UTF-8"); 

3 Comments

Absolutely not. Unless you have any evidence that this is actually meant to be UTF-8-encoded text (I see no such evidence in the question), you shouldn't be trying to create text out of it.
The answer is totally irrelevant.
My suggestion was based on the previous question at that particular time there was no code added which said that the data was being encrypted. There have been 2 edits to the question since I gave answers. I don't try creating text for the sake of creating it. People ought to read their code segments carefully before hitting the send button.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.