Java和Android之间的Base64错误编码/解码 [英] Error encode/decode Base64 between Java and Android

查看:1149
本文介绍了Java和Android之间的Base64错误编码/解码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java和Android之间对Base64进行编码/解码时遇到问题.

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

这是我的情况:

我在Java上使用ECC编写了用于加密/解密的代码,我的代码工作得很好.

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

然后我尝试在Java上加密字符串并在Android上解密此加密字符串,但是失败.

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

我认为问题可能是对Base64进行编码/解码.

I think the problem maybe encode/decode Base64.

这是我的代码:

仅在Java上加密/解密:

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();
    }

这是我的结果:

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

然后我编写代码以在Android上解密字符串,但是Android没有此方法:

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

Base64.getDecoder().decode(String);

我必须用这个替换它:

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();

但它显示此错误:

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);

使用

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

如果我没看错的话.尝试用 decode .

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

由于密文是对Base64进行两次编码而不是在尝试对其进行解密之前进行解码的,因此解密失败并显示您向我们显示的错误.

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

这篇关于Java和Android之间的Base64错误编码/解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆