C#等价于Java RSA / ECB / OAEPWithSHA-256AndMGF1Padding [英] C# equivalent to Java RSA/ECB/OAEPWithSHA-256AndMGF1Padding
问题描述
我试图在Java中的字符串加密和解密在C#。
我试图与RSA / ECB / PKCS1PADDING首先,它的工作方式像一个魅力,但现在我试图切换到OAEP填充,但我不能使它的工作。加密工作正常,但不是解密。我改变的唯一的事情是Java和C#中的算法名称我改变了rsa.Decrypt(data,true)从假到真。是否需要更多更改?
I am trying to encrypt at string in Java and decrypt in 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. 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. Does it require more changes?
我得到的例外是解码OAEP填充时出错。
The exception I get is "Error occurred while decoding OAEP padding".
我的Java加密方法:
My Java encryption method:
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;
}
我的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;
}
推荐答案
256不能使用C#。我改变了算法名为RSA / ECB / OAEPWithSHA-1AndMGF1Padding
,它工作!
It seems to be SHA-256 that's not working with C#. I changed the algorithm name to "RSA/ECB/OAEPWithSHA-1AndMGF1Padding"
, and it worked!
这篇关于C#等价于Java RSA / ECB / OAEPWithSHA-256AndMGF1Padding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!