Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SCRIPT
- #!/usr/bin/perl -w
- use strict;
- use warnings;
- use feature 'say';
- use Switch;
- use Crypt::CaesarCipher;
- my $shiftValue='3';
- my $plainText;
- switch (defined($ARGV[0])) {
- case '' {
- $plainText='workshop at unina mmxiv'
- }
- else {
- $plainText=$ARGV[0]
- }
- }
- say '--> Testo in chiaro: '.$plainText;
- # istanza oggetto cifrario di Cesare
- my $caesarCipher=Crypt::CaesarCipher->new({setShift => $shiftValue});
- # invocazione metodo di cifratura
- my $encrypted=$caesarCipher->encrypt($plainText);
- say '--> Messaggio criptato '.$encrypted;
- # decifrazione del messaggio criptato
- my $decrypted=$caesarCipher->decrypt($encrypted);
- say '--> Messaggio decriptato '.$decrypted;
- CLASSE Crypt::CaesarCipher
- # package Caesar Cipher by [email protected] - CC-BY-SA
- package Crypt::CaesarCipher;
- use strict;
- use warnings;
- use base 'Class::Accessor';
- Crypt::CaesarCipher->mk_accessors('encrypt','decrypt','setShift');
- our $VERSION='0.01';
- # ascii a = 97 e ascii z= 122
- sub encrypt {
- my ($self,$plainText)=@_;
- my $encryptedText;
- foreach my $plainChar(split //,$plainText) {
- my $ordCharShifted=ord($plainChar)+$self->setShift;
- if ($ordCharShifted > 122) {
- $encryptedText.=chr($ordCharShifted - 122 + 96)
- } else {
- $encryptedText.=chr($ordCharShifted)
- }
- }
- return $encryptedText
- }
- sub decrypt {
- my ($self,$encryptedText)=@_;
- my $plainText;
- foreach my $encryptedChar(split //,$encryptedText) {
- my $ordCharShifted=ord($encryptedChar)-$self->setShift;
- if ($ordCharShifted < 97) {
- $plainText.=chr(122 - $ordCharShifted + 96)
- } else {
- $plainText.=chr($ordCharShifted)
- }
- }
- return $plainText
- }
- 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement