RSA encryption problem

The following code is for encrypting a message. It follows the sample program that Xojo provides regarding RSAEncrypting.

I have previously made a Private and Public key and the code below is used to write and encrypt a message. Eveything works well until the
Dim encryptedData asMemoryBlock = Crypto.RSAEncrypt(msg, Publickey) line [second from bottom] where I get a bug. The bug values are: EncryptedData is Nil, Key is blank (has nothing shown), me is PushButton, Message is Jimbob ( which was txt typed in to encrypt), msg is MemoryBlock and self is SendMessage. SendMessage.

The Publickey is correct in transferring over; I’ve checked that it hadn’t been swapped with the Private key in error. I’ve read the manual but can’t seem to see where my error is. Would anyone be able to see where I’ve gone wrong? Thank you.

Dim privateKey As String
Dim publicKey As String
Dim Message as String
Dim Key as String

// The RSAGenerateKey function has already been done; one has been selected
// to be used for the encryption

PublicKey = PublicKeyArea.text

Message = taMessage.text

Dim msg as New MemoryBlock(170)
msg.StringValue (0,170) = Message

// Encrypt msg using the Public key
Dim encryptedData as MemoryBlock = Crypto.RSAEncrypt(msg, Publickey)

taEncryptMessage.text = encryptedData // this is used to copy the encrypted message to attach to an e-mail.

You don’t strictly need msg, you can pass the Message string directly as there is an implied MemoryBlock to String conversion. You should also EncodeHex or Base64 encrypted data if you want to store it as a string. Besides that, your code should work.

Thank you. I’ll work on it this evenng.