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.