Try/Catch not working with Crypto

I’m using Xojo2017r3 and I’m having an issue with Try/Catch not working in the following function:

EncryptString(message As String) As String Dim publicKey As String = remoteKey Dim msg As MemoryBlock = message Try Dim encryptedData As MemoryBlock = Crypto.RSAEncrypt(msg, publicKey) Return EncodeBase64(encryptedData) Catch Return "" End Try End Sub

If I pass the function a valid string (not empty or Nil) it works and I get the encrypted string returned as expected but if I pass an empty string or a Nil it crashes my program at the “Crypto.RSAEncrypt” line with a Crypto encoding error which is expected but I’ve enclosed the offending line in a Try/Catch block to try to “capture” the error but it still stops debugging rather than ignoring it and moving on.

I have other Try/Catch blocks in my application and they work as expected. It’s only this method that doesn’t seem to want to catch the error. I’ve tried turning “Break on Exceptions” off in the Projects menu to see if that would do it but it still fails. I’ve tried using “Catch ex As CryptoException” but no luck.

Has anyone seen this?

This is the expected behavior. Exceptions inside a Try…Catch block will still cause the IDE to pause execution and show the debugger. Click the resume button and execution will jump to the Catch clause.

To prevent the IDE from pausing execution use #pragma BreakOnExceptions Off.

[quote=381625:@Anthony Mott]I’m using Xojo2017r3 and I’m having an issue with Try/Catch not working in the following function:

EncryptString(message As String) As String Dim publicKey As String = remoteKey Dim msg As MemoryBlock = message Try Dim encryptedData As MemoryBlock = Crypto.RSAEncrypt(msg, publicKey) Return EncodeBase64(encryptedData) Catch Return "" End Try End Sub

If I pass the function a valid string (not empty or Nil) it works and I get the encrypted string returned as expected but if I pass an empty string or a Nil it crashes my program at the “Crypto.RSAEncrypt” line with a Crypto encoding error which is expected but I’ve enclosed the offending line in a Try/Catch block to try to “capture” the error but it still stops debugging rather than ignoring it and moving on.

I have other Try/Catch blocks in my application and they work as expected. It’s only this method that doesn’t seem to want to catch the error. I’ve tried turning “Break on Exceptions” off in the Projects menu to see if that would do it but it still fails. I’ve tried using “Catch ex As CryptoException” but no luck.

Has anyone seen this?[/quote]
If you run in the debugger, see if maybe you’re getting a different exception than CryptoException.

Already a verified issue: <https://xojo.com/issue/31964>

I bet your message is too long. I’ve had this before and had to move to Xojo.Crypto.* instead, because the Crypto.* (old framework) didn’t work for some reason.

Well there’s a limited amount that can be encrypted with RSA based on the key size. The common solution is to use symmetric encryption to encrypt your large content, and asymmetric encryption to encrypt that symmetric encryption’s key.

The issue at hand isn’t why the encryption has failed, it’s that it is impossible catch in certain scenarios.

[quote=381669:@Thom McGrath]Well there’s a limited amount that can be encrypted with RSA based on the key size. The common solution is to use symmetric encryption to encrypt your large content, and asymmetric encryption to encrypt that symmetric encryption’s key.

The issue at hand isn’t why the encryption has failed, it’s that it is impossible catch in certain scenarios.[/quote]

i’m basicly stating the two (old and new) frameworks don’t mix together in my experience.

when using Crypto.* (old framework) it even gives you a hint it’s expecting Xojo.Crypt.* (bottom pane) and it seems to expect it’s using the Xojo.Core.* (new framework) exceptions. To me it seemed it was broken, and the only thing to make it work (with exceptions) for me was to move (the RSA functions) to the new framework.

Oh that’s just because of the messed-up dual framework disaster we find ourselves in. Code hinting doesn’t understand context, so even a Using clause won’t tell it which version you mean. You can use Global.Crypto instead, but autocomplete and code hints choke on that too.

This isn’t working for OutOfBoundsExceptions either. I’ve raised a case for this: <https://xojo.com/issue/52112>

This is a web app with WebPopupMenu

Try
  
  ' Add a line
  cboTest.AddRow "Line 1"
  
  ' This line should throw an OutOfBoundsException
  cboTest.RowTag (101) = "This is tag on non existing line 101"
  
  ' Complete message
  MsgBox "Combo Filled"
  
Catch Err As RunTimeException
  
  ' This isn't displayed!
  MsgBox "An Error Has Occured"
  
End Try

[quote=381669:@Thom McGrath]Well there’s a limited amount that can be encrypted with RSA based on the key size. The common solution is to use symmetric encryption to encrypt your large content, and asymmetric encryption to encrypt that symmetric encryption’s key.

The issue at hand isn’t why the encryption has failed, it’s that it is impossible catch in certain scenarios.[/quote]

Thom is correct, so this is a good place to point out my open-source M_Crypto package that handles AES, Blowfish, Bcrypt, and Scrypt natively.