JayBee has asked for the wisdom of the Perl Monks concerning the following question:
Trying to connect to DB to convert passwords to encrypt with AES in CBC mode (vs ECB) I've learned that I have to SET the SESSION before executing any SQL commands. So these two work when I attempt to do it with phpMyAdmin:
SET @@session.block_encryption_mode = 'aes-128-cbc'; SELECT ID, HEX(AES_ENCRYPT(Password,'$key',RANDOM_BYTES(16))) FROM Use +r WHERE 1; ## OR SET SESSION block_encryption_mode = 'aes-128-cbc'; SELECT ID, HEX(AES_ENCRYPT(Password,'$key',RANDOM_BYTES(16))) FROM Use +r WHERE 1;
but none will work when I attempt it though my script. Here's the basics:
use strict; use CGI ':standard'; use DBI; use DBD::mysql; our ($sth,$dbh,%Set,@Out); &DBCredentials; # assigns %Set; print header,start_html('test'); DBRun("SET SESSION block_encryption_mode='aes-128-cbc'; SELECT ID, HEX(AES_ENCRYPT(Password,'$Set{AESKey}',RANDOM_BYTES(16))) FROM User WHERE 1"); while (my @ar=$sth->fetchrow_array){ my $len=length($ar[1]); push @Out,"$ar[0]: L=$len -- $ar[1]<br />\n"; } &DBEnd; print shift(@Out)."\n" while @Out; sub DBConnect { my $er; my $dsn='DBI:mysql:database='.$Set{DBName}; $dbh=DBI->connect($dsn, $Set{DBUser}, $Set{DBPass}) || ($er=1); if ($er){ myErr('DB Start Error'); } } ##DBConnect## sub DBRun { my $er; &DBConnect; $sth=$dbh->prepare($_[0]) || ($er=1); $sth->execute || ($er=1) if !$er; if ($er){ myErr('DB Execute Error', $_[0]); } } ##DBRun## sub DBDo { my $er; &DBConnect; $dbh->do($_[0]) || ($er=1); if ($DBI::err || $er){ myErr('DB Do Error', $_[0], $DBI::errstr); } $dbh->disconnect(); } ##DBDo## sub DBEnd {$sth->finish; $dbh->disconnect; } ##DBEnd#
Not sure what this is exactly, but I've also tried adding Callbacks to the DBConnect portion, but that didn't work either:
sub DBConnect { my $er; my $DBCall={ 'connect_cached.connected' => sub { shift->do("SET SESSION block_encryption_mode='aes-128-cbc'") +; } }; my $dsn='DBI:mysql:database='.$Set{DBName}; $dbh=DBI->connect($dsn, $Set{DBUser}, $Set{DBPass}, { Callbacks => $DB +Call }) || ($er=1); if ($er){ myErr('DB Start Error'); } } ##DBConnect##
It all leads to an error being generated. My next solution would be to encrypt & decrypt in perl module (prior to sending to mySQL), not sure that's such a great idea just yet.
Thanks in advance for your guidance.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: MySQL AES Encryption with CBC mode
by haukex (Archbishop) on May 16, 2017 at 08:35 UTC | |
by JayBee (Scribe) on May 16, 2017 at 20:39 UTC | |
by haukex (Archbishop) on May 17, 2017 at 06:32 UTC | |
|
Re: MySQL AES Encryption with CBC mode
by thanos1983 (Parson) on May 16, 2017 at 12:29 UTC | |
by JayBee (Scribe) on May 16, 2017 at 20:26 UTC |