#!/usr/bin/perl

# This program demonstrates basic use of the Crypt::RSA Perl module.
# For module documentation, run
#
#    perldoc Crypt::RSA
#
# from a shell prompt on phoenix.

use strict;

use Crypt::RSA;

# Digest::MD5 isn't necessary in this example, but it might be useful
# for the assignment.  Documentation is available through perldoc.

use Digest::MD5;

# Create a Crypt::RSA object for use in RSA calls.  Generate a new private
# key, public key pair and store it on disk.

my $rsa = new Crypt::RSA;
print "Hold on, we're generating the key pair...\n";
my ($public, $private) = $rsa->keygen(
                            Size => 2048,
                            Filename => "key"
                         );

# We could have simply used the key pair generated earlier, but this
# demonstrates how to read them from disk.

my $privateKey = new Crypt::RSA::Key::Private(
                    Filename => "key.private"
                 );
my $publicKey = new Crypt::RSA::Key::Public(
                    Filename => "key.public"
                );

# The message to be encrypted, followed by the encryption call.

my $message = "The quick brown fox jumps over the lazy dog.";
my $cyphertext = $rsa->encrypt(
                    Message => $message,
                    Key => $publicKey,
                    Armour => 1
                 );

print "Cyphertext:\n$cyphertext\n";

# Decrypt the message.

my $message = $rsa->decrypt(
                 Cyphertext => $cyphertext,
                 Key => $privateKey,
                 Armour => 1
              );

print "Message:\n$message\n";

exit(0);
