sub _symmetric { #Applies symmetric cipher caller eq __PACKAGE__ or die "_symmetric is a private method\n"; die "instance method only" unless ref (my $self = shift); my $ifname = shift; my ($tfname, $ofname, $fh_plain, $fh_cipher); my $cipher = Crypt::CBC->new($self->{SYMMKEY}, 'Twofish') # Using AES-level cipher or die "Unable to create cipher: $@\n"; if ($self->{MODE} eq TRANSMIT) { $tfname = $ifname . ZIP; $ofname = $ifname . ENC; # A - compress $self->_compressor($ifname, $tfname); die "Unable to delete ${ifname}: $!\n" unless (unlink $ifname); # B - encrypt $fh_plain = IO::File->new($tfname, O_RDONLY) or die "Unable to open ${tfname}: $!\n"; $fh_plain->binmode; $fh_cipher = IO::File->new($ofname, O_WRONLY | O_CREAT) or die "Unable to create ${ofname}: $!\n"; $fh_cipher->binmode; $cipher->start('encrypting'); $fh_cipher->write($cipher->crypt($fh_plain->getline)) until ($fh_plain->eof); $fh_cipher->write($cipher->finish); $fh_cipher->flush; $fh_plain->close; $fh_cipher->close; die "Unable to delete ${tfname}: $!\n" unless (unlink $tfname); } else { my @name_parts = split(/\./, $ifname); $ofname = shift(@name_parts); $tfname = $ofname . ZIP; # A - decrypt if ( (stat($ifname))[7] ) { $fh_cipher = IO::File->new($ifname, O_RDONLY) or die "Unable to open ${ifname}: $!\n"; $fh_cipher->binmode; $fh_plain = IO::File->new($tfname, O_WRONLY | O_CREAT) or die "Unable to create ${tfname}: $!\n"; $fh_plain->binmode; # # It fails on the following line # $cipher->start('decrypting'); $fh_plain->write($cipher->crypt($fh_cipher->getline)) until ($fh_cipher->eof); $fh_plain->write($cipher->finish); $fh_plain->flush; $fh_cipher->close; $fh_plain->close; # B - restore if ( (stat($tfname))[7] ) { $self->_compressor($tfname, $ofname); } die "Unable to delete ${tfname}: $!\n" unless (unlink $tfname); } die "Unable to delete ${ifname}: $!\n" unless (unlink $ifname); } return $ofname; }