Net::SSH::Perl::Cipher::CFB - CFB Implementation


Net-SSH-Perl documentation Contained in the Net-SSH-Perl distribution.

Index


Code Index:

NAME

Top

Net::SSH::Perl::Cipher::CFB - CFB Implementation

SYNOPSIS

Top

    use Net::SSH::Cipher::CFB;
    my $cbc = Net::SSH::Cipher::CFB->new($cipher_obj);
    print $cbc->encrypt($plaintext);

DESCRIPTION

Top

Net::SSH::Perl::Cipher::CFB provides a CFB (cipher feedback) implementation for SSH encryption ciphers.

AUTHOR & COPYRIGHTS

Top

This code is based in part on the Crypt::CFB code originally developed by Systemics Ltd.

Please see the Net::SSH::Perl manpage for author, copyright, and license information.


Net-SSH-Perl documentation Contained in the Net-SSH-Perl distribution.

# $Id: CFB.pm,v 1.5 2001/04/03 19:44:47 btrott Exp $

# This code based in part on the Systemics Crypt::CFB.
# Parts Copyright (C) 1995, 1996 Systemics Ltd (http://www.systemics.com/)
# All rights reserved.

package Net::SSH::Perl::Cipher::CFB;
use strict;

sub new {
    my($class, $ciph, $iv) = @_;
    bless {
        cipher    => $ciph,
        iv        => $iv || ("\0" x $ciph->blocksize),
    }, $class;
}

sub encrypt {
    my $cfb = shift;
    my $data = shift;

    my $retval = "";
    my $iv = $cfb->{iv};
    my $size = $cfb->{cipher}->blocksize;

    while (length $data) {
        my $out = $cfb->{cipher}->encrypt($iv);
        $iv = substr($data, 0, $size, '') ^ substr($out, 0, $size, '');
        $retval .= $iv;
    }

    $cfb->{iv} = $iv;
    $retval;
}

sub decrypt {
    my $cfb = shift;
    my $data = shift;

    my $retval = "";
    my $iv = $cfb->{iv};
    my $size = $cfb->{cipher}->blocksize;

    while (length $data) {
        my $out = $cfb->{cipher}->encrypt($iv);
        $iv = substr($data, 0, $size, '');
        $retval .= $iv ^ substr($out, 0, $size);
    }

    $cfb->{iv} = $iv;
    $retval;
}

1;
__END__