Error encode/decode Base64 between Java and Android

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

As my question, I have a big problem when I encode/decode Base64 between Java and Android.

Here is my case:

I write code to encrypt/decrypt using ECC on Java, mycode 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:

1/ 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

I'm really stuck. Please help me. Thanks in advance.

回答1:

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.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!