WSO2 encrypt/decrypt 密码(字符串)和 public-私钥
WSO2 encrypt/decrypt password (string) with public-private keys
在 BAM 配置文件中的 WSO2 ESB (4.8.1) 中,当我导出它(BAM 配置文件)时,我注意到密码已加密。我发现如果我有私钥和 public 密钥,我可以 encrypt/decrypt 密码,所以我这样写:
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 配置文件密码中时,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 存储库中有一个 class,我可以在那里找到它是如何工作的?
更新
我注意到在我的代码中每次 运行 代码时我都会得到新的加密密码,但在 WSO2 ESB 中我总是得到相同的字符串。
当 encrypting/encoding 和 decoding/decrypting 时,请尝试以下操作:
org.wso2.carbon.core.util.CryptoUtil.getDefaultCryptoUtil().encryptAndBase64Encode(value.getBytes())
到decode/decrypt,使用方法:
base64DecodeAndDecrypt()
您必须为 org.wso2.carbon.core(或 org.wso2.carbon.utils 添加依赖项,尝试这两个)才能使其正常工作(检查您的 <product>/repository/components/plugins
以找到依赖项的正确版本)
我不确定这是否适用于您的情况,但大多数碳基产品都是这样做的。
在 BAM 配置文件中的 WSO2 ESB (4.8.1) 中,当我导出它(BAM 配置文件)时,我注意到密码已加密。我发现如果我有私钥和 public 密钥,我可以 encrypt/decrypt 密码,所以我这样写:
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 配置文件密码中时,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 存储库中有一个 class,我可以在那里找到它是如何工作的?
更新
我注意到在我的代码中每次 运行 代码时我都会得到新的加密密码,但在 WSO2 ESB 中我总是得到相同的字符串。
当 encrypting/encoding 和 decoding/decrypting 时,请尝试以下操作:
org.wso2.carbon.core.util.CryptoUtil.getDefaultCryptoUtil().encryptAndBase64Encode(value.getBytes())
到decode/decrypt,使用方法:
base64DecodeAndDecrypt()
您必须为 org.wso2.carbon.core(或 org.wso2.carbon.utils 添加依赖项,尝试这两个)才能使其正常工作(检查您的 <product>/repository/components/plugins
以找到依赖项的正确版本)
我不确定这是否适用于您的情况,但大多数碳基产品都是这样做的。