【问题标题】:Base64 encoded string to Public Key using RSA From C# to Java使用 RSA 从 C# 到 Java 的 Base64 编码字符串到公钥
【发布时间】:2016-03-16 12:59:34
【问题描述】:

我正在将遗留应用程序从 .net 转换为 java。使用公钥加密的传统方法。

string text = "<base64encodedstring here>";
IBuffer buffer = CryptographicBuffer.DecodeFromBase64String(text);
AsymmetricKeyAlgorithmProvider asymmetricKeyAlgorithmProvider = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.get_RsaPkcs1());
CryptographicKey cryptographicKey = asymmetricKeyAlgorithmProvider.ImportPublicKey(buffer, 3);
IBuffer buffer2 = CryptographicBuffer.ConvertStringToBinary(data, 0);
IBuffer buffer3 = CryptographicEngine.Encrypt(cryptographicKey, buffer2, null);
byte[] array;
CryptographicBuffer.CopyToByteArray(buffer3, ref array);
//return CryptographicBuffer.EncodeToBase64String(buffer3);

这是我将给定 文本 转换为公钥的 Java 代码

public static PublicKey getKey(String key) throws Exception{
    try{
        byte[] byteKey = Base64.getDecoder().decode(key);

        X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey);
        KeyFactory kf = KeyFactory.getInstance("RSA","BC");

        return kf.generatePublic(X509publicKey);

    }
    catch(Exception e){
       throw e;
    }

}

这是我的主要方法

public static void main(String[] args) {
    String text = "base64encodedstring";
    try {
        Security.addProvider(new BouncyCastleProvider());
        decode(text);
        PublicKey pubKey=getKey(text);
        byte[] input = "plaintext".getBytes();
        Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "BC");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        byte[] cipherText = cipher.doFinal(input);
        System.out.println("cipher: " + new String(cipherText));

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

但是当我尝试获取公钥时,出现以下异常

java.security.spec.InvalidKeySpecException: encoded key spec not recognised
    at org.bouncycastle.jcajce.provider.asymmetric.util.BaseKeyFactorySpi.engineGeneratePublic(Unknown Source)
    at org.bouncycastle.jcajce.provider.asymmetric.rsa.KeyFactorySpi.engineGeneratePublic(Unknown Source)
    at java.security.KeyFactory.generatePublic(Unknown Source)
    at com.test.EncryptionUtil.getKey(EncryptionUtil.java:38)
    at com.test.EncryptionUtil.main(EncryptionUtil.java:60)

我做错了吗?我是密码学新手。

通过更多研究,我发现了它在 C# 中的工作方式,但无法将其转换为 java

 public static CryptographicKey GetCryptographicPublicKeyFromCert(string strCert)

    {

        int length;

        CryptographicKey CryptKey = null;



        byte[] bCert = Convert.FromBase64String(strCert);



        // Assume Cert contains RSA public key

        // Find matching OID in the certificate and return public key

        byte[] rsaOID = EncodeOID("1.2.840.113549.1.1.1");

        int index = FindX509PubKeyIndex(bCert, rsaOID, out length);



        // Found X509PublicKey in certificate so copy it.

        if (index > -1)

        {

            byte[] X509PublicKey = new byte[length];

            Array.Copy(bCert, index, X509PublicKey, 0, length);



            AsymmetricKeyAlgorithmProvider AlgProvider = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaPkcs1);

            CryptKey = AlgProvider.ImportPublicKey(CryptographicBuffer.CreateFromByteArray(X509PublicKey));

        }



        return CryptKey;

    }

EncodeOID 方法的目的是什么以及如何在 Java 中实现它。下面的链接解释了 base64 编码的公钥字符串的创建并在 C# 中对其进行解码 http://blogs.msdn.com/b/stcheng/archive/2013/03/12/windows-store-app-how-to-perform-rsa-data-encryption-with-x509-certificate-based-key-in-windows-store-application.aspx

【问题讨论】:

  • 您能识别出您提供的代码中的哪些行是 EncryptionUtil.java 中的第 38 行和第 60 行吗?
  • @J.Murray Err,generatePublic() 调用是第 38 行,getKey() 调用是第 60 行。显然。
  • 第 60 行:PublicKey pubKey=getKey(text);
  • 第 38 行:PublicKey pubKey=getKey(text)

标签: java .net encryption rsa bouncycastle


【解决方案1】:

没有直接的方法可以将 microsoft Capi1PublicKey 读入 java。我首先在 WinRT 中将 Capi1PublicKey 转换为 X509 编码的公钥。然后我在java中使用了created key。

【讨论】:

    【解决方案2】:

    显然密钥不是 X509 编码的。了解它是如何编码的并使用适当的 KeySpec。

    【讨论】:

    • 我发现它使用的是微软的 Capi1PublicKey。关于它在 java 中的对应部分的任何想法
    【解决方案3】:

    C# 使用 AsymmetricAlgorithmNames。get_RsaPkcs1 您需要为您的 JAVA 代码找到等效项。

    你可能想看看这个Import Public RSA Key From Certificate

    【讨论】:

    • 我发现它使用的是微软的 Capi1PublicKey。关于它在 java 中的对应部分的任何想法。
    猜你喜欢
    • 2018-05-28
    • 2015-02-23
    • 2014-09-27
    • 2012-03-06
    • 1970-01-01
    • 2018-12-22
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多