Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
#!/usr/bin/perl -w use strict; # Vigenere cipher toolkit. Encrypt and Decrypt. # Cracking not included. # my $plaintext = "This is not secure, but it's kind of fun."; my $key = 'perlmonks'; # By definition, letters only +. my $ciphertext; my $derived_plaintext; # For convenience, do the work in lowercase... $plaintext = lc($plaintext); $key = lc($key); # By definition, only encipher letters, and only use letters in the ke +y... $plaintext =~ s/[^a-z]//g; $key =~ s/[^a-z]//g; if(length($key) < 1){ die "Key must contain at least one letter.\n"; } print "plaintext is [$plaintext]\n\n"; $ciphertext = encrypt_string( $plaintext, $key); $derived_plaintext = decrypt_string( $ciphertext, $key ); print "Plaintext used :$plaintext\n"; print "Ciphertext :$ciphertext\n"; print "Recreated plaintext :$derived_plaintext\n"; sub encrypt_string{ # encrypts a full string of plaintext, given the plaintext and the + key. # returns the encrypted string. my ($plaintext, $key) = @_; my $ciphertext; $key = $key x (length($plaintext) / length($key) + 1); for( my $i=0; $i<length($plaintext); $i++ ){ $ciphertext .= encrypt_letter( (substr($plaintext,$i,1)), (substr($key,$ +i,1))); } return $ciphertext; } sub decrypt_string{ # decrypts a full string of ciphertext, given the ciphertext and t +he key. # returns the plaintext string. my ($ciphertext, $key) = @_; my $plaintext; $key = $key x (length($ciphertext) / length($key) + 1); for( my $i=0; $i<length($ciphertext); $i++ ){ $plaintext .= decrypt_letter( (substr($ciphertext,$i,1)), (substr($key, +$i,1))); } return $plaintext; } sub encrypt_letter{ # encrypts a single letter of plaintext, given the plaintext # letter and the key to use for that letter's position. # The key is the first letter of the row to look in. my ($plain, $row) = @_; my $cipher; # in row n, ciphertext is plaintext + n, mod 26. $row = ord(lc($row)) - ord('a'); # enable mod 26 $plain = ord(lc($plain)) - ord('a'); # enable mod 26 $cipher = ($plain + $row) % 26; $cipher = chr($cipher + ord('a')); return uc($cipher); #ciphertext in uppercase } sub decrypt_letter{ # decrypts a single letter of ciphertext, given the ciphertext # letter and the key to use for that letter's position. # The key is the first letter of the row to look in. my ($cipher, $row) = @_; my $plain; # in row n, plaintext is ciphertext - n, mod 26. $row = ord(lc($row)) - ord('a'); # enable mod 26 $cipher = ord(lc($cipher)) - ord('a'); # enable mod 26 $plain = ($cipher - $row) % 26; $plain = chr($plain + ord('a')); return lc($plain); #plaintext in lowercase }

In reply to Vigenere Cipher: Encode and Decode by mrbbking

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-24 08:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found