Java 8 - Base64编码生成无法解码的“==”
Java 8 - Base64 encoding generates "==" which can not be decoded
我目前正在开发一个 Server/Client 系统,它应该:
- 发送用户名和密码到服务器
- 向客户端发送一个未定义的大文件
我想用 AES 编码这两个步骤,然后再用 Base64 编码。第一步已经有效,但如果我想发送文件,Base64 编码会生成一个“==”,这会导致解码器认为这是文件的结尾。
Class 用于我的编码
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;
}
}
如果我尝试在客户端中解码字符串时抛出的错误
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)
我之间用的是eclipse
如果我只使用像“拒绝访问”这样的消息,那也没有问题。
参见Encoder。
Base64.Encoder myencoder = Base64.getEncoder().withoutPadding();
使用 withoutPadding 会产生一个新的编码器,不会在文件末尾写入 =
填充字符。
我目前正在开发一个 Server/Client 系统,它应该:
- 发送用户名和密码到服务器
- 向客户端发送一个未定义的大文件
我想用 AES 编码这两个步骤,然后再用 Base64 编码。第一步已经有效,但如果我想发送文件,Base64 编码会生成一个“==”,这会导致解码器认为这是文件的结尾。
Class 用于我的编码
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;
}
}
如果我尝试在客户端中解码字符串时抛出的错误
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)
我之间用的是eclipse
如果我只使用像“拒绝访问”这样的消息,那也没有问题。
参见Encoder。
Base64.Encoder myencoder = Base64.getEncoder().withoutPadding();
使用 withoutPadding 会产生一个新的编码器,不会在文件末尾写入 =
填充字符。