Java RSA-bouncycastle PEMReader返回PEMKeyPair,而不是用于读取私钥的非对称密码密钥对

Java RSA-bouncycastle PEMReader返回PEMKeyPair,而不是用于读取私钥的非对称密码密钥对,java,openssl,rsa,bouncycastle,private-key,Java,Openssl,Rsa,Bouncycastle,Private Key,我有一个函数可以成功读取openssl格式的私钥: static AsymmetricKeyParameter readPrivateKey(string privateKeyFileName) { AsymmetricCipherKeyPair keyPair; using (var reader = File.OpenText(privateKeyFileName)) keyPair = (AsymmetricCipherKeyPair)new PemRea

我有一个函数可以成功读取openssl格式的私钥:

static AsymmetricKeyParameter readPrivateKey(string privateKeyFileName)
{
    AsymmetricCipherKeyPair keyPair;

    using (var reader = File.OpenText(privateKeyFileName))
        keyPair = (AsymmetricCipherKeyPair)new PemReader(reader).ReadObject();

    return keyPair.Private;
}
并返回一个AsymmetricKeyParameter,然后用于解密加密文本

下面是解密代码:

public static byte[] Decrypt3(byte[] data, string pemFilename)
{
    string result = "";
    try {
        AsymmetricKeyParameter key = readPrivateKey(pemFilename);

        RsaEngine e = new RsaEngine();

        e.Init(false, key);
        //byte[] cipheredBytes = GetBytes(encryptedMsg);

        //Debug.Log (encryptedMsg);

        byte[] cipheredBytes = e.ProcessBlock(data, 0, data.Length);
        //result = Encoding.UTF8.GetString(cipheredBytes);
        //return result;
        return cipheredBytes;

    } catch (Exception e) {
        Debug.Log ("Exception in Decrypt3: " + e.Message);
        return GetBytes(e.Message);
    }
}
这些在C#中工作,使用bouncy castle库,我得到了正确的解密文本。但是,当我将其添加到Java中时,PEMParser.readObject()返回一个PEMKeyPair类型的对象,而不是asymmetricipherkeypair类型的对象,Java在尝试转换它时抛出一个异常。我签入了C#,它实际上返回了不对称密码密钥对

我不知道为什么Java的行为会有所不同,但我希望这里的人能够帮助我们如何强制转换这个对象或读取privatekey文件并成功解密。我在C#和Java代码中使用了相同的public和privatekey文件,所以我不认为错误来自它们

以下是我如何阅读privatekey的Java版本,以供参考:

public static String readPrivateKey3(String pemFilename) throws FileNotFoundException, IOException
{
    AsymmetricCipherKeyPair keyParam = null;
    AsymmetricKeyParameter keyPair = null;
    PEMKeyPair kp = null;
    //PrivateKeyInfo pi = null;

    try {
        //var fileStream = System.IO.File.OpenText(pemFilename);
        String absolutePath = "";
        absolutePath = Encryption.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        absolutePath = absolutePath.substring(0, (absolutePath.lastIndexOf("/")+1));
        String filePath = "";
        filePath = absolutePath + pemFilename;

        File f = new File(filePath);
        //return filePath;

        FileReader fileReader  = new FileReader(f);
        PEMParser r = new PEMParser(fileReader);

        keyParam = (AsymmetricCipherKeyPair) r.readObject();

        return keyParam.toString();

    }
    catch (Exception e) {
        return "hello: " + e.getMessage() + e.getLocalizedMessage() + e.toString();
        //return e.toString();
        //return pi;
    }
}

Java代码已经更新为一个新的API,该API尚未移植到C#。您可以尝试等效的(但现在已弃用)JavaPemReader类。但它将返回一个JCE密钥对(更改的部分原因是因为原始版本只处理JCE类型,而不是BC轻量级类)

如果使用PEMParser,并且您得到了一个PEMKeyPair,那么您可以使用org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter.getKeyPair从中获得一个JCE密钥对。理想情况下会有一个BCPEMKeyConverter,但它似乎还没有被编写出来。在任何情况下,制作不对称密码密钥对都应该很容易:

PEMKeyPair kp = ...;
AsymmetricKeyParameter privKey = PrivateKeyFactory.createKey(kp.getPrivateKeyInfo());
AsymmetricKeyParameter pubKey = PublicKeyFactory.createKey(kp.getPublicKeyInfo());
new AsymmetricCipherKeyPair(pubKey, privKey);

这些工厂类位于org.bouncycastle.crypto.util包中。

是的,这绝对有效!非常感谢你。你几乎救了我的命!:)