Base64:java.lang.IllegalArgumentException:非法字符

我正在尝试在用户注册后发送确认电子邮件。 我正在使用JavaMail库和Java 8 Base64 util类。

我正在以下列方式编码用户电子邮件:

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"+ "\nlink"; MimeBodyPart link = new MimeBodyPart(headers, confirmLink.getBytes("UTF-8")); multipart.addBodyPart(link); 

其中confirmationURL是:

 private final static String confirmationURL = "http://localhost:8080/project/controller?command=confirmRegistration&ID="; 

然后以这种方式在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; } 

当我试图解码时:

 http://localhost:8080/project/controller?command=confirmRegistration&ID=[B@6499375d 

我得到java.lang.IllegalArgumentException: Illegal base64 character 5b

我尝试使用基本的编码/解码器(不是URL编码器)但没有成功。

解决了:

问题是下一个 – 在线:

  String confirmLink = "Complete your registration by clicking on following"+ "\nlink"; 

我在一个字节数组上调用toString,所以我应该做以下事情:

 String encodedEmail = new String(Base64.getEncoder().encode( user.getEmail().getBytes(StandardCharsets.UTF_8))); 

感谢Jon SkeetByteHamster

你的编码文本是[B@6499375d 。 这不是Base64, 编码时出错了。 那个解密代码看起来不错。

在将byte []添加到URL之前,使用此代码将其转换为String:

 String encodedEmailString = new String(encodedEmail, "UTF-8"); // ... String confirmLink = "Complete your registration by clicking on following" + "\nlink"; 

我遇到了这个错误,因为我的编码图像以data:image/png;base64,iVBORw0...开头data:image/png;base64,iVBORw0...

这个答案让我得到了解决方案:

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

只需使用以下代码即可解决此问题:

 JsonObject obj = Json.createReader(new ByteArrayInputStream(Base64.getDecoder().decode(accessToken.split("\\.")[1]. replace('-', '+').replace('_', '/')))).readObject(); 

在上面的代码中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; } } 

Base64.Encoder.encodeToString方法自动使用ISO-8859-1字符集。

对于我正在编写的加密实用程序,我使用密文的输入字符串和Base64对其进行编码以进行传输,然后反转该过程。 相关部分如下所示。 注意:我的file.encoding属性在调用JVM时设置为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(); } } 

输出如下:

 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...