Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java RSA/ECB/OAEPWITHHA-256和MGF1填充在C#Bouncy Castle中-输入对RSA密码来说太大_Java_C#_Rsa_Bouncycastle - Fatal编程技术网

Java RSA/ECB/OAEPWITHHA-256和MGF1填充在C#Bouncy Castle中-输入对RSA密码来说太大

Java RSA/ECB/OAEPWITHHA-256和MGF1填充在C#Bouncy Castle中-输入对RSA密码来说太大,java,c#,rsa,bouncycastle,Java,C#,Rsa,Bouncycastle,您好,我正在尝试将一段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 p

您好,我正在尝试将一段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#代码

触发

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/OAEPTHHA-256和MGF1padding

此外,对于与此相关的RSA密码,错误输入是否太大?

因此C代码在功能上与Java代码相对应,在C代码
加密字符串中,必须在解密之前对其进行Base64解码(而不是UTF8编码)。解密数据必须经过UTF8解码(而不是Base64编码):

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);
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);
}