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; }请根据上面的解密方法写出对应的加密方法
时间: 2024-03-02 12:53:21 浏览: 126
rsa-demo2021-10-8.zip
根据上面的解密方法,可以编写对应的加密方法如下:
```
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编码的字符串。
阅读全文