Solstice::Encryption - Solstice's standard two-way encryption library.


Solstice documentation Contained in the Solstice distribution.

Index


Code Index:

NAME

Top

Solstice::Encryption - Solstice's standard two-way encryption library.

SYNOPSIS

Top

  use Solstice::Encryption;

  my $encrypter = Solstice::Encryption->new;

  my $ciphertext = $encrypter->encrypt($string);

  my $string = $encrypter->decrypt($ciphertext);

DESCRIPTION

Top

Will encrypt/decrypt a string using the Rijndael algorithm (aes).

Export

No symbols exported.

Methods

new()

Constructor. Returns an encrypter object.

encrypt(plain_text_string)

Returns the encrypted version of the plain_text_string in URL safe text.

encryptHex(plain_text_string) Returns the encrypted version of the plain_text_string escaped as hex. =cut
_encrypt() =cut
decrypt(encrypted_string)

Returns the decrypted version of the encrypted_string

decryptHex(encrypted_string) Returns the decrypted version of the encrypted string. =cut
_decrypt() =cut

Private Methods

_setCipher($cipher)
_getCipher()

Modules Used

Crypt::Rijndael, MIME::Base64, URI::Escape.

AUTHOR

Top

Catalyst Group, <catalyst@u.washington.edu>

VERSION

Top

$Revision: 3364 $

COPYRIGHT

Top


Solstice documentation Contained in the Solstice distribution.
package Solstice::Encryption;

# $Id: Encryption.pm 3364 2006-05-05 07:18:21Z mcrawfor $

use 5.006_000;
use strict;
use warnings;

use Crypt::Rijndael;
use MIME::Base64;
use URI::Escape;
use Solstice::Configure;
use Unicode::String;

our $private_key;
use constant DIVISOR => 16;
our ($VERSION) = ('$Revision: 3364 $' =~ /^\$Revision:\s*([\d.]*)/);

sub new {
    my $pkg = shift;
    my $self = bless {}, $pkg;

    if (!defined $private_key) {
        my $config = Solstice::Configure->new();
        $private_key = $config->getEncryptionKey();
    }

    $self->_setCipher(Crypt::Rijndael->new($private_key));

    return $self;
}

sub encrypt {
    my $self = shift;
    my $pt   = shift;
    return uri_escape(encode_base64($self->_encrypt($pt)));
}

sub encryptHex {
    my $self = shift;
    my $pt   = shift;
    return unpack "H*", $self->_encrypt($pt);
}

sub _encrypt {
    my $self = shift;
    my $pt   = shift;  # text to encode

    # require a parameter
    return undef unless (defined $pt && $pt);

    # make sure that our data is a multiple of DIVISOR bytes.
    my $length = length(Unicode::String->new($pt)->as_string());
    if ($length % DIVISOR != 0) {
        $pt = ' ' x (DIVISOR - ($length % DIVISOR)) . $pt;
    }
    return $self->_getCipher()->encrypt($pt);
}

sub decrypt {
    my $self   = shift;
    my $cipher = shift;  # text to decode

    # required parameter
    return undef unless (defined $cipher && $cipher);
    my $base64 = uri_unescape($cipher);
    my $crypted = decode_base64($base64);
    if ((length($crypted) % DIVISOR) != 0) {
        return undef;
    }

    return $self->_decrypt(decode_base64($base64));

}

sub decryptHex {
    my $self = shift;
    my $cipher = shift;
    return undef unless (defined $cipher && $cipher);
    return $self->_decrypt(pack "H*", $cipher);
}

sub _decrypt {
    my $self = shift;
    my $et   = shift;
    my $padded;
    eval{
        $padded = $self->_getCipher()->decrypt($et);
    };

    $padded =~ s/^[ ]*//;
    return $padded;
}

sub _setCipher {
    my $self = shift;
    $self->{'_cipher'} = shift;
}

sub _getCipher {
    my $self   = shift;
    return $self->{'_cipher'};
}


1;
__END__