This should be an easy question to answer but I’ve wasted more than an hour without success.
I just want to be able to encrypt/decrypt a string. I’ve read the documentation a couple of times but it’s meaningless to me and doesn’t have any examples. I found a blog entry that’s more complex than I can deal with — I think I figured out that the AESDecrypt and AESEncrypt can’t be used because they don’t work with strings. And I’ve gone through a couple dozen entries here that are asking special cases. I’m just trying to write code for two functions so that
x = EncryptString(“This is a test”) gives me encrypted-value
and
x = DescryptString(encrypted-value) gives me “This is a test”
And I could’ve written my own simple routine in the time I’ve spent, since I’m not encrypting anything like financial secrets, just a token from a gaming site. And the AI keeps giving me code that won’t compile, but I’m stubborn. But maybe I’m wrong and this isn’t something that’s built in. But it has to run on Mac & on iOS. Tell me, please; is this something I have to write myself? Or is there an example somewhere of how to encrypt a short string that I just haven’t found yet?
Dim secretkey, publickey As String
Call Crypto.RSAGenerateKeyPair(1024, secretkey, publickey)
Dim message As String = "This is a test"
Dim ciphertxt As MemoryBlock = Crypto.RSAEncrypt(message, publickey)
Dim cleartxt As String = Crypto.RSADecrypt(ciphertxt, secretkey)
You might be confused by the fact that Xojo’s crypto methods are all intended for use with public-key cryptography. This is where different keys are used to encrypt and decrypt, allowing you to publish your “encrypt only” key for others to encrypt messages to you. But it sounds like you just want plain secret key crypto. For that you’ll need a 3rd party plugin or code.
So you’re not sending data to anyone else, you just want to write it to a text file and not have someone casually read it?
I’d just obscure it.
EG
dim s as string = “Hello C J Hinkle”
dim h as string
h = Encodebase64(s)
h = encodebase64(h)
//store h as your encrypted text
//read the text back from a file, and then decode it
//h = read the text
h = decodebase64(h)
h = decodebase64(h)
// h is now back to the original
The correct way to store a user’s secret key (user credentials in general) for a website on macOS would be with the Keychain class. I am unsure if Xojo made it a available on iOS, but the keychain store is there under the name Passwords. You’ll probably need declares if Xojo didn’t bring Keychain to iOS.
Also, Einhugur plugins can encrypt and decrypt strings as easily as you desire in your OP. But again, it’s a generally poor idea to do your own encryption on people’s credentials. I’m on my phone in bed or I’d provide links.
I’m reading the Xojo documentation about Keychain. The notes say:
Keychain is a system-wide facility on MacOS….
An equivalent technology to the Mac OS Keychain doesn’t currently exist on other platforms, so the Keychain class is supported only on MacOS.
But that confuses me because iOS and MacOS share passwords. If I store a password on the Mac, how do I access it on iOS? Or if I retrieve a token on iOS, how do I store it in the Passwords?
If I want my Mac and iOS apps to be able to share a token, am I back to the idea of encrypting it and storing it in the database because I can’t use the iOS passwords?
Just a note, you can pass a string into anything that accepts a memoryblock. And receive a string from anything that returns a memoryblock. You can treat them as interchangeable.
That’s a good point. Xojo’s Keychain documentation also includes Carbon declares – which have been deprecated for nearly 15 years and inaccessible for the past 7 (MacOS Catalina removes all 32-bit app support, and Carbon was 32-bit only). So suffice to say that the Keychain docs were written a long, long, long time ago and some parts may be showing their age. Obviously, Keychain items are now sharable between macOS and iOS and iPadOS and whateverOS.
However, Xojo doesn’t appear to directly support Keychain in iOS projects, so you may be stuck writing the declares yourself or finding a plugin to do it for you.
I have found that because Swift/ObjC is used by so many, is documented well, and has so many resources, that AIs are actually really good at writing declares.
I use the code below to encrypt a string from a textbox. Then you can later decrypt the encrypted string that you saved in the database. You will need to add a constant that holds a key string that is a 16 byte or 32 byte length. Also: senc is just a property to hold the encrypted string. Here is the encrypt code.
Var encrypted As MemoryBlock
// Convert TextField text to UTF8 safely
Var dataToEncrypt As New MemoryBlock(LenB(TextField1.Text))
dataToEncrypt.StringValue(0, LenB(TextField1.Text)) = _
TextField1.Text.DefineEncoding(Encodings.UTF8)
// Encrypt
encrypted = Crypto.AESEncrypt(thekey, dataToEncrypt, Crypto.BlockModes.CBC, thekey)
// Base64 encode directly from MemoryBlock
senc = EncodeBase64(encrypted)
MessageBox(senc)
Here is the decrypt code.
Var encryptedMB As MemoryBlock
Var decryptedMB As MemoryBlock
Var decryptedString As String
// Decode Base64 directly to MemoryBlock
encryptedMB = DecodeBase64(senc)
// Decrypt
decryptedMB = Crypto.AESDecrypt(thekey, encryptedMB, Crypto.BlockModes.CBC, thekey)
// Convert back to string
decryptedString = decryptedMB.StringValue(0, decryptedMB.Size)
decryptedString = decryptedString.DefineEncoding(Encodings.UTF8)
MessageBox(decryptedString)
A casual viewer might try decodebase64 but if what they get back doesn’t show anything recognisable, they would likely try something else rather than decode again.