Crypt::Mimetic::TEA - Tiny Encryption Algorithm


Crypt-Mimetic documentation Contained in the Crypt-Mimetic distribution.

Index


Code Index:

NAME

Top

Crypt::Mimetic::TEA - Tiny Encryption Algorithm

DESCRIPTION

Top

This module is a part of Crypt::Mimetic.

This modules uses TEA to encrypt blocks of bytes, so DecryptFile needs @info containing generic-blocks-length and last-block-length (padlen) to know how decrypt a file. EncryptString and DecryptString always encrypt/decrypt a string as a single block.

PROCEDURAL INTERFACE

Top

Return a short description of algorithm

Return true if password is needed by this algorithm, false otherwise. ('TEA' return always true)

Encrypt a file with TEA algorithm. See Crypt::Mimetic::EncryptFile.

Encrypt a string with TEA algorithm. See Crypt::Mimetic::EncryptString.

Decrypt a file with TEA algorithm. See Crypt::Mimetic::DecryptFile.

Decrypt a string with TEA algorithm. See Crypt::Mimetic::DecryptString.

NEEDED MODULES

Top

This module needs: Error::Mimetic Crypt::Tea

KNOWN BUGS

Top

Seems to be quite slow.

SEE ALSO

Top

Crypt::Mimetic

LICENSE

Top

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself (Artistic/GPL2).

AUTHOR

Top

Erich Roncarolo <erich-roncarolo@users.sourceforge.net>


Crypt-Mimetic documentation Contained in the Crypt-Mimetic distribution.
package Crypt::Mimetic::TEA;
use strict;
use Error::Mimetic;
use vars qw($VERSION);
$VERSION = '0.01';

eval 'use Crypt::Tea';
die ("Crypt::Tea required by ". __PACKAGE__) if $@;

sub ShortDescr {
	return "TEA - Tiny Encryption Algorithm.";
}

sub PasswdNeeded {
	return 1;
}

sub EncryptFile {
	my ($filename,$output,$algorithm,$key,@info) = @_;
	my ($buf, $text, $txt) = ("","","");
	my ($len,$blocklen,$padlen) = (0,0,0);
	if ($output) {
		open(OUT,">>$output") or throw Error::Mimetic "Cannot open $output: $!";
	}
	open(IN,"$filename") or throw Error::Mimetic "Cannot open $filename: $!";
	$key = Crypt::Mimetic::GetConfirmedPasswd() or throw Error::Mimetic "Password is needed" unless $key;
	while ( read(IN,$buf,32768) ) {
		$blocklen = $padlen;
		$text = encrypt($buf,$key);
		$padlen = length($text);
		$len += $padlen;
		if ($output) {
			print OUT $text;
		} else {
			$txt .= $text;
		}
	}
	close(IN);
	if ($output) {
		close(OUT);
		return ($len,$blocklen,$padlen);
	}
	return ($len,$blocklen,$padlen,$txt);
}

sub EncryptString {
	my ($string,$algorithm,$key,@info) = @_;
	$key = Crypt::Mimetic::GetConfirmedPasswd() or throw Error::Mimetic "Password is needed" unless $key;
	return &encrypt ($string, $key);
}

sub DecryptFile {
	my ($filename,$output,$offset,$len,$algorithm,$key,@info) = @_;
	my ($blocklen,$padlen) = @info;
	my ($buf, $text, $i, $txt) = ("","",0,"");
	my $blocks = 0;
	$blocks = int($len/$blocklen) if $blocklen;
	if ($output) {
		open(OUT,">$output") or throw Error::Mimetic "Cannot open $output: $!";
	}
	open(IN,"$filename") or throw Error::Mimetic "Cannot open $filename: $!";
	$key = Crypt::Mimetic::GetPasswd() or throw Error::Mimetic "Password is needed" unless $key;
	seek IN, $offset, 0;
	for ($i = 0; $i < $blocks; $i++ ) {
		read(IN,$buf,$blocklen);
		$text = decrypt($buf,$key);
		if ($output) {
			print OUT $text;
		} else {
			$txt .= $text;
		}
	}
	read(IN,$buf,$padlen);
	$text = decrypt($buf,$key);
	if ($output) {
		print OUT $text;
	} else {
		$txt .= $text;
	}
	close(IN);
	if ($output) {
		close(OUT);
	} else {
		return $txt;
	}
}

sub DecryptString {
	my ($string,$algorithm,$key,@info) = @_;
	$key = GetPasswd() or throw Error::Mimetic "Password is needed" unless $key;
	return &decrypt ($string, $key);
}

1;
__END__