简体   繁体   English

C#等效于Java RSA / ECB / OAEPWithSHA-256AndMGF1Padding

[英]C# equivalent to Java RSA/ECB/OAEPWithSHA-256AndMGF1Padding

I am trying to encrypt at string in Java and decrypt in C#. 我试图在Java中对字符串进行加密,然后在C#中进行解密。 I tried with RSA/ECB/PKCS1PADDING first and it worked like a charm, but now I'm trying to switch to OAEP padding, but I cannot make it work. 我首先尝试使用RSA / ECB / PKCS1PADDING,但它的工作原理很吸引人,但是现在我试图切换到OAEP填充,但是我无法使其工作。 The encryption works fine but not the decryption. 加密可以正常工作,但不能解密。 The only things I changed was the algorithm name in Java and in C# I changed rsa.Decrypt(data, true) from false to true. 我唯一更改的是Java中的算法名称,在C#中,我将rsa.Decrypt(data,true)从false更改为true。 Does it require more changes? 需要更多更改吗?

The exception I get is "Error occurred while decoding OAEP padding". 我得到的异常是“解码OAEP填充时发生错误”。

My Java encryption method: 我的Java加密方法:

public byte[] rsaEncrypt(byte[] data) {

    byte[] cipherData;

    try {

        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(pubMod, pubExp);
        KeyFactory fact = KeyFactory.getInstance("RSA");
        PublicKey pubKey = fact.generatePublic(keySpec);

        Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        cipherData = cipher.doFinal(data);
        return cipherData;

    } catch (NoSuchAlgorithmException | IllegalBlockSizeException | InvalidKeyException | InvalidKeySpecException | NoSuchPaddingException | BadPaddingException e) {
        e.printStackTrace();
    }

    return null;
}

My C# decryption method: 我的C#解密方法:

private string RSADecrypt(byte[] data)
    {
        const string PrivateKey = *the key*;

        const int PROVIDER_RSA_FULL = 1;
        const string CONTAINER_NAME = "Tracker";

        CspParameters cspParams;
        cspParams = new CspParameters(PROVIDER_RSA_FULL);
        cspParams.KeyContainerName = CONTAINER_NAME;

        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspParams);

        rsa.FromXmlString(PrivateKey);

        byte[] decrypted = rsa.Decrypt(data, true);

        String decryptedString = System.Text.Encoding.UTF8.GetString(decrypted);

        return decryptedString;
    }

It seems to be SHA-256 that's not working with C#. 似乎是SHA-256无法与C#一起使用。 I changed the algorithm name to "RSA/ECB/OAEPWithSHA-1AndMGF1Padding" , and it worked! 我将算法名称更改为"RSA/ECB/OAEPWithSHA-1AndMGF1Padding" ,并且有效!

RSACryptoServiceProvider does not support OAEP-SHA2. RSACryptoServiceProvider不支持OAEP-SHA2。

.NET 4.6 added RSACng, which is capable of OAEP-SHA2 (256, 384, 512). .NET 4.6加入RSACng,其能够 OAEP-SHA2(256,384,512)的。 .NET 4.6 also changed the Encrypt/Decrypt and Sign/Verify signatures a bit to be more scalable than a Boolean, and moved them to the RSA base class: .NET 4.6还对“加密/解密”和“签名/验证”签名进行了一些更改,以使其比布尔型更具可伸缩性,并将其移至RSA基类:

using (RSA rsa = new RSACng())
{
    rsa.FromXmlString(privateKeyXml);
    byte[] decrypted = rsa.Decrypt(data, RSAEncryptionPadding.OaepSHA256);
    return Encoding.UTF8.GetString(decrypted);
}

If your private key comes from an X509Certificate2 instance the new GetRSAPrivateKey method (also in 4.6) will prefer a RSACng instance; 如果您的私钥来自X509Certificate2实例,则新的GetRSAPrivateKey方法(同样在4.6中)将首选RSACng实例; though the return type is deliberately not guaranteed... so if you must cast it you should use as instead of a hard cast. 尽管故意不能保证返回类型...因此,如果必须强制转换,则应使用as而不是强制强制转换。

暂无
暂无

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

相关问题 Java的RSA / ECB / OAEPWITHSHA-256ANDMGF1PADDING在PHP中等效 - Java's RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING equivalent in PHP C# Bouncy Castle 中的 RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING - 对于 RSA 密码输入太大 - RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING in C# Bouncy Castle - input too large for RSA cipher Java 的 RSA/ECB/OAEPWithSHA-256AndMGF1Padding 在 Node.js 中的等价物 - Java's RSA/ECB/OAEPWithSHA-256AndMGF1Padding equivalent in Node.js 获取 ruby 的“RSA/ECB/OAEPWithSHA-256AndMGF1Padding”组合 - Get this “RSA/ECB/OAEPWithSHA-256AndMGF1Padding” combination for ruby java RSA/ECB/OAEPWithSHA 256AndMGF1Padding 在 golang 中等效 - java RSA/ECB/OAEPWithSHA 256AndMGF1Padding equivalent in golang 分解 RSA/ECB/OAEPWithSHA-256AndMGF1Padding - Breaking down RSA/ECB/OAEPWithSHA-256AndMGF1Padding “RSA/ECB/OAEPWITHSHA256ANDMGF1PADDING”和“RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING”之间的区别是什么 - what is deference between "RSA/ECB/OAEPWITHSHA256ANDMGF1PADDING" and "RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING" RSA / ECB / OAEPWithSHA-256AndMGF1Padding但MGF1使用SHA-256? - RSA/ECB/OAEPWithSHA-256AndMGF1Padding but with MGF1 using SHA-256? 错误的填充例外-pkcs11中的RSA / ECB / OAEPWITHSHA-256ANDMGF1PADDING - Bad Padding Exception - RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING in pkcs11 找不到任何支持 RSA/None/OAEPWITHSHA-256ANDMGF1PADDING 的提供程序 - Cannot find any provider supporting RSA/None/OAEPWITHSHA-256ANDMGF1PADDING
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM