[Gambas-user] Chiffrement en Gambas
Tobias Boege
taboege at gmail.com
Sat Feb 16 12:02:36 CET 2019
On Sat, 16 Feb 2019, Alarc'h wrote:
> Bonjour,
>
> je désirerais enregistrer des données chiffrées dans une base de
> données. Je sais en bash utiliser par exemple OpenSSL avec l'option enc
> et un algorithme de chiffrement type blowfish pour chiffrer ou
> déchiffrer un fichier au moyen d'une passphrase.
>
> En Gambas je ne sais pas trop par quel bout prendre la chose. Il existe
> bien la commande shell, mais openssl fonctionne en interactif et
> demande de taper la passphrase et ne sait que chiffrer qu'un fichier et
> sortir le résultat soit sur la sortie standard, soit dans un fichier.
>
> Je ne veux pas passer par un fichier. En fait je veux qu'au lancement
> du programme l'utilisateur saisisse sa clé de chiffrement (passphras si
> l'on veut) qu'elle soit stockée dans une variable globale le temps
> pendant lequel l'application tourne et que pour certaines données
> celles-ci soient chiffrées avec la passphrase avant d'être placée en
> base, soit déchiffrées après requête sur la base sans intervention de
> l'utilisateur.
>
> Y a-t-il une solution intégrée en gambas ou une bibliothèque de
> cryptographie (hors l'interface openssl) qui puisse être utilisée ?
>
> Pour info j'ai un poste linux mint sous KDE et gambas 3.12 (donc il me
> semble la version stable la plus récente).
>
> Et juste pour rire... c'est amusant le nom gambas (almost mean basic),
> mais dans mes recherches sur internet je remonte dans mes
> filets davantage de recettes de cuisines que de renseignements sur
> l'informatique... même si cela m'a permis de découvrir quelques bonnes
> recettes de crevettes grillées, c'est un vrai problème pour rechercher
> de la doc.
>
I'm quite happy that I understood this without external help years after
my highschool French classes, but I can't answer in intelligible French.
And this *is* an English-speaking mailing list, so you should try to do
that.
Yes, there is a component wrapping OpenSSL, it's called gb.openssl and
you can find its documentation here: http://gambaswiki.org/wiki/comp/gb.openssl
In general, visit http://gambaswiki.org/wiki/comp/ to see an overview
of all Gambas components.
The gb.openssl component has methods that imitate what the openssl
program does when you encrypt or decrypt, so you should have an easy
time, hopefully.
$ openssl enc -blowfish -k "GAMBAS Almost Means BASic" -S 0123456789abcdef -md md5 -e -base64 <<<"Hello world"
U2FsdGVkX18BI0VniavN7wRigSkQQjTZcdByRLIn6+g=
becomes in Gambas:
Print Base64$(Cipher["blowfish"].EncryptSalted("Hello world\n", "GAMBAS Almost Means BASic", "\x01\x23\x45\x67\x89\xab\xcd\xef"))
U2FsdGVkX18BI0VniavN7wRigSkQQjTZcdByRLIn6+g=
You can use every other cipher besides "blowfish", of course. To get a
list of ciphers installed on your system (and how they are called),
look at the Cipher.List property.
Note a few things:
- You don't normally need to specify a salt (-S in `openssl` and the
optional last parameter to EncryptSalted) because one is generated
for you if you don't. I just included it here to get reproducible
outputs.
- The `-md md5` part makes `openssl` use MD5 to derive a key from the
given passphrase. Newer `openssl`s will use SHA256 for this, but
again I wanted the outputs of the two commands to be same.
An optional parameter should be added to the Gambas interface to
specify the message digest.
- Newer `openssl`s will warn you about using a deprecated key derivation
function if you, like me above, don't use PBKDF2. gb.openssl is
missing an option for this key derivation method. I can try to add
that as well, but I don't want to be yelled at for introducing features
to the component's *interface* which someone's cold war-era OpenSSL
cannot satisfy (thus making the whole component uncompilable).
Regards,
Tobi
--
"There's an old saying: Don't change anything... ever!" -- Mr. Monk
More information about the User
mailing list