【问题标题】:Base64: java.lang.IllegalArgumentException: Illegal characterBase64:java.lang.IllegalArgumentException:非法字符
【发布时间】:2015-04-19 11:33:36
【问题描述】:

我正在尝试在用户注册后发送确认电子邮件。为此,我使用 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"+ "\n<a href='" + confirmationURL + encodedEmail + "'>link</a>";
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"+ "\n<a href='" + confirmationURL + encodedEmail + "'>link</a>";

我在一个字节数组上调用 toString,所以我应该执行以下操作:

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

感谢 Jon SkeetByteHamster

【问题讨论】:

  • 出于好奇,为什么.getBytes(StandardCharsets.UTF_8).getBytes("UTF-8") 在同一个函数中?对我来说似乎缺乏一致性:|
  • 提示:你在byte[]上调用toString()。你不想那样做。
  • @JonSkeet 是的!我认为这会导致问题。我应该如何改变它?
  • @JonSkeet 也许我应该调用 new String(encodedEmail)
  • 或者更好的是,使用编码为字符串而不是字节 [] 的 base64 API。 iharder.net/base64 的图书馆还不错……

标签: java base64 jakarta-mail registration


【解决方案1】:

您的编码文本是[B@6499375d。那不是 Base64,编码时出了点问题。那个解码代码看起来不错。

在将字节[] 添加到 URL 之前,使用此代码将其转换为字符串:

String encodedEmailString = new String(encodedEmail, "UTF-8");
// ...
String confirmLink = "Complete your registration by clicking on following"
    + "\n<a href='" + confirmationURL + encodedEmailString + "'>link</a>";

【讨论】:

  • 正如 Jon Skeet 指出的那样,我在一系列咬合上调用 toString。这就是原因
【解决方案2】:

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

This answer 引导我找到解决方案:

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.png");
  Files.write(destinationFile, decodedImg);
}

该代码删除了 Base64 编码图像前面的元数据,并将 Base64 字符串传递给 Java 的 Base64.Decoder 以获取图像作为字节。

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

      • 基本上尝试将加密数据显示为文本不起作用,因为存在不具有可打印表示的字节值。在这种情况下,“�”字符。因此,加密数据最好以十六进制显示。
      • @zaph 这只是调试代码,但是如果您的加密输出已经是可打印的字符,那么将加密数据显示为文本就可以了。在这种情况下,它们并非都是可打印的,但也无关紧要。
      • 是什么让你觉得我不懂编码?它只是一个完整性检查输出,以查看它是否已加密,因此,不可打印的字符比整齐格式化的 %02x 字符更好地表示。输出是我一个人的,唯一的开发者。
      • 鉴于这种蹩脚的反驳,我已将 cmets 添加到我的源代码中。
      【解决方案5】:

      我的 Linux Jenkins 从站出现此错误。我通过将节点从“已知主机文件验证策略”更改为“非验证验证策略”来修复它。

      【讨论】:

        【解决方案6】:

        我在这里添加了我的失败和发现以及我是如何克服这个问题的。 我最近一直在使用 SpringBoot 进行 JASYPT 加密,并且在此过程中遇到了很多问题并修复了它们。 对于非法字符异常的这个问题,我已经记录了my answer 是这里的另一个线程。

        另外,我在这里添加了another answer,它与 JASYP 加密有关,它与这个问题无关,只是对相同上下文的引用。随意跳过。并希望这些发现对您有所帮助并节省一些时间。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-08-28
          • 2016-09-26
          • 1970-01-01
          • 2018-02-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多