Base64:java.lang.IllegalArgumentException:非法字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28584080/
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
Base64: java.lang.IllegalArgumentException: Illegal character
提问by marknorkin
I'm trying to send a confirmation email after user registration. I'm using the JavaMail library for this purpose and the Java 8 Base64 util class.
我正在尝试在用户注册后发送确认电子邮件。为此,我使用了 JavaMail 库和 Java 8 Base64 util 类。
I'm encoding user emails in the following way:
我通过以下方式对用户电子邮件进行编码:
byte[] encodedEmail = Base64.getUrlEncoder().encode(user.getEmail().getBytes(StandardCharsets.UTF_8));
Multipart multipart = new MimeMultipart();
InternetHeaders headers = new InternetHeaders();
headers.addHeader("Content-type", "text/html; charset=UTF-8");
String confirmLink = "Complete your registration by clicking on following"+ "\n<a href='" + confirmationURL + encodedEmail + "'>link</a>";
MimeBodyPart link = new MimeBodyPart(headers,
confirmLink.getBytes("UTF-8"));
multipart.addBodyPart(link);
where confirmationURL
is:
在哪里confirmationURL
:
private final static String confirmationURL = "http://localhost:8080/project/controller?command=confirmRegistration&ID=";
And then decoding this in ConfirmRegistrationCommand in such way:
然后以这种方式在 ConfirmRegistrationCommand 中解码:
String encryptedEmail = request.getParameter("ID");
String decodedEmail = new String(Base64.getUrlDecoder().decode(encryptedEmail), StandardCharsets.UTF_8);
RepositoryFactory repositoryFactory = RepositoryFactory
.getFactoryByName(FactoryType.MYSQL_REPOSITORY_FACTORY);
UserRepository userRepository = repositoryFactory.getUserRepository();
User user = userRepository.find(decodedEmail);
if (user.getEmail().equals(decodedEmail)) {
user.setActiveStatus(true);
return Path.WELCOME_PAGE;
} else {
return Path.ERROR_PAGE;
}
And when I'm trying to decode:
当我尝试解码时:
http://localhost:8080/project/controller?command=confirmRegistration&ID=[B@6499375d
I'm getting java.lang.IllegalArgumentException: Illegal base64 character 5b
.
我得到java.lang.IllegalArgumentException: Illegal base64 character 5b
。
I tried to use basic Encode/Decoder (not URL ones) with no success.
我尝试使用基本的编码/解码器(不是 URL 的)但没有成功。
SOLVED:
解决了:
The problem was the next - in the line:
问题是下一个 - 在行中:
String confirmLink = "Complete your registration by clicking on following"+ "\n<a href='" + confirmationURL + encodedEmail + "'>link</a>";
I'm calling toString on an array of bytes, so I should do the following:
我在一个字节数组上调用 toString,所以我应该执行以下操作:
String encodedEmail = new String(Base64.getEncoder().encode(
user.getEmail().getBytes(StandardCharsets.UTF_8)));
Thanks to Jon Skeetand ByteHamster.
感谢Jon Skeet和ByteHamster。
采纳答案by ByteHamster
Your encoded text is [B@6499375d
. That is not Base64, something went wrong while encoding. That decoding code looks good.
您的编码文本是[B@6499375d
. 那不是 Base64,编码时出了点问题。那个解码代码看起来不错。
Use this code to convert the byte[] to a String before adding it to the URL:
使用以下代码将 byte[] 转换为 String,然后再将其添加到 URL:
String encodedEmailString = new String(encodedEmail, "UTF-8");
// ...
String confirmLink = "Complete your registration by clicking on following"
+ "\n<a href='" + confirmationURL + encodedEmailString + "'>link</a>";
回答by guitarpicva
The Base64.Encoder.encodeToString method automatically uses the ISO-8859-1 character set.
Base64.Encoder.encodeToString 方法自动使用 ISO-8859-1 字符集。
For an encryption utility I am writing, I took the input string of cipher text and Base64 encoded it for transmission, then reversed the process. Relevant parts shown below. NOTE: My file.encoding property is set to ISO-8859-1 upon invocation of the JVM so that may also have a bearing.
对于我正在编写的加密实用程序,我将输入的密文字符串和 Base64 编码用于传输,然后反转该过程。相关部分如下图。 注意:在调用 JVM 时,我的 file.encoding 属性设置为 ISO-8859-1,因此这也可能有影响。
static String getBase64EncodedCipherText(String cipherText) {
byte[] cText = cipherText.getBytes();
// return an ISO-8859-1 encoded String
return Base64.getEncoder().encodeToString(cText);
}
static String getBase64DecodedCipherText(String encodedCipherText) throws IOException {
return new String((Base64.getDecoder().decode(encodedCipherText)));
}
public static void main(String[] args) {
try {
String cText = getRawCipherText(null, "Hello World of Encryption...");
System.out.println("Text to encrypt/encode: Hello World of Encryption...");
// This output is a simple sanity check to display that the text
// has indeed been converted to a cipher text which
// is unreadable by all but the most intelligent of programmers.
// It is absolutely inhuman of me to do such a thing, but I am a
// rebel and cannot be trusted in any way. Please look away.
System.out.println("RAW CIPHER TEXT: " + cText);
cText = getBase64EncodedCipherText(cText);
System.out.println("BASE64 ENCODED: " + cText);
// There he goes again!!
System.out.println("BASE64 DECODED: " + getBase64DecodedCipherText(cText));
System.out.println("DECODED CIPHER TEXT: " + decodeRawCipherText(null, getBase64DecodedCipherText(cText)));
} catch (Exception e) {
e.printStackTrace();
}
}
The output looks like:
输出看起来像:
Text to encrypt/encode: Hello World of Encryption...
RAW CIPHER TEXT: q$;?C?l??<8??U???X[7l
BASE64 ENCODED: HnEPJDuhQ+qDbInUCzw4gx0VDqtVwef+WFs3bA==
BASE64 DECODED: q$;?C?l??<8??U???X[7l``
DECODED CIPHER TEXT: Hello World of Encryption...
回答by Matthias Braun
I encountered this error since my encoded image started with data:image/png;base64,iVBORw0...
.
自从我的编码图像以data:image/png;base64,iVBORw0...
.
This answerled me to the solution:
这个答案让我找到了解决方案:
String partSeparator = ",";
if (data.contains(partSeparator)) {
String encodedImg = data.split(partSeparator)[1];
byte[] decodedImg = Base64.getDecoder().decode(encodedImg.getBytes(StandardCharsets.UTF_8));
Path destinationFile = Paths.get("/path/to/imageDir", "myImage.jpg");
Files.write(destinationFile, decodedImg);
}
回答by Esakkiappan .E
Just use the below code to resolve this:
只需使用下面的代码来解决这个问题:
JsonObject obj = Json.createReader(new ByteArrayInputStream(Base64.getDecoder().decode(accessToken.split("\.")[1].
replace('-', '+').replace('_', '/')))).readObject();
In the above code replace('-', '+').replace('_', '/')
did the job. For more details see the https://jwt.io/js/jwt.js. I understood the problem from the part of the code got from that link:
在上面的代码中replace('-', '+').replace('_', '/')
完成了这项工作。有关更多详细信息,请参阅https://jwt.io/js/jwt.js。我从该链接获得的代码部分理解了问题:
function url_base64_decode(str) {
var output = str.replace(/-/g, '+').replace(/_/g, '/');
switch (output.length % 4) {
case 0:
break;
case 2:
output += '==';
break;
case 3:
output += '=';
break;
default:
throw 'Illegal base64url string!';
}
var result = window.atob(output); //polifyll https://github.com/davidchambers/Base64.js
try{
return decodeURIComponent(escape(result));
} catch (err) {
return result;
}
}
回答by twasbrillig
I got this error for my Linux Jenkins slave. I fixed it by changing from the node from "Known hosts file Verification Strategy" to "Non verifying Verification Strategy".
我的 Linux Jenkins slave 出现了这个错误。我通过将节点从“已知主机文件验证策略”更改为“非验证验证策略”来修复它。