WSO2使用公钥私钥加密/解密密码(字符串)
问题描述:
在BAM配置文件中的WSO2 ESB(4.8.1)中,当我导出它(BAM配置文件)时,我注意到密码已加密。我发现,如果我将有私钥和公钥,我可以加密/解密的密码,所以我写这篇文章:WSO2使用公钥私钥加密/解密密码(字符串)
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import java.io.FileInputStream;
import java.security.*;
import java.security.cert.Certificate;
public class Main {
public static void main(String[] argv) throws Exception {
Security.addProvider(new BouncyCastleProvider());
FileInputStream is = new FileInputStream("wso2carbon.jks");
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
keystore.load(is, "wso2carbon".toCharArray());
String alias = "wso2carbon";
Key key = keystore.getKey(alias, "wso2carbon".toCharArray());
if (key instanceof PrivateKey) {
Certificate cert = keystore.getCertificate(alias);
PublicKey publicKey = cert.getPublicKey();
String dataToBeEncrypted = "admin";
String adminToDecrypted = "kuv2MubUUveMyv6GeHrXr9il59ajJIqUI4eoYHcgGKf/BBFOWn96NTjJQI+wYbWjKW6r79S7L7ZzgYeWx7DlGbff5X3pBN2Gh9yV0BHP1E93QtFqR7uTWi141Tr7V7ZwScwNqJbiNoV+vyLbsqKJE7T3nP8Ih9Y6omygbcLcHzg=";
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
String encryptedData = Base64.encodeBase64String(cipher.doFinal(dataToBeEncrypted.getBytes()));
System.out.println("Encrypted Data: " + encryptedData);
Cipher dipher = Cipher.getInstance("RSA");
dipher.init(Cipher.DECRYPT_MODE, key);
System.out.println(new String(dipher.doFinal(Base64.decodeBase64(encryptedData))));
}
}
}
它工作正常,becasue“管理员”进行加密和解密后。但是,当我复制此加密值,并希望将其粘贴在我的BAM设定档的密码,ESB不能得到它,密码为空,并在控制台中我得到这个:
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,429]
Message: An invalid XML character (Unicode: 0x2) was found in the element content of the document.
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:598)
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.getElementText(XMLStreamReaderImpl.java:842)
at org.apache.axiom.util.stax.wrapper.XMLStreamReaderWrapper.getElementText(XMLStreamReaderWrapper.java:100)
at org.apache.axiom.om.impl.SwitchingWrapper.getElementText(SwitchingWrapper.java:962)
第二个问题是,当我尝试解密密码(WSO2 ESB通过出口BAM曲线加密,并且它“adminToDecrypted”)我得到这样的:
Exception in thread "main" javax.crypto.BadPaddingException: Decryption error
at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:380)
at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:291)
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:356)
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:382)
at javax.crypto.Cipher.doFinal(Cipher.java:2087)
at Main.main(Main.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
所以,也许我做错事或我错过一些重要的事情。也许还有其他方法可以做到这一点。也许在WSO2回购中有一个类可以找到它的工作原理?
UPDATE
我注意到,在我的代码我每次都获得新的加密密码,当我运行的代码,但在WSO2 ESB我总是得到相同的字符串。
答
当加密/编码和解码/解密,请尝试以下操作:
org.wso2.carbon.core.util.CryptoUtil.getDefaultCryptoUtil().encryptAndBase64Encode(value.getBytes())
为了解码/解密,使用方法:
base64DecodeAndDecrypt()
你必须添加一个依赖于组织.wso2.carbon.core(org.wso2.carbon.utils,尝试这两个)来得到这个工作(检查你的<product>/repository/components/plugins
找到正确的版本的依赖)
我不exac确定这是否适用于您的情况,但这是大多数碳基产品的做法。
谢谢你,它的工作原理! – Kacu