[英]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.