Jim, Thank you for your response. I have gone back to the drawing board.
The code is now self-contained and is attached. decrypted = cipherDec.ProcessBlock(adjusted, 0, adjusted.Length-1); Is still throwing an exception even though I adjusted. I have tried it with adjusted.Length and adjusted.Length-1. So, to summarize, I have generated an RSA key pair, encrypted with the private key, and attempted to decrypt with the public key. What am I doing wrong. This is BC 1.8.5 based. The other question, for the future, is that I believe the Pkcs1Encoding Is NOT PCKS1 padding. How should I approach an encrypt/decrypt with PKCS1 padding? From: Jim Schaad <i...@augustcellars.com> Sent: Saturday, September 5, 2020 4:08 PM To: Herbert Falk <herb.f...@otb-consultingservices.com>; dev-crypto-csharp@bouncycastle.org Subject: RE: [dev-crypto-csharp] Problems with the RSA decrypt: input data too large Check to see if one of the items is zero prefixed. Msft requires that all of the parameters be of the correct length but other libraries want to have unsigned numbers, thus the zero prefix. From: Herbert Falk <herb.f...@otb-consultingservices.com<mailto:herb.f...@otb-consultingservices.com>> Sent: Saturday, September 5, 2020 12:30 PM To: dev-crypto-csharp@bouncycastle.org<mailto:dev-crypto-csharp@bouncycastle.org> Subject: [dev-crypto-csharp] Problems with the RSA decrypt: input data too large I have public and private key information from a self-signed certificate. I extract the RSA parameters using Microsoft cypto and then pass it to the code in the following function: byte[] retValue = null; byte[] encrypted = null; IAsymmetricBlockCipher cipher = new Pkcs1Encoding(new RsaEngine()); BigInteger modI = new BigInteger(1, rsaPubParmsMicrosoft.Modulus); BigInteger expI = new BigInteger(rsaPubParmsMicrosoft.Exponent); RsaKeyParameters rsaPublic = new RsaKeyParameters(false, modI, expI); modI = new BigInteger(1, rsaPrivateParmMicrosoft.Modulus); expI = new BigInteger(rsaPrivateParmMicrosoft.Exponent); RsaKeyParameters rsaPrivate = new RsaKeyParameters(true, modI, expI); byte[] data = { 0x01, 0x02, 0x03 }; cipher.Init(true, rsaPrivate); try { encrypted = cipher.ProcessBlock(data, 0, data.Length); } catch(Exception ex) { string emsg = ex.ToString(); } IAsymmetricBlockCipher cipher1 = new Pkcs1Encoding(new RsaEngine()); cipher1.Init(false, rsaPublic); try { int blockLength = cipher1.GetInputBlockSize(); retValue = cipher.ProcessBlock(encrypted, 0, blockLength); } catch(Exception ex) { string emsg = ex.ToString(); } The encryption works, however the decrypt throws the exception {"input data too large (Parameter 'inLen')"}. The blocksize is: 0x00000100 And the size of encrypted is: {byte[0x00000100]} Any ideas about why the exception is being thrown
byte[] retValue = null; byte[] encrypted = null; RsaKeyPairGenerator rsaKeyPairGnr = new RsaKeyPairGenerator(); rsaKeyPairGnr.Init(new Org.BouncyCastle.Crypto.KeyGenerationParameters(new SecureRandom(), 2048)); Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair keyPair = rsaKeyPairGnr.GenerateKeyPair(); byte[] inputBytes = { 0x01, 0x02, 0x03 }; RsaKeyParameters publicKey = (RsaKeyParameters)keyPair.Public; RsaKeyParameters privateKey = (RsaKeyParameters)keyPair.Private; try { //encrypt IAsymmetricBlockCipher cipherEnc = new Pkcs1Encoding(new RsaEngine()); cipherEnc.Init(true, privateKey); //byte[] ciphered = cipher.ProcessBlock(inputBytes, 0, inputMessage.Length); byte[] ciphered = cipherEnc.ProcessBlock(inputBytes, 0, inputBytes.Length); //decrypte IAsymmetricBlockCipher cipherDec = new Pkcs1Encoding(new RsaEngine()); cipherDec.Init(false, publicKey); //byte[] ciphered = cipher.ProcessBlock(inputBytes, 0, inputMessage.Length); byte[] decrypted = null; if (ciphered[0] != 0x0) { byte[] adjusted = new byte[ciphered.Length + 1]; adjusted[0] = 0x0; Buffer.BlockCopy(ciphered, 0, adjusted, 1, ciphered.Length); decrypted = cipherDec.ProcessBlock(adjusted, 0, adjusted.Length-1); } else { decrypted = cipherDec.ProcessBlock(inputBytes, 0, inputBytes.Length); } } catch(Exception ex) { string emsg = ex.ToString(); }