bcgit / pc-dart

Pointy Castle - Dart Derived Bouncy Castle APIs
MIT License
237 stars 122 forks source link

RSA Public Key decryption not able to decrypt large strings #40

Closed penhorwood closed 4 years ago

penhorwood commented 4 years ago

I am trying to encrypt and decrypt a string that is large then the blockSize. I can encrypt it but I can't decrypt the resulting ciphertext. The same basic code works in the java version of Bouncy Castle.

Coding on Android Studio for a Samsung Galaxy S10 via a Windows 10 Pro box.

update to date versions of libraries.

mwcw commented 4 years ago

Hi,

Could you tell me how many bytes the data is and how many bytes the key is please?

MW

penhorwood commented 4 years ago

I was doing a test and I used this as my message. Here is the main line of code with the bitLength and modLength.

String message = "The quick brown fox jumped over the lazy dogs on a hot afternoon in July somewhere in the midwest. Initialize the asymmetrical block cipher for decryption and with the private key. Pointy Castle implements PKCS #1 version 2.0 encryption and decryption. Specifically, it implements the RSAES-OAEP and RSAES";

final rsaParams = new RSAKeyGeneratorParameters( BigInt.from(65537), 2048, 64 );

penhorwood commented 4 years ago

On encryption the message comes out to 305 bytes. On decryption the ciphertext is 490 bytes with a block size of 256.

The code jumps out at line 83 on rsa.dart.

Shouldn't I be able to decrypt any size message as long as I do it in blocks? Maybe not a good idea but it should work. It does work that way on the Java version.

penhorwood commented 4 years ago

I replaced my _processInBlocks code with the one found on this page: https://github.com/bcgit/pc-dart/blob/master/tutorials/rsa.md

and then I commented out lines lines 82 to 89 on rsa.dart. That allow me to decrypt the whole message.

penhorwood commented 4 years ago

I noticed another issue with the output. Here is my plain text string.

String message = "BEGIN-- A-123456789 = B-123456789 = C-123456789 = D-123456789 = E-123456789 = F-123456789 = G-123456789 = H-123456789 = I-123456789 = J-123456789 = K-123456789 = L-123456789 = M-123456789 = N-123456789 = O-123456789 = P-123456789 = Q-123456789 = R-123456789 --END";

When I encrypt then decrypt this message, I am getting this output.

R-123456789 --END9 = B-123456789 = C-123456789 = D-123456789 = E-123456789 = F-123456789 = G-123456789 = H-123456789 = I-123456789 = J-123456789 = K-123456789 = L-123456789 = M-123456789 = N-123456789 = O-123456789 = P-123456789 = Q-123456789 =

The buffer offset is not being handled correctly. Notice the 2nd block is written on top of the first block.

penhorwood commented 4 years ago

I am using PKCS1Encoding. The problem with returning the wrong output is in pkcs1.dart in how the out buffer is filled. I might figure it out but someone with more knowledge will need to look at it.

Lines 173 to the end. There is a result variable never used and the out.setRange uses the wrong values. It always starts at position zero.

out.setRange(outOff, outOff + rlen, block.sublist(start));

should fix it.

penhorwood commented 4 years ago

I discovered that I am was not using the latest version of this package.