javaDeveloper
javaDeveloper

Reputation: 1441

RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING in C# Bouncy Castle - input too large for RSA cipher

HI I am trying to convert a piece of Java Code to C# for decrypting using RSA Key

Java Code

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# Code

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

Trigger

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

When running I am getting an error input too large for RSA cipher

How can I specify the cipher RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING properly in C# Bouncy Castle?

Also is the error input too large for RSA cipher related to this?

Upvotes: 1

Views: 2094

Answers (1)

Topaco
Topaco

Reputation: 49131

So that the C# code functionally corresponds to the Java code, in the C# code encryptedString must be Base64 decoded before decryption (and not UTF8 encoded). The decrypted data must be UTF8 decoded (and not Base64 encoded):

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

Upvotes: 1

Related Questions