Java:使用 java.util.Base64 与 android.util.Base64 解码 base64 字符串时的不同结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32935783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:03:10  来源:igfitidea点击:

Java: Different results when decoding base64 string with java.util.Base64 vs android.util.Base64

javaandroidbase64

提问by Parker Kemp

I'm working on a client/server system and I'm trying to do some basic encryption. When I connect to the server, I send a public key as an escaped string across the socket. I've verified that the string is identicalon both ends, newlines and all.

我正在开发客户端/服务器系统,并且正在尝试进行一些基本的加密。当我连接到服务器时,我通过套接字发送一个公钥作为转义字符串。我已经验证字符串两端、换行符和所有字符串都是相同的

On the client (Android), I'm able to use the public/private keys to successfully encrypt and decrypt a secret key (for testing purposes). However, the server fails right out of the gate when trying to decode the public key from a String to a byte[], with:

在客户端 (Android) 上,我可以使用公钥/私钥成功加密和解密密钥(用于测试目的)。但是,服务器在尝试将公钥从 String 解码为 byte[] 时立即失败,使用:

 java.lang.IllegalArgumentException: Illegal base64 character a

which seems preposterous, as 'a' is absolutely a base64 character, if I understand correctly. The client and server use a shared library to handle all encryption, so the code is nearly identical. The onlydifference is encoding/decoding base64 Strings, since java.util.Base64 is unavailable on Android.

这似乎很荒谬,因为如果我理解正确的话,'a' 绝对是一个 base64 字符。客户端和服务器使用共享库来处理所有加密,因此代码几乎相同。的唯一差别是编码/解码的base64字符串,由于java.util.Base64是在Android不可用。

Shared class

共享类

public abstract class EasyCrypt {

...

    public PublicKey loadPublicKey(String key64) throws GeneralSecurityException {

        byte[] data = decode(key64); //Calls abstract methods, shown below

        X509EncodedKeySpec spec = new X509EncodedKeySpec(data);
        KeyFactory fact = KeyFactory.getInstance("RSA");
        return fact.generatePublic(spec);
    }

...

}

Client (Android) methods

客户端 (Android) 方法

import android.util.Base64;

public class ClientCrypt extends EasyCrypt {
    @Override
    protected byte[] decode(String s) {
        return Base64.decode(s.getBytes(), Base64.DEFAULT); //Works perfectly
    }

    @Override
    protected String encode(byte[] bytes) {
        return Base64.encodeToString(bytes, Base64.DEFAULT);
    }

}

}

Server (Linux) methods

服务器 (Linux) 方法

import java.util.Base64;

public class ServerCrypt extends EasyCrypt{
    @Override
    public byte[] decode(String str){
        return Base64.getDecoder().decode(str); //Throws IllegalArgumentException
    }

    @Override
    public String encode(byte[] bytes){
        return Base64.getEncoder().encodeToString(bytes);
    }

}

}

采纳答案by Mohammad Adil

On android, Use Base64.NO_WRAPinstead of Base64.DEFAULT

在 android 上,使用Base64.NO_WRAP代替Base64.DEFAULT

@Override
protected String encode(byte[] bytes) {
    return Base64.encodeToString(bytes, Base64.NO_WRAP);
}

回答by S Shepard

Instead of Base64.getDecoder()use Base64.getMimeDecoder().

而不是Base64.getDecoder()使用Base64.getMimeDecoder().