ROT13 Perl example
Download:
https://www.comweb.nl/perl/rot13/rot13.zip
Disclaimer:
The use of this code is at your own risk.
Description:
This subroutines encrypts a string using rot13 (ceasar code).
Rot13 makes a text more difficult to read.
In the old times it was used to obfuscate text on the usenet
It originated from the Roman times Ceasar used it.
See wikipedia for more info.
Purpose:
I just wrote it for the fun.
Performance:
Some testing was done.
Donation:
If you want to support my work, thanks
Download:
https://www.comweb.nl/perl/rot13/rot13.zip
1: #!/usr/bin/perl
2:
3: use strict;
4: use warnings;
5:
6: my %rot13table;
7:
8: print <<END_OF_TEXT;
9:
10: ********************************************************************
11: * *
12: * PERL ROT 13 EXAMPLE *
13: * *
14: * This is just a programming example in Perl for rot13 *
15: * It is today an useless encryption method according to Wikipedia *
16: * It was used in the old times by Roman emperor Ceaser *
17: * Sometimes called Ceasar code. *
18: * See https://en.wikipedia.org/wiki/ROT13 *
19: * *
20: * (C) Comweb Netherlands *
21: * RJHM van den Bergh, sales2\@comweb.nl *
22: * Free to use and distribute *
23: * No warranty, at own risk *
24: * *
25: ********************************************************************
26: END_OF_TEXT
27:
28:
29: #testing it
30: my $message="Ceasar salutes you in 2019!";
31: print "\n";
32: print "Message before rot13: \"$message\"\n";
33: $message=&rot13($message);
34: print "Message after rot13: \"$message\"\n";
35: print "\n";
36: print "Rot13 again will return old message\n";
37: print "\n";
38: print "Message before rot13: \"$message\"\n";
39: $message=&rot13($message);
40: print "Message after rot13: \"$message\"\n";
41: print "\n";
42:
43: sub rot13()
44: {
45: $message=shift;
46: my $new_message=undef;
47:
48: #create hash table for converting?
49: if (keys(%rot13table)==0) # rot13 table contains no key so create it
50: {
51: my @letter_keys=("A".."Z","a".."z");
52: my @letter_values=("N".."Z","A".."M","n".."z","a".."m");
53: for (my $i=0; $i<=$#letter_keys; $i++)
54: {
55: $rot13table{$letter_keys[$i]}=$letter_values[$i];
56: }
57: }
58:
59: # converting the messgae
60: foreach (split("",$message))
61: {
62: if (exists $rot13table{$_})
63: {
64: $new_message.=$rot13table{$_};
65: } else
66: {
67: $new_message.=$_;
68: }
69: }
70:
71: return $new_message;
72: }
Source Code description:
There are many ways that lead to Rome.
There are many ways to write a program.
In the old days everything was simple and easy.
My advice is keep it simple and easy.
That makes it readable and maintainable.
And probably less buggy.
Programming also changed in time.
In the Apple II era we had to stuff everything sometimes in 8kb.
Data was small, computers also slow
Old programs on new computers run in a flash of a second!!
Still it does do its job.
Ok enough shit you probably not came here for this.
Line 1: shebang declares where to find the Perl language.
Line 3: forces programmer to write neat programs.
Line 4: forces programmer to write neat programs.
Line 6: defines the a hash also called associated array.
Example:
$familyName{"RJHM"}="van den Bergh";
$familyName{"Rudolf"}="Radwanski";
If I now want to now the family name of Rudolf
print $familyName{"Rudolf"};
In here used for example letter Z converts to M
print $rot13table{"Z"};
results that M is printed
In here this hash table is still empty.
(Line 48 till 57 fills it.)
Line 8 till 26 Just an easy way to print text
line 29 till 41 does test the rot13 subroutine.
And prints out the results
Line 43 start of the rot13 subroutine.
A subroutine is actually a reusable program part.
You can call it from everywhere in your perl program.
For example
$encrypted_message=rot13("Hello Ceasar");
Line 48 until 57 creates the hashtable if needed.
In here I check if it is empty in line 49.
So I not need to recreate it on every call to the rot13 subroutine.
(Actually not a very great performance boost, perhaps makes it uselessly more complex)
Line 59 to 69 converts the message
Line 71 returns the encrypted/decrypted message to the caller.
Not much explanation but hopes it helps
Download:
https://www.comweb.nl/perl/rot13/rot13.zip