LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-07-2008, 10:08 PM   #1
mimithebrain
Member
 
Registered: Nov 2003
Location: ~
Distribution: Ubuntu 10.04
Posts: 843
Blog Entries: 1

Rep: Reputation: 30
Simple Encryption and Perl


I've been having lots of fun with Perl recently.

One thing that I did was to create an encryption program that encrypts the STDIN with a key using a simple XOR algorithm.

Here's my code:

Code:
#!/usr/bin/perl

die "error, provide password" if ( $#ARGV != 0 );
$masterkey = $ARGV[0];
$mklen = length($masterkey);
$chri = 0; #character index
$key = "";

while (<STDIN>){
$line = $_;
$llen = length($line);
$i = 0;
$key = "";

	while ($i < $llen)
	{
	$key = $key . substr( $masterkey, $chri, 1);
	$chri++;
	if ($chri == $mklen) {$chri = 0};
	$i++;
	}
#print $key;
print ($line ^ $key);
}
My question: What's a more efficient way of doing this?
I'd like to improve...

Thanks
 
Old 01-08-2008, 02:52 AM   #2
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
http://search.cpan.org/~kasei/Crypt-...0.06/Simple.pm

there are a lot of encryption methods and modules available at cpan.
cheers, j
 
Old 01-08-2008, 11:54 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
how's this?

(use "<>" which accepts stdin or command line args)

Code:
#!/usr/bin/perl -w

$key = shift or die "error, provide password\n";

local $/ = undef; # slurp mode
$file = <>; # slurp whole file

$key .=  $key x int(length($file)/length($key));
$key = substr ($key, 0, length($file));  # remove excess

print ($file ^ $key);
make test data file:
Code:
$ cat /usr/dict/words  /usr/dict/words  /usr/dict/words  /usr/dict/words > input
$ wc -l input
  100572 input
your version:

Code:
$ time cat input | ./2.pl sponge  > 2

real    0m1.15s
user    0m1.11s
sys     0m0.02s
my version:

Code:
$ time ./1.pl sponge input > 1                 

real    0m0.08s
user    0m0.01s
sys     0m0.04s

check it's identical:
Code:
$ diff 1 2
$ cksum 1 2
1515968497      826652  1
1515968497      826652  2
 
Old 01-08-2008, 08:44 PM   #4
mimithebrain
Member
 
Registered: Nov 2003
Location: ~
Distribution: Ubuntu 10.04
Posts: 843

Original Poster
Blog Entries: 1

Rep: Reputation: 30
bigearsbilly,

You are a genius, thank you
 
Old 01-09-2008, 03:57 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
I'm not a genius, just a sad geek who loves perl problems!

glad you like it, it's a more perl-ish solution, i.e. less typing!
 
Old 01-09-2008, 04:23 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Quote:
Originally Posted by bigearsbilly View Post
I'm not a genius, just a sad geek who loves perl problems!
In this country that would be "a sick bastard" ...

(meant of course as a friendly jibe - the best way)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
simple regex not so simple (perl) ludeKing Programming 5 03-02-2005 02:29 AM
Simple Perl question! Please help! jacksmash Programming 4 11-07-2003 05:56 AM
Simple Perl Forum XxAndyxX Programming 2 03-31-2003 09:27 PM
Simple Perl Question Danny.au Programming 6 07-18-2002 09:46 AM
simple perl tunedLow Programming 4 06-04-2002 03:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration