Can't catch CryptoExceptions

I have this code before a call to the Crypto module:

try return Crypto.RSADecrypt(encryptedData, kPrivateKey) catch err as CryptoException Log.Write("A CryptoException occurred", location) MsgBox("A CryptoException occured. Please see the log for details") end try

If I deliberately cause an exception (by passing an incorrectly length ciphertext), the error is never caught and my app crashes. Am I misunderstanding the Catch keyword here?

[quote=61864:@Garry Pettet]I have this code before a call to the Crypto module:

try return Crypto.RSADecrypt(encryptedData, kPrivateKey) catch err as CryptoException Log.Write("A CryptoException occurred", location) MsgBox("A CryptoException occured. Please see the log for details") end try

If I deliberately cause an exception (by passing an incorrectly length ciphertext), the error is never caught and my app crashes. Am I misunderstanding the Catch keyword here?[/quote]

Please file a Feedback report on this, with a test project and what platform you’re running it on. It should work as you expect.

Will do.

Done. Case #31964

Just a guess. Not even tested, correct if needed. Check what happens:

Try
    return Crypto.RSADecrypt(encryptedData, kPrivateKey)
  catch err as RuntimeException
    MsgBox "Error "+Str(err.ErrorNumber)+ “, ”+ err.message
End Try

I ran into the same problem and saw this issue already been closed.
I’ve added a complete testproject to #31964 which shows inmediately that exception never being thrown.
See #31964.
Hope this one will be fixed soon because it’s the basics for trusting the stability of your applications.

is that a problem on Mac? Win?

His post on case 31964 says:

“Both Try Catch and UnhandledException is not working, so error will not be thrown by the Crypto function.
Windows 7 2014r2

Okay. On Windows there is a problem with exception catching not working in plugins, if the frame headers are optimized away by compiler. You need to use /OY- option in Visual Studio compilers.
At least that was an issue years ago with my plugins.

[quote=118668:@Joost Rongen]I ran into the same problem and saw this issue already been closed.
I’ve added a complete testproject to #31964 which shows inmediately that exception never being thrown.
See #31964.
Hope this one will be fixed soon because it’s the basics for trusting the stability of your applications.[/quote]
Are you passing a String or a MemoryBlock? If it’s a String, make sure it has no encoding.

Why would that make a difference?
Wouldn’t your plugin simply get a pointer to the bytes in the string as my plugins would do?

It’s a bug, and we know about it. I’m just telling you what the workaround is.

On Win as well as Mac Christian.

Can you have a look Greg ?

[code] Try

// copy value of textbox to a memeryblock
Dim l As Integer  = Len(TextDataInput.text)
Dim msg As New MemoryBlock(l)

// max length of input for crypto.RSAEncrypt seems to be 86 characters.
// so if my input is more than 86 characters and RuntimeException should be raised,
// which never happens with a crash of this app as a result.

msg = Left(TextDataInput.text,88)

Dim encryptedData As MemoryBlock = Crypto.RSAEncrypt( msg, TextPublicKey.text)
TextEncrypted.text = EncodeHex(encryptedData)
LabelLenEncrypted.text = Str(Len(TextEncrypted.text))

Dim encryptedData1 As MemoryBlock
encryptedData1 = DecodeHex(TextEncrypted.text)

Dim decryptedData As MemoryBlock = Crypto.RSADecrypt( encryptedData, TextPrivateKey.text )
TextDecrypted.text = decryptedData
labelLenDecrypted.text = Str(Len(TextDecrypted.text))

Catch err As runtimeexception

// a RuntimeException never being raised 
Break

End Try[/code]

Your problem is right where I said it was. You need to be passing IN a memoryblock as well or using DefineEncoding on msg to set it’s encoding to Nil.