我用java写了一个RSA加密实用程序,在本地PC上加解密就可以了。我将它部署到weblogic server 8.1,发现它无法正确解密字符串,并得到“javax.crypto.BadPaddingException: Decryption error”。
我将它部署到 apache-tomcat-9.0.10 并运行相同的程序,它工作正常。我错过了什么吗?有人可以给我解决方案或一些相关的关键字吗?
这是我的代码:
public class EncryptUtil {
private KeyPair keyPair;
private static EncryptUtil encryptUtil;
private Cipher cipher;
/** 加解密模組 */
public static EncryptUtil getEncryptUtil() {
if (encryptUtil == null) {
encryptUtil = new EncryptUtil();
}
return encryptUtil;
}
private EncryptUtil() {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
try {
//cipher = Cipher.getInstance("RSA");
cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
} catch (NoSuchAlgorithmException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (NoSuchPaddingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (keyPair == null) {
try {
keyPair = loadKeyPair();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 私鑰解密
*
* @param privateKey 私鑰
* @param encryptStr 解密內容
*/
public String decrypt(String encryptStr)
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, FormatException {
//Cipher cipher = Cipher.getInstance("RSA");
// 傳遞私鑰,設定為解密模式。
cipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivate());
// 解密器解密由Base64解碼後的密文,獲得明文位元組陣列
byte[] b = cipher.doFinal(new BASE64Decoder().decode(encryptStr));
// 轉換成字串
return new String(b);
}
/**
* 公鑰加密
*
* @param rawStr 要加密的內容
* @param publicKey 公鑰
* @return
*/
public String encrypt(String rawStr) throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
// 獲取一個加密演算法為RSA的加解密器物件cipher。
//Cipher cipher = Cipher.getInstance("RSA");
// 設定為加密模式,並將公鑰給cipher。
cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());
// 獲得密文
byte[] secret = cipher.doFinal(rawStr.getBytes());
// 進行Base64編碼並返回
return new BASE64Encoder().encode(secret);
}
/**
* 載入金鑰
*
* @param path 檔案路徑
*/
public KeyPair loadKeyPair()
throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
// Initiate the factory with specified algorithm.
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
// Read public key from file.
// File fileForPublicKey = Paths.get(path, "public.key").toFile();
File fileForPublicKey = new File("C://encrypt//public.key");
PublicKey publicKey = null;
FileInputStream PublicFis = null;
try {
PublicFis = new FileInputStream(fileForPublicKey);
byte[] loadedBytes = new byte[(int) fileForPublicKey.length()];
PublicFis.read(loadedBytes);
X509EncodedKeySpec spec = new X509EncodedKeySpec(loadedBytes);
publicKey = keyFactory.generatePublic(spec);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (PublicFis != null)
PublicFis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// Read private key from file.
// File fileForPrivateKey = Paths.get(path, "private.key").toFile();
File fileForPrivateKey = new File("C://encrypt//private.key");
PrivateKey privateKey = null;
FileInputStream PrivateFis = null;
try {
PrivateFis = new FileInputStream(fileForPrivateKey);
byte[] loadedBytes = new byte[(int) fileForPrivateKey.length()];
PrivateFis.read(loadedBytes);
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(loadedBytes);
privateKey = keyFactory.generatePrivate(privateKeySpec);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (PrivateFis != null)
PrivateFis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return new KeyPair(publicKey, privateKey);
}
}