Hi Lou, Thomas, Norbert
Thanks Thomas (I just saw Norbert's response too:) for chiming in.
"I would like a method call that I could give a password and a string and get back the encrypted/unencrypted result"
- You certainly can do this, but you have to know what algorithm you want.
- I'm assuming we are talking about symmetric encryption.
- If this is the case, you use the factory methods on OSSslCipher to select the algorithm you want.
- The easiest is an unauthenticated mode. `OSSslCipher aes_256_cbc`.
- If you want to add authentication in addition to encryption (which is certainly preferred today), you need an authenticated mode such as `OSSslCipher aes_256_gcm`
I'll add a short symmetric cipher example here, but we have extensive examples in OpenSSLCryptoInterfaceExamplesApp for doing hashing, authentication, various types of encryptions, key agreement, key derivations...
Here is the simple unauthenticated example using just a password and string.
| algo plainText key iv cipherText plainText2 |
algo := OSSslCipher aes_256_cbc.
plainText := 'Hello Smalltalk' asByteArray.
key := algo randomKey. "(you can preselect a key also, but generally that is not a favored approach)"
iv := algo randomIV.
cipherText := algo encrypt: plainText key: key iv: iv.
plainText2 := algo decrypt: cipherText key: key iv: iv.
self assert: [plainText = plainText2]
For more examples of this type of encryption, including adding authentication, see OpenSSLSymmetricCipherExamples.