在BAM配置文件中的WSO2 ESB(4.8.1)中,当我导出它(BAM配置文件)时,我注意到密码已加密。我发现如果我有私钥和公钥我可以加密/解密密码,所以我写这个:
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))));
}
}
}
它工作正常,因为'admin'经过加密和解密。但是当我复制这个加密值并希望将其粘贴到我的BAM profle密码中时,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回购中有一个类我可以找到它是如何工作的?
更新
我注意到在我的代码中,每次运行代码时都会获得新的加密密码,但在WSO2 ESB中,我总是得到相同的字符串。
答案 0 :(得分:1)
加密/编码和解码/解密时,请尝试以下操作:
org.wso2.carbon.core.util.CryptoUtil.getDefaultCryptoUtil().encryptAndBase64Encode(value.getBytes())
要解码/解密,请使用以下方法:
base64DecodeAndDecrypt()
您必须为org.wso2.carbon.core(或org.wso2.carbon.utils,尝试这两种方法)添加一个依赖项才能使其正常工作(检查您的<product>/repository/components/plugins
以找到正确的依赖的版本)
我不确定这是否适用于您的情况,但这是大多数碳基产品的完成方式。