Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error encode/decode Base64 between Java and Android

Tags:

I have a problem when I encode/decode Base64 between Java and Android.

Here is my case:

I write code to encrypt/decrypt using ECC on Java, my code work very well.

Then I try to encrypt string on Java and decrypt this encrypted string on Android, it fail.

I think the problem maybe encode/decode Base64.

Here is my code:

Encrypt/decrypt on Java only:

  //ENCRYPT
try {
        Cipher c = Cipher.getInstance("ECIES",BouncyCastleProvider.PROVIDER_NAME);
        c.init(Cipher.ENCRYPT_MODE,publicKey);
        encodeBytes = c.doFinal(origin.getBytes());

        String encrypt = Base64.getEncoder().encodeToString(encodeBytes);

        System.out.println("Encrypt:"+ encrypt+"\n");


    } catch (Exception e) {
        e.printStackTrace();
    }

//////DECRYPT
    try
    {
        String abc = Base64.getDecoder().decode(encrypt);
        Cipher c = Cipher.getInstance("ECIES","BC");
        c.init(Cipher.DECRYPT_MODE,privateKey);
        //decodeBytes = c.doFinal(encodeBytes);
        decodeBytes = c.doFinal(abc);
        String deCrypt = new String(decodeBytes,"UTF-8");

        System.out.println("Decrypt:"+ deCrypt +"\n");
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
    }

Here is my result:

private key: EC Private Key [eb:bc:b0:30:f0:42:4e:f1:0f:c5:6a:49:22:93:51:72:ea:23:0c:9a]
        X: c55275cd4a40690ec8d5333cd31994e3066d7f49f57df6c3aed20385c6b50325
        Y: b1a2c67c9c3ac8b8508e210cb2ac476999a913b85e283359fd4482d68164c7e9


public key: EC Public Key [eb:bc:b0:30:f0:42:4e:f1:0f:c5:6a:49:22:93:51:72:ea:23:0c:9a]
        X: c55275cd4a40690ec8d5333cd31994e3066d7f49f57df6c3aed20385c6b50325
        Y: b1a2c67c9c3ac8b8508e210cb2ac476999a913b85e283359fd4482d68164c7e9


Encrypt:BG+pFzDgRLhTI44Rj9w3zkTprPqTryOrqP8xfrS7tb5H3e0KGoxyq/e5SngwQeAr91aGBn6jAUNupwqEihYta7epTtpLP84d7LFxgTJs+bsYgu3WskadiLSImjigzLM1g/VgZ2PWk1Y7idAX

Decrypt:63B952562----0907888511

Then I write code to decrypt string on Android, but Android not have this method:

Base64.getDecoder().decode(String);

I must replace it with this:

byte[] encodeBytes = null;
encodeBytes = Base64.encode(my_encrypted_string.getBytes(),Base64.DEFAULT);
Cipher c = Cipher.getInstance("ECIES","SC");
c.init(Cipher.DECRYPT_MODE,privateKeyFromFile);
decodeBytes = c.doFinal(encodeBytes);
String deCrypt = new String(decodeBytes,"UTF-8");
txtHiden.setText(deCrypt);
Toast.makeText(activity, deCrypt, Toast.LENGTH_SHORT).show();

But it shows this error:

10-03 09:50:24.466 13134-13134/com.example.napoleon.luanvana W/System.err: org.spongycastle.jcajce.provider.util.BadBlockException: unable to process block
like image 488
nistelrooy41001662 Avatar asked Oct 03 '17 02:10

nistelrooy41001662


1 Answers

This seems to be a simple mistake. You replaced

String abc = Base64.getDecoder().decode(encrypt);

with

byte[] encodeBytes = null;
encodeBytes = Base64.encode(my_encrypted_string.getBytes(),Base64.DEFAULT);

if I read this correctly. Try to replace that with decode instead.

As the ciphertext is Base64 encoded twice instead of decoded before attempting to decrypt it, decryption fails with the error you showed us.

like image 73
Hulk Avatar answered Oct 12 '22 23:10

Hulk