C# 解密Android中.net生成的RSA加密值

C# 解密Android中.net生成的RSA加密值,c#,android,.net,encryption,rsa,C#,Android,.net,Encryption,Rsa,我在这里浏览了很多帖子,但没有找到正确的解决方案。我想从Android解密一个用c#net加密的值 我已使用以下代码片段在.net平台中成功解密 public static void Main() { string _privateKey = Base64Decode("myprivatekey"); var rsa = new RSACryptoServiceProvider(); string data = "198,47,14

我在这里浏览了很多帖子,但没有找到正确的解决方案。我想从Android解密一个用c#net加密的值

我已使用以下代码片段在.net平台中成功解密

public static void Main()
{
    string _privateKey = Base64Decode("myprivatekey");

     var rsa = new RSACryptoServiceProvider();
       string data = "198,47,144,175,154,47,194,175,242,41,212,150,220,177,198,161,236,36,197,62,18,111,21,244,232,245,90,234,195,169,141,195,139,199,131,163,26,124,246,50,102,229,73,148,18,110,170,145,112,237,23,123,226,135,158,206,71,116,9,219,56,96,140,19,180,192,80,29,63,160,43,127,204,135,155,67,46,160,225,12,85,161,107,214,104,218,6,220,252,73,252,92,152,235,214,126,245,126,129,150,49,68,162,120,237,246,27,25,45,225,106,201,251,128,243,213,250,172,26,28,176,219,198,194,7,202,34,210";
        var dataArray = data.Split(new char[] { ',' });
        byte[] dataByte = new byte[dataArray.Length];
        for (int i = 0; i < dataArray.Length; i++)
        {
            dataByte[i] = Convert.ToByte(dataArray[i]);
        }

        rsa.FromXmlString(_privateKey);
        var decryptedByte = rsa.Decrypt(dataByte, false);
        Console.WriteLine(_encoder.GetString(decryptedByte));       
}

对于1024位(=128字节)密钥,密文应具有相同的长度。但是,C#代码中发布的密文只有99字节长,并且还包含
字节
类型不允许的值,例如543或823。由于C代码似乎在运行,请发布与C代码一起工作的一致测试数据(私有测试密钥和密文)。好的,我发布了实际值。你能告诉我在线工具,我可以在那里测试值吗?没有私钥,不可能复制!如果它是一个有效密钥,则创建一个测试密钥对和相关的密文,并发布两者。用于测试解密的网站还依赖于私钥格式(PKCS#1,PKCS#8,…)。发布的Java代码对解密没有意义:您试图从公钥派生私钥,这当然不起作用(或者你真的尝试用公钥解密,这当然也不起作用。)此外,你正在加密模式下使用
密码(
cipher.ENCRYPT_模式
)。但是如果没有完整的测试数据,我无法提供进一步的帮助。
        String modulusString = "hm2oRCtP6usJKYpq7o1K20uUuL11j5xRrbV4FCQhn/JeXLT21laKK9901P69YUS3bLo64x8G1PkCfRtjbbZCIaa1Ci/BCQX8nF2kZVfrPyzcmeAkq4wsDthuZ+jPInknzUI3TQPAzdj6gim97E731i6WP0MHFqW6ODeQ6Dsp8pc=";
        String publicExponentString = "AQAB";

        byte[] modulusBytes = Base64.decode(modulusString, DEFAULT);
        byte[] exponentBytes = Base64.decode(publicExponentString, DEFAULT);
        BigInteger modulus = new BigInteger(1, modulusBytes);
        BigInteger publicExponent = new BigInteger(1, exponentBytes);

        RSAPrivateKeySpec rsaPubKey = new RSAPrivateKeySpec(modulus, publicExponent);
        KeyFactory fact = KeyFactory.getInstance("RSA");
        PrivateKey pubKey = fact.generatePrivate(rsaPubKey);
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);

        byte[] plainBytes = result.getBytes("UTF-16LE");
        byte[] cipherData = cipher.doFinal(plainBytes);
        String encryptedStringBase64 = Base64.decode(cipherData, DEFAULT).toString();