Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: MySQL AES Encryption with CBC mode

by thanos1983 (Parson)
on May 16, 2017 at 12:29 UTC ( [id://1190373]=note: print w/replies, xml ) Need Help??


in reply to MySQL AES Encryption with CBC mode

Hello JayBee,

I have never used MySQL for this kind of purposes, but from you description I think you are looking for this module (Crypt::Rijndael ).

From the documentation (blocksize):

The blocksize for Rijndael is 16 bytes (128 bits), although the algori +thm actually supports any blocksize that is any multiple of our bytes +. 128 bits, is however, the AES-specified block size, so this is all +we support.

$cipher = Crypt::Rijndael->new( $key [, $mode] )

Create a new Crypt::Rijndael cipher object with the given key (which m +ust be 128, 192 or 256 bits long). The additional $mode argument is t +he encryption mode, either MODE_ECB (electronic codebook mode, the de +fault), MODE_CBC (cipher block chaining, the same that Crypt::CBC doe +s), MODE_CFB (128-bit cipher feedback), MODE_OFB (128-bit output feed +back), or MODE_CTR (counter mode).

Update: Hello again, I wrote a script to replicate the error of your script. I think you are missing the backslash on \ on the @ mysql commands. See example bellow.

#!/usr/bin/perl use DBI; use strict; use warnings; use Data::Dumper; use Config::Simple; $|=1; #flush every time the program my $path = 'conf.ini'; my %config = (); my $checkExist = ""; sub mysql { Config::Simple->import_from("".$path."", \%config) or die Config::Simple->error(); my $dbh = DBI->connect("dbi:mysql::".$config{'MySQL.host'}.":".$co +nfig{'MySQL.port'}."", "".$config{'MySQL.user'}."", "".$config{'MySQL.pass'}."", { 'PrintError' => 1, 'RaiseError' => 1 , 'AutoInactiveD +estroy' => 1 } ) or die "Could not connect to ". $config{'MySQL.host'} .": ". $DB +I::errstr ."\n"; my $databases = $dbh->do("SHOW DATABASES LIKE '".$config{'MySQL.db +'}."'") or die "Error: " .dbh->errstr. "\n"; if ($databases eq 1) { printf "Database: ". $config{'MySQL.db'} ." exists not creating: " +. $config{'MySQL.db'} ."\n"; } else { printf "Database: ". $config{'MySQL.db'} ." does not exist creatin +g: ". $config{'MySQL.db'} ."\n"; $checkExist = $dbh->do("CREATE DATABASE IF NOT EXISTS `".$config{' +MySQL.db'}."`") or die "Could not create the: ".$config{'MySQL.db'}." error: " +. $dbh->errstr ."\n"; } # End of else $dbh->do("USE ".$config{'MySQL.db'}."") or die "Error: " .dbh->errstr. "\n"; my $tables = $dbh->do("SHOW TABLES FROM `".$config{'MySQL.db'}."` WHERE Tables_in_".$config{'MySQL.db'}." LIKE '".$config{'MySQL.table'}."'") or die "Error: ".dbh->errstr. "\n"; if ($tables eq 1) { printf "Table: ".$config{'MySQL.table'}." exists not creating: ".$ +config{'MySQL.table'}."\n"; } else { printf "Table: ".$config{'MySQL.table'}." does not exist creating: + ".$config{'MySQL.table'}."\n"; $checkExist = $dbh->prepare("CREATE TABLE IF NOT EXISTS `".$config +{'MySQL.table'}."` ( `id` int(11) NOT NULL AUTO_INCREMENT, `UnixTime` int(11) NOT NULL, `losses` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREM +ENT=1 ;"); if (!$checkExist->execute()) { die "Error: ". $checkExist->errstr ."\n"; } } # End of else $checkExist = $dbh->prepare("SELECT \@\@session.block_encryption_m +ode = 'aes-128-ecb';"); if (!$checkExist->execute()) { die "Error: ". $checkExist->errstr ."\n"; } my $range = 50; my $minimum = 100; my $random_number = int(rand($range)) + $minimum; my $time = time(); my $losses = $time . ' ' . $random_number; $checkExist = $dbh->prepare("INSERT IGNORE INTO `".$config{'MySQL. +table'}. "` (`UnixTime`, `losses`) VALUES ('".$time."','".$random_number."') "); if (!$checkExist->execute()) { die "Error: ". $checkExist->errstr ."\n"; } my $statement = "SELECT * FROM `".$config{'MySQL.table'}."` WHERE +1"; my $hash_ref = $dbh->selectall_hashref($statement, 'id'); $checkExist->finish(); $dbh->disconnect() or warn "Error disconnecting: $DBI::errstr\n"; return $hash_ref; } # End of mysql sub my $output_ref = mysql(); print Dumper($output_ref); __DATA__ $ perl mysql.pl Database: PerlMonks does not exist creating: PerlMonks Table: Data does not exist creating: Data $VAR1 = { '1' => { 'losses' => 128, 'UnixTime' => 1494975837, 'id' => 1 } };

Update2: Sample of conf.ini for replication purposes:

[MySQL] user=user pass=password host=localhost port=3306 db=PerlMonks table=Data

Hope this helps.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re^2: MySQL AES Encryption with CBC mode
by JayBee (Scribe) on May 16, 2017 at 20:26 UTC
    Yes, Crypt::Rijndael is my next plan. If no further help on this, might as well get started...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1190373]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-25 11:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found