简体   繁体   English

使用RSA从C#到Java的Base64编码的字符串到公钥

[英]Base64 encoded string to Public Key using RSA From C# to Java

I am converting legacy application from .net to java. 我正在将旧版应用程序从.net转换为Java。 Legacy method using encryption using public key. 使用公钥加密的旧方法。

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);

Here is my Java code to convert the given text into the public key 这是我的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;
    }

}

Here is my main method 这是我的主要方法

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();
    }
}

But When I try to get the public key I get the exception below 但是当我尝试获取公钥时,出现以下异常

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)

Am I doing something wrong? 难道我做错了什么? I am new to cryptography. 我是密码学的新手。

With some more research I found the way how it is doing in C#, but not able to convert it into java 通过更多研究,我发现了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;

    }

What is the purpose of EncodeOID method and how it can be achieved in Java. EncodeOID方法的目的是什么,以及如何在Java中实现。 The link below explains the creation of base64 encoded public key string and decode it in 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 下面的链接说明了base64编码的公共密钥字符串的创建并在C#中对其进行解码http://blogs.msdn.com/b/stcheng/archive/2013/03/12/windows-store-app-how-to-perform -RSA数据加密,与-X509的基于证书的琴键在窗口店,application.aspx

Obviously the key isn't X509-encoded. 显然,该密钥不是X509编码的。 Find out how it is encoded and use an appropriate KeySpec. 找出编码方式并使用适当的KeySpec。

C# uses AsymmetricAlgorithmNames. C#使用AsymmetricAlgorithmNames。 get_RsaPkcs1 you need to find equivalent for your JAVA code. get_RsaPkcs1您需要找到与您的JAVA代码等效的代码。

You may want to have look at this Import Public RSA Key From Certificate 您可能需要查看此从证书导入公共RSA密钥

There is no direct way to read microsoft Capi1PublicKey into java. 没有将Microsoft Capi1PublicKey读入Java的直接方法。 I first converted the Capi1PublicKey to X509-encoded public key in WinRT. 我首先在WinRT中将Capi1PublicKey转换为X509编码的公钥。 then I used created key in java. 然后我在Java中使用了创建的密钥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM