简体   繁体   English

Java和Android之间的错误编码/解码Base64

[英]Error encode/decode Base64 between Java and Android

I have a problem when I encode/decode Base64 between Java and Android. 我在Java和Android之间编码/解码Base64时遇到问题。

Here is my case: 这是我的情况:

I write code to encrypt/decrypt using ECC on Java, my code work very well. 我在Java上使用ECC编写代码来加密/解密,我的代码工作得非常好。

Then I try to encrypt string on Java and decrypt this encrypted string on Android, it fail. 然后我尝试在Java上加密字符串并在Android上解密这个加密的字符串,它失败了。

I think the problem maybe encode/decode Base64. 我认为问题可能是编码/解码Base64。

Here is my code: 这是我的代码:

Encrypt/decrypt on Java only: 仅在Java上加密/解密:

  //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: 然后我在Android上编写解密字符串的代码,但Android没有这个方法:

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. 尝试用decode替换它。

As the ciphertext is Base64 encoded twice instead of decoded before attempting to decrypt it, decryption fails with the error you showed us. 由于密文是Base64编码两次而不是在尝试解密之前解码,因此解密失败并显示您向我们显示的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM