【问题标题】:WSO2 encrypt/decrypt password (string) with public-private keysWSO2 使用公私钥加密/解密密码(字符串)
【发布时间】:2015-07-31 03:21:21
【问题描述】:

在 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 配置文件密码中时,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 repo 中有一个类,我可以在其中了解它是如何工作的?

更新

我注意到在我的代码中我每次运行代码时都会得到新的加密密码,但在 WSO2 ESB 中我总是得到相同的字符串。

【问题讨论】:

    标签: security encryption cryptography wso2 wso2carbon


    【解决方案1】:

    在加密/编码和解码/解密时,请尝试以下操作:

    org.wso2.carbon.core.util.CryptoUtil.getDefaultCryptoUtil().encryptAndBase64Encode(value.getBytes())
    

    要解码/解密,使用方法:

    base64DecodeAndDecrypt()
    

    您必须为 org.wso2.carbon.core(或 org.wso2.carbon.utils,尝试这两个)添加一个依赖项才能使其正常工作(检查您的 <product>/repository/components/plugins 以找到正确的版本依赖)

    我不确定这是否适用于您的情况,但大多数碳基产品都是这样做的。

    【讨论】:

      猜你喜欢
      • 2011-06-17
      • 2015-11-02
      • 2013-06-08
      • 2015-01-25
      • 2021-04-01
      • 2019-12-15
      • 2014-08-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多