简体   繁体   English

Java 8 - Base64 编码生成无法解码的“==”

[英]Java 8 - Base64 encoding generates “==” which can not be decoded

I am currently working on a Server/Client system which should:我目前正在开发一个服务器/客户端系统,它应该:

  • send username and password to Server将用户名和密码发送到服务器
  • send a undefined large file to the Client向客户端发送一个未定义的大文件

I want to encode these two steps with an AES and after that an Base64 encoding.我想用 AES 对这两个步骤进行编码,然后是 Base64 编码。 The first step already works, but if I want to send the file, the Base64 encoding generates an "==" which causes the decoder to think this is the end of the file.第一步已经有效,但如果我想发送文件,Base64 编码会生成一个“==”,这会导致解码器认为这是文件的结尾。


Class which is used for my coding用于我的编码的类

package tools;

import java.security.MessageDigest;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class AES {
    public static String cryptString(String toCrypt) {
        String ret = "";
        try {
            String keyStr = "key";
            byte[] key = keyStr.getBytes("ASCII");
            MessageDigest sha = MessageDigest.getInstance("MD5");
            key = sha.digest(key);
            SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
            byte[] crypt = cipher.doFinal(toCrypt.getBytes("ASCII"));
            Base64.Encoder myencoder = Base64.getEncoder();
            String crypted = myencoder.encodeToString(crypt);
            ret = new String(crypted).trim();
        } catch (Exception e) {
            e.printStackTrace();
        }
    return ret;
    }

    public static String decryptString(String crypted) {
        String ret = "";
        try {
            String keyStr = "key";
            byte[] key = keyStr.getBytes("ASCII");
            MessageDigest sha = MessageDigest.getInstance("MD5");
            key = sha.digest(key);
            SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
            Base64.Decoder myDecoder = Base64.getDecoder();
            byte[] encrypt = myDecoder.decode(crypted.trim().getBytes("ASCII"));
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
            ret = new String(cipher.doFinal(encrypt)).trim();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }
}

The error which is thrown if I try to decode the string in the Client如果我尝试解码客户端中的字符串,则会引发错误

java.lang.IllegalArgumentException: Input byte array has incorrect ending byte at 856
    at java.util.Base64$Decoder.decode0(Unknown Source)
    at java.util.Base64$Decoder.decode(Unknown Source)
    at tools.AES.decryptString(AES.java:39)

I am using eclipse between.我在两者之间使用日食。
If I use only a Message Like "Access Denied" there isn't a problem, too.如果我只使用“拒绝访问”之类的消息,也没有问题。

See Encoder .请参阅编码器

Base64.Encoder myencoder = Base64.getEncoder().withoutPadding();

Using withoutPadding yields a new Encoder not writing = pad chars at the end of file.使用 withoutPadding 会产生一个新的编码器,不会在文件末尾写入= pad 字符。

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

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