c#bouncy castle中的rsa/ecb/oaepwith sha-256和mgf1 padding-输入对rsa密码太大

4ktjp1zp  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(619)

嗨,我正在尝试将一段java代码转换为c#,以便使用rsa密钥进行解密
java代码

import javax.crypto.Cipher;
import javax.crypto.spec.OAEPParameterSpec;
import javax.crypto.spec.PSource;

import sun.misc.BASE64Decoder;
public static String decryptWithRSAKey(String encryptedString, Key pk) throws Exception {

        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

        Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING");
        OAEPParameterSpec oaepParameterSpec = new OAEPParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT);
        cipher.init(Cipher.DECRYPT_MODE, pk,oaepParameterSpec);        
        return new String(cipher.doFinal(new BASE64Decoder().decodeBuffer(encryptedString)),"UTF-8");//1.6.031->rt.jar -> sun.misc.Base64Decoder 
    }

c代码

using javax.crypto;
using javax.crypto.spec;
using java.security.spec;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto.Encodings;
using java.security;
 public static String DecryptWithRSAKey(String encryptedString, Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters pk)
        {

            var decrypter = new OaepEncoding(new RsaEngine(), new Sha256Digest(), new Sha256Digest(), null);
            decrypter.Init(false, pk);           
            var encrypted = decrypter.ProcessBlock(System.Text.Encoding.UTF8.GetBytes(encryptedString), 0, encryptedString.Length);            
            return Base64Encoder.Encode(encrypted);            
        }

触发

PrivateKeyfilePath =PATH TO PRIVATE KEY
RSAPrivateKey privateKey1 = (RSAPrivateKey)objAc.GetPrivate(PrivateKeyfilePath);

var rsaPri1 = new Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters(true, new Org.BouncyCastle.Math.BigInteger(privateKey1.getModulus().ToString()),
                    new Org.BouncyCastle.Math.BigInteger(privateKey1.getPrivateExponent().ToString()));
String decryptedAESKeyString = RSAEncryptionWithAES.DecryptWithRSAKey(encryptedResponseKey, rsaPri1);

运行时,我得到一个错误输入太大的rsa密码
如何在c#bouncy castle中正确指定密码rsa/ecb/oaepwith sha-256和mgf1?
另外,对于rsa密码,错误输入是否太大?

j2qf4p5b

j2qf4p5b1#

因此,c代码在功能上与java代码相对应,在c代码中 encryptedString 必须在解密前进行base64解码(而不是utf8编码)。解密的数据必须是utf8解码(而不是base64编码):

public static string DecryptWithRSAKey(string encryptedString, RsaKeyParameters pk)
{
    var decrypter = new OaepEncoding(new RsaEngine(), new Sha256Digest(), new Sha256Digest(), null);
    decrypter.Init(false, pk);
    var encryptedBytes = Convert.FromBase64String(encryptedString);
    var decrypted = decrypter.ProcessBlock(encryptedBytes, 0, encryptedBytes.Length);
    return Encoding.UTF8.GetString(decrypted);
}

你也可以通过以下两个链接在线验证,https://dotnetfiddle.net/zi0n64 以及https://repl.it/@3hk8cl8h24hwis7/decimalnoteworthycharacters 用c#方法解密密文 DecryptWithRSAKey 以及java方法 decryptWithRSAKey ,分别执行(在第一次调用时,将加载依赖项,因此此执行可能需要更长的时间)。

相关问题