# A test for encryption (#27: https://forge.ipsl.jussieu.fr/epoll/ticket/27)

use Crypt::RSA; # crypt/decrypt
use XML::Simple; # dump data
use MIME::Base64; # to make it ascii

my $rsa = new Crypt::RSA ES => 'PKCS1v15'; 

    my ($public, $private) = 
        $rsa->keygen ( 
            Identity  => 'Vote ID 12',
            Size      => 768,  
            Password  => undef, 
            Verbosity => 0,
            KF=>'SSH',
        ) or die $rsa->errstr();
print $public->serialize;
print "\n";
print encode_base64($private->serialize);
print "\n";
my $xs = XML::Simple->new(ForceArray => 1, RootName => 'ballot');
print my $xml = $xs->XMLout({ id => '8daad55a7f94a4fb79bda7ab007953dc', sbal => [ 'coco', 'kikikikikikikikikikikikiki' ], fsbal => [] });
print "##########\n";

my $encryptedballot = $rsa->encrypt ( 
            Message    => $xml,
            Key        => $public,
            Armour     => 1,
        ) || die $rsa->errstr();

# reading
print $encryptedballot . "\n";

my $ballot;
foreach (0..1000) {
$ballot = $rsa->decrypt(
        Cyphertext => $encryptedballot,
            Key        => $private,
            Armour     => 1,
        ) || die $rsa->errstr();
}

print $ballot

