Crypt-XTEA_PP

 view release on metacpan or  search on metacpan

lib/Crypt/XTEA_PP.pm  view on Meta::CPAN

    my $sumation = 0 & 0xffff_ffff;
    my $delta = $DELTA & 0xffff_ffff;

    for my $i ( 0 .. $self->{rounds}-1 ) {
        $block[0] = ( $block[0] + ( ( ( ( ( ( ( ( $block[1] << 4 ) & 0xffff_ffff ) ^ ( ( $block[1] >> 5 ) & 0xffff_ffff ) ) & 0xffff_ffff ) + $block[1] ) & 0xffff_ffff ) ^ ( ( $sumation + $key[ $sumation & 3 ] ) & 0xffff_ffff ) ) & 0xffff_ffff ) ) & ...
        $sumation = ( $sumation + $delta ) & 0xffff_ffff;
        $block[1] = ( $block[1] + ( ( ( ( ( ( ( ( $block[0] << 4 ) & 0xffff_ffff ) ^ ( ( $block[0] >> 5 ) & 0xffff_ffff ) ) & 0xffff_ffff ) + $block[0] ) & 0xffff_ffff ) ^ ( ( $sumation + $key[ ( ( $sumation >> 11 ) & 0xffff_ffff ) & 3 ] ) & 0xffff_f...
    }
    return \@block;
}

sub decrypt_block {
    my $self = shift;
    my $block_ref = shift;
    my $key_ref = $self->{key};

    my @block = map { $_ & 0xffff_ffff } @{ $block_ref };
    my @key = map { $_ & 0xffff_ffff } @{ $key_ref };

    croak( sprintf( 'block must has %d elements', $ELEMENTS_IN_BLOCK ) ) if scalar( @{ $block_ref } ) != $ELEMENTS_IN_BLOCK;
    croak( sprintf( 'key must has %d elements', $ELEMENTS_IN_KEY ) ) if scalar( @{ $key_ref } ) != $ELEMENTS_IN_KEY;

    my $delta = $DELTA & 0xffff_ffff;
    my $sumation = ( $delta * $self->{rounds} ) & 0xffff_ffff;

    for my $i ( 0 .. $self->{rounds}-1 ) {
        $block[1] = ( $block[1] - ( ( ( ( ( ( ( ( $block[0] << 4 ) & 0xffff_ffff ) ^ ( ( $block[0] >> 5 ) & 0xffff_ffff ) ) & 0xffff_ffff ) + $block[0] ) & 0xffff_ffff ) ^ ( ( $sumation + $key[ ( ( $sumation >> 11 ) & 0xffff_ffff ) & 3 ] ) & 0xffff_f...
        $sumation = ( $sumation - $delta ) & 0xffff_ffff;
        $block[0] = ( $block[0] - ( ( ( ( ( ( ( ( $block[1] << 4 ) & 0xffff_ffff ) ^ ( ( $block[1] >> 5 ) & 0xffff_ffff ) ) & 0xffff_ffff ) + $block[1] ) & 0xffff_ffff ) ^ ( ( $sumation + $key[ $sumation & 3 ] ) & 0xffff_ffff ) ) & 0xffff_ffff ) ) & ...
    }
    return \@block;
}

sub key_setup {
    my $key_str = shift;
    croak( sprintf( 'key must be %s bytes long', $KEY_SIZE ) ) if length( $key_str ) != $KEY_SIZE;
    my @xtea_key = unpack 'N*', $key_str;
    return \@xtea_key;
}


1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Crypt::XTEA_PP - Pure Perl Implementation of the eXtended Tiny Encryption Algorithm

=head1 VERSION

version 0.0106

=head1 SYNOPSIS

   use Crypt::XTEA_PP;
   use Crypt::CBC;

   my $xtea = Crypt::XTEA_PP->new( $key );
   my $cbc = Crypt::CBC->new( -cipher => $xtea );

   my $text = 'The quick brown fox jumps over the lazy dog.';
   my $cipher_text = $cbc->encrypt( $text );

   my $plain_text = $cbc->decrypt( $cipher_text );

=head1 DESCRIPTION

In cryptography, XTEA (eXtended TEA) is a block cipher designed to correct weaknesses in TEA.
The cipher's designers were David Wheeler and Roger Needham of the Cambridge Computer Laboratory,
and the algorithm was presented in an unpublished technical report in 1997 (Needham and Wheeler, 1997).
It is not subject to any patents.

Like TEA, XTEA is a 64-bit block Feistel cipher with a 128-bit key and a suggested 64 rounds.
But in Crypt::XTEA_PP, the recommended value for $rounds is 32.

This module implements XTEA encryption. It supports the Crypt::CBC interface, with the following functions.

=head1 METHODS

=head2 keysize

Returns the maximum XTEA key size, 16 bytes.

=head2 blocksize

Returns the XTEA block size, which is 8 bytes. This function exists so that Crypt::XTEA_PP can work with Crypt::CBC.

=head2 new

    my $xtea = Crypt::XTEA_PP->new( $key, $rounds );

This creates a new Crypt::XTEA_PP object with the specified key.
The optional rounds parameter specifies the number of rounds of encryption to perform, and defaults to 32.

=head2 encrypt

    $cipher_text = $xtea->encrypt($plain_text);

Encrypts blocksize() bytes of $plain_text and returns the corresponding ciphertext.

=head2 decrypt

    $plain_text = $xtea->decrypt($cipher_text);

Decrypts blocksize() bytes of $cipher_text and returns the corresponding plaintext.

=head1 SEE ALSO

L<Crypt::CBC>

=head1 AUTHOR

Kars Wang <jahiy@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2015 by Kars Wang.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut



( run in 0.490 second using v1.01-cache-2.11-cpan-e1769b4cff6 )