Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

SabreHex

by TStanley (Canon)
on May 08, 2001 at 21:50 UTC ( #78911=sourcecode: print w/ replies, xml ) Need Help??

Category: Cryptography
Author/Contact Info Thomas Stanley
Description: An encryption/decryption program using chromatic's Crypt::CipherSaber module.
I also included an option to output the encrypted material in a hexidecimal format.
#!/usr/bin/perl -w
############################
##   Name: SabreHex       ##
## Author: Thomas Stanley ##
############################
use strict;
use Crypt::CipherSaber;
use Term::ReadKey;
use Getopt::Std;
use vars qw/$opt_d $opt_e $opt_h $opt_o $opt_v/;
 
my $infile;
my $outfile;
 
system('clear');
##########################
getopts('vhd:e:o:');
if($opt_h and $opt_d and $opt_o){  
  $infile = $opt_d;
  $outfile = $opt_o;
  &DECRYPT($infile,$outfile);
}elsif($opt_h and $opt_e and $opt_o){
  $infile = $opt_e;
  $outfile = $opt_o;
  &ENCRYPT($infile,$outfile);
}elsif($opt_d and $opt_o){
  $infile = $opt_d;
  $outfile = $opt_o;
  &DECRYPTNOHEX($infile,$outfile);
}elsif($opt_e and $opt_o){
  $infile = $opt_e;
  $outfile = $opt_o;
  &ENCRYPTNOHEX($infile,$outfile);
}elsif($opt_v){
  die"SabreHex Version 1.06 by Thomas Stanley\n";
}else{
  die"\n\tUsage:  SabreHex [hdeov] [filename]\n\t\t
            -h: Hex Mode\n\t\t
            -d: Decrypt [filename]\n\t\t
            -e: Encrypt [filename]\n\t\t
            -o: Outputfile [filename]\n\t\t
            -v: Version\n";
}
########################### 
sub DECRYPT(){
  my $inputfile = shift;
  my $outputfile = shift;
  print"\n";
 
  print"Please enter the key phrase to decode the file (Input will be 
+hidden): ";
  ReadMode 2;
  my $key = <STDIN>;
  ReadMode 0;
  chomp($key);
  print"\n";
 
  my $SABRE = new Crypt::CipherSaber($key);
 
  open (FH1,"$inputfile")||die "Can't open $inputfile : $!\n";
 
  my $encoded = <FH1>;
 
  $encoded = Dehexed($encoded);
 
  my $decoded = $SABRE->decrypt($encoded);
 
  open (FH2,">$outputfile")||die "Can't open $outputfile : $!\n";
  print FH2 $decoded;
 
  close FH1;
  close FH2;
 
  unlink $inputfile;
}
 
sub ENCRYPT(){
  my $inputfile = shift;
  my $outputfile = shift;
  print"\n";
 
  print"Enter the key phrase to encrypt the file (Input will be hidden
+): ";
  ReadMode 2;
  my $key = <STDIN>;
  ReadMode 0;
  chomp($key);
  print"\n";
 
  my $SABRE = new Crypt::CipherSaber($key);
 
  open(FH1,"$inputfile")||die "Can't open $inputfile: $!\n";
 
  my @text = <FH1>;
  my $text;
  my $l=@text;
  for(my $x=0;$x<$l;$x++){
    $text.=$text[$x];
  }
  my $encrypted = $SABRE->encrypt($text);
 
  $encrypted = Hexed($encrypted);
 
  open(FH2,">$outputfile")||die "Can't open $outputfile: $!\n";
  print FH2 $encrypted;
 
  close FH1;
  close FH2;
 
  unlink $inputfile;
}
 
sub Hexed(){
  my $encrypted = shift;
  my @conversion = split //,$encrypted;
  my $elements = @conversion;
  my $num;
  my $hexed;
 
  for(my $x=0;$x<$elements;$x++){
    $num = ord $conversion[$x];
    $conversion[$x] = sprintf("%02x",$num);
  }
 
  for(my $y=0;$y<$elements;$y++){
    $hexed .= $conversion[$y];
  }
 
  return $hexed;
}

sub Dehexed(){
  my $encrypted = shift;
  my $l = length $encrypted;
  my @conversion=();
  my $dehexed;
  my $c = $l/2;
  my $x = 0;
 
  for(my $y=0;$y<$c;$y++){
    $conversion[$y] = substr($encrypted,$x,2);
    $x+=2;
  }
 
  my $len = @conversion;
 
  for(my $z=0;$z<$len;$z++){
    $dehexed .= chr hex $conversion[$z];
  }
 
  return $dehexed;
}
 
 
sub DECRYPTNOHEX(){
  my $inputfile = shift;
  my $outputfile = shift;
  print"\n";
 
  print"Please enter the key phrase to decode the file (Input will be 
+hidden): ";
  ReadMode 2;
  my $key = <STDIN>;
  ReadMode 0;
  chomp($key);
  print"\n";
 
  my $SABRE = new Crypt::CipherSaber($key);
 
  open (FH1,"$inputfile")||die "Can't open $inputfile : $!\n";
 
  my $encoded = <FH1>;
 
  my $decoded = $SABRE->decrypt($encoded);
 
  open (FH2,">$outputfile")||die "Can't open $outputfile : $!\n";
  print FH2 $decoded;
 
  close FH1;
  close FH2;

  unlink $inputfile;
}
 
sub ENCRYPTNOHEX(){
  my $inputfile = shift;
  my $outputfile = shift;
  print"\n";
 
  print"Enter the key phrase to encrypt the file (Input will be hidden
+): ";
  ReadMode 2;
  my $key = <STDIN>;
  ReadMode 0;
  chomp($key);
  print"\n";
 
  my $SABRE = new Crypt::CipherSaber($key);
 
  open(FH1,"$inputfile")||die "Can't open $inputfile: $!\n";
 
  my @text = <FH1>;
  my $l=@text;
  my $text;
  
  for(my $x=0;$x<$l;$x++){
    $text.=$text[$x];
  }

  my $encrypted = $SABRE->encrypt($text);
 
  open(FH2,">$outputfile")||die "Can't open $outputfile: $!\n";
  print FH2 $encrypted;
 
  close FH1;
  close FH2;
 
  unlink $inputfile;
} 

Comment on SabreHex
Download Code

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://78911]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2014-07-24 09:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (158 votes), past polls