android base64 java,Java和Android之间的Base64错误编码/解码

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

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值