[Gambas-user] Crypting single files

Tobias Boege taboege at gmail.com
Fri May 25 13:05:53 CEST 2018


On Fri, 25 May 2018, Rolf-Werner Eilert wrote:
> There is a gb.crypt component, but it is meant to encrypt passwords only.
> 
> Is there a component that has some easy-to-handle functions to encrypt
> files?
> 
> Or if not, how do you handle this?
> 

gb.openssl does this. Example:

  Public Sub Main()

    Dim sTemp As String = Temp$()

    File.Save(sTemp, CStr(Now))
    Print "Original data:";; File.Load(sTemp)

    File.Save(sTemp, Cipher["BF-CBC"].EncryptSalted(File.Load(sTemp), "passphrase", "salt"))

    Print "Encrypted data:";; Base64$(File.Load(sTemp))
    Print "Decrypted data:";; Cipher["BF-CBC"].DecryptSalted(File.Load(sTemp), "passphrase")

    Print "Wrongly decrypted data:";; Cipher["BF-CBC"].DecryptSalted(File.Load(sTemp), "Passphrase")

  End

  Original data: 05/25/2018 10:58:45.768
  Encrypted data: U2FsdGVkX19zYWx0AAAAAEQ8/7vYGZRMqEpmhf7SssiQJkBlcT4tmw==
  Decrypted data: 05/25/2018 10:58:45.768
  ' Last line raises an error because the password is wrong

Note that the encrypted data is base64 encoded because it is binary
gibberish that you don't want to print to a terminal.

Indexing the Cipher class with a cipher method (here "BF-CBC") gives you
an object, which you can use to encrypt and decrypt arbitrary strings.
The Cipher.List property gives you a list of all ciphers available from
the openssl crypto library on your system.

Regards,
Tobi

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


More information about the User mailing list