public class RSAEncrypt { private static final Provider PROVIDER = new BouncyCastleProvider(); public static PrivateKey getPrivateKey(String key) throws Exception { byte[] keyBytes; keyBytes = Base64.decodeBase64(key); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PrivateKey privateKey = keyFactory.generatePrivate(keySpec); return privateKey; } //私钥解密 public static byte[] decryptRSA(PrivateKey privateKey, byte[] data) { try { Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", PROVIDER); cipher.init(Cipher.DECRYPT_MODE, privateKey); return cipher.doFinal(data); } catch (Exception e) { return null; } } public static String decrypt(PrivateKey privateKey, String text) { byte[] data = decryptRSA(privateKey, Base64.decodeBase64(text)); return data != null ? new String(data) : null; } public static String decrypt(String privateKeyText, String text) throws Exception { PrivateKey privateKey = getPrivateKey(privateKeyText); return decrypt(privateKey,text); } }
时间: 2024-02-14 10:36:13 浏览: 23
这是一个Java类,用于RSA加密算法中的私钥解密操作。它包含了以下方法:
1. `getPrivateKey(String key)` 方法用于将Base64编码的私钥字符串转换为`PrivateKey`对象。
2. `decryptRSA(PrivateKey privateKey, byte[] data)` 方法用于使用私钥解密给定的密文数据,并返回解密后的明文数据。
3. `decrypt(PrivateKey privateKey, String text)` 方法用于将Base64编码的密文字符串解密为明文字符串。
4. `decrypt(String privateKeyText, String text)` 方法用于将Base64编码的私钥字符串和密文字符串一起解密为明文字符串。
该类中使用了BouncyCastleProvider提供的加密算法库,实现了RSA/ECB/PKCS1Padding的解密方式。在使用时,需要传入正确的私钥和密文数据。
相关问题
java 怎么从.p12文件中获取私钥并返回PrivateKey privateKey
你可以使用以下代码从.p12文件中获取私钥并返回PrivateKey对象:
```java
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.Certificate;
public class KeyStoreExample {
public static PrivateKey getPrivateKey(String p12FilePath, String password, String alias, String keyPassword) {
try {
// 加载.p12文件
FileInputStream fis = new FileInputStream(p12FilePath);
KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(fis, password.toCharArray());
// 获取私钥
KeyStore.PasswordProtection keyPasswordProtection = new KeyStore.PasswordProtection(keyPassword.toCharArray());
KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry) keyStore.getEntry(alias, keyPasswordProtection);
PrivateKey privateKey = privateKeyEntry.getPrivateKey();
return privateKey;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String p12FilePath = "path/to/your/file.p12";
String password = "password";
String alias = "alias";
String keyPassword = "keyPassword";
PrivateKey privateKey = getPrivateKey(p12FilePath, password, alias, keyPassword);
if (privateKey != null) {
System.out.println("私钥: " + privateKey);
}
}
}
```
在上面的代码中,你需要将"path/to/your/file.p12"替换为你实际的.p12文件路径,"password"替换为.p12文件的密码,"alias"替换为私钥的别名,"keyPassword"替换为私钥的密码。运行代码后,你将会得到PrivateKey对象。
public static String encryptRSA(String encodeValue) { String decryptValue = ""; try { String privateKey = CryptoRsaKey_NEO.getPrivateKey(); RSAPrivateKey privateKey1 = (RSAPrivateKey)getPrivateKeyDec(privateKey); byte[] decBytesBase64 = encodeValue.getBytes(); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.DECRYPT_MODE, privateKey1); byte[] resultBytes = cipher.doFinal(decBytesBase64); String aaa = new String(resultBytes,"UTF-8"); String bbb = Arrays.toString(resultBytes); System.out.println("Base64‘OaaaF" + aaa); System.out.println("Base64‘ObbbF" + bbb); decryptValue = new String(java.util.Base64.getEncoder().encode(resultBytes)); } catch (Exception e) { WPLErrUtil.logger("CipherUtil", "decryptRSA", "RSA" + encodeValue + "," + e.getMessage() + ":" + e.toString()); return null; } return decryptValue; }请根据上面的解密方法写出对应的加密方法
根据上面的解密方法,可以编写对应的加密方法如下:
```
public static String encryptRSA(String plainValue) {
String encryptValue = "";
try {
String publicKey = CryptoRsaKey_NEO.getPublicKey();
RSAPublicKey publicKey1 = (RSAPublicKey)getPublicKeyDec(publicKey);
byte[] plainBytes = plainValue.getBytes("UTF-8");
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey1);
byte[] resultBytes = cipher.doFinal(plainBytes);
encryptValue = java.util.Base64.getEncoder().encodeToString(resultBytes);
} catch (Exception e) {
WPLErrUtil.logger("CipherUtil", "encryptRSA", "RSA" + plainValue + "," + e.getMessage() + ":" + e.toString());
return null;
}
return encryptValue;
}
```
这个方法接收一个字符串参数作为明文,返回一个字符串作为密文。它使用了与解密方法中相同的公钥和填充方式,然后将明文转换为字节数组,使用公钥进行加密,并将结果字节数组转换为Base64编码的字符串。