[Gambas-user] open-ssl

Tobias Boege taboege at gmail.com
Thu Sep 27 17:16:30 CEST 2018


On Thu, 27 Sep 2018, Antonio Teixeira wrote:
> Hi everyone,
> 
> Does anyone worked with open-ssl in gambas? Any chance to post an example
> about how to encrypt and decrypt a string?
> I looked everywhere and i didnt find any info about that.
> 

En- and decryption is done using the Cipher class. First, you have to settle
on a cipher, say AES256 in CBC mode. You can get a list of ciphers that your
local openssl supports from Cipher.List. You'll see that "AES-256-CBC" is a
supported method (I suppose virtually everywhere). You use this string as an
index into the Cipher class to get a .Cipher.Method object which can do en-
and decryption, see [1]. For example:

  Public Sub Main()
    Dim sCipher, sData As String

    sCipher = Cipher["AES-256-CBC"].EncryptSalted("Hello there", "secret")
    Print "Cipher text (base64):";; Base64$(sCipher)
    sData = Cipher["AES-256-CBC"].DecryptSalted(sCipher, "secret")
    Print "Decrypted:";; sData

    Try Cipher["AES-256-CBC"].DecryptSalted(sCipher, "wrong")
    If Error Then Print "ERROR:";; Error.Text
  End

produces the following output:

  Cipher text (base64): U2FsdGVkX1+j36HLTJVWjG2ciDw2ZOk/dhbdB7aiTOg=
  Decrypted: Hello there
  ERROR: Decryption failed

Regards,
Tobi

[1] http://gambaswiki.org/wiki/comp/gb.openssl/.cipher.method

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk


More information about the User mailing list