使用RSA/ECB/OAEPWithSHA-256和MGF1填充的Java加密无法在.NET中解密



我们有一个要求,Java中进行加密,.NET进行解密。问题在于.NET中的不对称解密,Java中使用的算法、模式和填充,而加密是"RSA/ECB/OAEPWithSHA-256AndMGF1Padding"。在.NET上,我尝试过用OAEP填充以各种不同的方式解密RsaCng、Rsa、Chilkat Rsa、Bouncy Castle,但到目前为止都没有成功。以下是代码片段。只有当Java的加密方法改为"RSA/ECB/OAEPWithSHA-1AndMGF1Padding"时,.NET才能解密,但由于安全原因,Java无法改变加密方法。我们使用PEM文件进行解密,同时使用CERT进行加密。请告诉我们解决问题的最佳方法。这与创建基于SHA256哈希的PEM文件有关吗?如果有人能帮我,我将不胜感激。

错误如下所述。RsaCng和Rsa:-参数不正确。RsaCryptoService:-解码OAEP填充时出错。Bouncy Castle:-它给出了一个带有符号的alpha unicode字符串。Chilkat:-返回null。

非对称加密的Java代码段:-

// Encrypt the symetric key
X509Certificate cert = ConduentCertificateLocator.getConduentCertificate();
PublicKey certKey = cert.getPublicKey();
Cipher c2 = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING");
//Cipher c2 = Cipher.getInstance("RSA");
c2.init(Cipher.ENCRYPT_MODE, certKey);
byte[] inputCertKey = key.getEncoded();
byte[] encryptedKey = c2.doFinal(inputCertKey);
String encryptedKeyUrl = encodeToURL(encryptedKey);

.NET非对称解密代码段:-我们确实尝试了几种不同的方法。下面是代码。

a。RSACryptoService

using (RSACryptoServiceProvider csp = new RSACryptoServiceProvider())
{
using (var stream = File.OpenRead(_appSettings.PEM))
using (var reader = new PemUtils.PemReader(stream))
{
var rsaParameters = reader.ReadRsaKey();
csp.ImportParameters(rsaParameters);
var resultBytes = csp.Decrypt(ksBytes, true);
finalResult = Convert.ToBase64String(resultBytes);
}
}

b。RsaCng

using (RSA rsa = new RSACng())
{
using (var stream = File.OpenRead(_appSettings.PEM))
using (var reader = new PemUtils.PemReader(stream))
rsa.ImportParameters(reader.ReadRsaKey());
byte[] decrypted2 = rsa.Decrypt(ksBytes, RSAEncryptionPadding.OaepSHA256);
var ss = Encoding.UTF8.GetString(decrypted2);
}

c。Rsa(系统安全加密(

using (RSA rsa = RSA.Create())
{
using (var stream = File.OpenRead(_appSettings.PEM))
using (var reader = new PemUtils.PemReader(stream))
{
var rsaParameters = reader.ReadRsaKey();
rsa.ImportParameters(rsaParameters);
}

finalResult = rsa.Decrypt(ksBytes, RSAEncryptionPadding.OaepSHA256);
}

d。Chilkat

using (PrivateKey pkey = new PrivateKey())
{
pkey.LoadPemFile(_appSettings.PEMFile);
key = pkey.GetXml();
}
using (Chilkat.Rsa decryptor = new Chilkat.Rsa())
{
decryptor.ImportPrivateKey(key);
// Make sure we have the same settings used for encryption.
decryptor.OaepPadding = true;
decryptor.EncodingMode = "hex";
decryptor.OaepHash = "sha-256";
string eStr = decryptor.DecryptStringENC(ks, true);
}

e。BouncyCastle

AsymmetricCipherKeyPair keyPair;
using (var reader = File.OpenText(_appSettings.PEM))
keyPair = (AsymmetricCipherKeyPair)new 
Org.BouncyCastle.OpenSsl.PemReader(reader).ReadObject();
var decryptEngine = new OaepEncoding(new RsaBlindedEngine(), new Sha256Digest(), new 
Sha1Digest(), null);
decryptEngine.Init(false, keyPair.Private);
var decrypted = Encoding.UTF8.GetString(decryptEngine.ProcessBlock(ksBytes, 0, 
ksBytes.Length));

我的场景有点不同,但它应该会对您有所帮助。我需要用java解密,而它是用.NET加密的。所以提出了与此相反的问题,但就像其他人提到的一样。即使指定RSA/ECB/OAEPWithSHA-256AndMGF1Padding,幕后Java也默认为幕后SHA-1。对我来说,诀窍是传递一个额外的参数,告诉它实际使用SHA-256。

JAVA:

private static byte[] decryptBytes(byte[] buffer, Key key) throws InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException {
Cipher rsa;
rsa = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
rsa.init(Cipher.DECRYPT_MODE, key, new OAEPParameterSpec("SHA-256", "MGF1", new MGF1ParameterSpec("SHA-256"), PSource.PSpecified.DEFAULT));
return rsa.doFinal(buffer);
}

.NET

var encryptedBytes = publicKey.Encrypt(bytesToEncrypt, RSAEncryptionPadding.OaepSHA256);

相关内容

  • 没有找到相关文章

最新更新