加密平台的行为是特定的-NotYetCommonsSSL java
我正试图使用这段代码并编写了一个测试用例——用secretkey1加密纯文本,用secretkey2解密输出将抛出BadPaddingException 但是,我观察到的行为是不同的加密平台的行为是特定的-NotYetCommonsSSL java,java,encryption,Java,Encryption,我正试图使用这段代码并编写了一个测试用例——用secretkey1加密纯文本,用secretkey2解密输出将抛出BadPaddingException 但是,我观察到的行为是不同的 当我在mac上运行它时,测试用例成功了 在linux上,测试用例失败,这意味着解密成功地输出了一个值 以下是我收集到的差异。感谢您为我们提供的帮助 雨衣 java版本“1.7.0_79” Java(TM)SE运行时环境(build 1.7.0_79-b15) Java HotSpot(TM)64位服务器虚拟机(
- 当我在mac上运行它时,测试用例成功了
- 在linux上,测试用例失败,这意味着解密成功地输出了一个值李>
}如果生成堆栈跟踪的代码与问题中包含的代码类似,则问题在于以下操作:
cipherText.getBytes(Charsets.UTF_8)
密文不是“文本”。这是一个不可预测的字节序列,通常无法解码为字符
如果需要将密文表示为可打印的字符串,请使用base-64编码或类似方法。“使用secretkey1加密纯文本并使用secretkey2解密输出将引发BadPaddingException。”您注意到使用的是AES吗?您的代码也不会使用不同的键。
import com.google.common.base.Charsets;
import java.io.IOException;
import java.security.GeneralSecurityException;
import javax.annotation.Nonnull;
import org.apache.commons.ssl.OpenSSL;
import static java.util.Objects.requireNonNull;
public class NotYetSslLibraryUsage
{
private final String secretKey;
public NotYetSslLibraryUsage(@Nonnull String secretKey)
{
this.secretKey = secretKey;
}
public String getPlainText(@Nonnull String encryptedText)
{
return new String(decrypt(encryptedText), Charsets.UTF_8);
}
private byte[] decrypt(@Nonnull String cipherText)
{
try
{
byte[] plainText = OpenSSL.decrypt("aes128", secretKey.toCharArray(), cipherText.getBytes(Charsets.UTF_8));
System.out.println(new String(plainText, Charsets.UTF_8));
return requireNonNull(plainText, "plainText must be non-null");
}
catch (IOException | GeneralSecurityException e)
{
System.out.println(e);
throw e;
}
}
public void encrypt(@Nonnull String messageToEncrypt) throws IOException, GeneralSecurityException
{
byte[] aes128s = OpenSSL.encrypt("aes128", secretKey.toCharArray(), messageToEncrypt.getBytes(Charsets.UTF_8));
System.out.println("===========================Encrypted=============================");
System.out.println(new String(aes128s, Charsets.UTF_8));
System.out.println("=================================================================");
}
cipherText.getBytes(Charsets.UTF_8)