【发布时间】:2016-09-25 01:35:22
【问题描述】:
我已经为加密/解密编写了一个非常基本的程序,并且现在尝试只加密单个字符串。 加密工作正常,但在解密中却抛出错误
javax.crypto.BadPaddingException:解密错误。
下面一行报错
byte[] decodedData = (rsa.doFinal(decodedValue));
我尝试了多种方法并经历了很多线程,但无法找到解决方案。有人可以帮我解决这个问题吗?
这个类很简单,只有4个方法,第一个是测试方法,第二个是keystore加载方法,剩下的两个方法是加密和解密。
package XXXX;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
public class EncryptDecryptUtil {
private String publicKeyStoreFileName = "C:\\Program Files\\Java\\jdk1.8.0_51\\jre\\lib\\security\\cacerts";
private String pubKeyStorePwd = "XXX";
private String pubKeyAlias="XXXX";
private static final String JKS = "JKS";
private static final int CONST_16 = 16;
public String TestMethod(final String clearText) throws InvalidKeyException, NoSuchAlgorithmException,
NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, KeyStoreException,
CertificateException, IOException, UnrecoverableKeyException {
byte[] ecryptedAESKey = generateEncryptedData("TEST");
System.out.println("Encrypted Key = " + ecryptedAESKey);
System.out.println("Decrypted Key = " + generateDecryptedData(ecryptedAESKey));
return generateDecryptedData(ecryptedAESKey);
}
private KeyStore loadKeyStore() throws KeyStoreException, NoSuchAlgorithmException, CertificateException,
IOException {
KeyStore keystore = KeyStore.getInstance(JKS);
FileInputStream tmp = new FileInputStream(publicKeyStoreFileName);
keystore.load(tmp, pubKeyStorePwd.toCharArray());
tmp.close();
return keystore;
}
private byte[] generateEncryptedData(final String data) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException,
KeyStoreException, CertificateException, IOException, UnrecoverableKeyException {
Base64 base64 = new Base64();
X509Certificate cert;
KeyStore keystore = loadKeyStore();
cert = (java.security.cert.X509Certificate) keystore.getCertificate(pubKeyAlias);
Cipher rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding");
rsa.init(Cipher.ENCRYPT_MODE, cert);
byte[] ecrypteddata = (base64.encode(rsa.doFinal(data.getBytes(StandardCharsets.UTF_8))));
return ecrypteddata;
}
public String generateDecryptedData(final byte[] encryptedData) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException,
KeyStoreException, CertificateException, IOException, UnrecoverableKeyException {
Base64 base64 = new Base64();
X509Certificate cert;
KeyStore keystore = loadKeyStore();
cert = (java.security.cert.X509Certificate) keystore.getCertificate(pubKeyAlias);
Cipher rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding");
rsa.init(Cipher.DECRYPT_MODE, cert);
byte[] decodedValue = base64.decode(encryptedData);
byte[] decodedData = (rsa.doFinal(decodedValue));
return new String(decodedData);
}
}
【问题讨论】:
-
您是否使用 UTF-8 作为系统编码?也许不是......只需尝试运行 byte[] ecryptedAESKey = generateEncryptedData(new String("TEST", "UTF-8"));
-
感谢 Jurgen 的回复..!!我已经尝试过不使用 UTF-8(只是 data.getBytes())以及使用 UTF-8。还尝试将数据从加密器传递到解密器作为字节数组,作为字符串作为十六进制数据但没有成功,同样的错误
标签: java encryption cryptography