zeroclickinfo-goodies/lib/DDG/Goodie/Rc4.pm

66 lines
2.0 KiB
Perl
Raw Normal View History

2015-02-23 10:19:45 -08:00
package DDG::Goodie::Rc4;
# ABSTRACT: This goddie provides a simple encription/decryption service
# using RC4 algorithm and a key provided by the user.
use DDG::Goodie;
use strict;
use warnings;
use Crypt::RC4;
use MIME::Base64;
zci answer_type => "rc4";
zci is_cached => 1;
2015-02-26 09:20:13 -08:00
name "RC4.pm";
2015-02-23 10:19:45 -08:00
description "Encrypt or decrypt a text using a key provided by the user";
2015-03-04 12:27:31 -08:00
primary_example_queries "rc4 en mysecretkey hello", "rc4 de duck yWrJniG/nNg=";
2015-02-23 10:19:45 -08:00
category "computing_tools";
topics "cryptography";
code_url "https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/Rc4.pm";
attribution github => ["https://github.com/puskin94", "puskin94"],
github => ["diegojuan", "JD"],
web => "sysadminjd.com";
# Triggers
triggers startend => "rc4";
# Handle statement
handle remainder => sub {
(my $type, my $key, my $plaintext) = split / /;
2015-02-23 12:15:17 -08:00
my $operation;
my $result;
2015-02-23 10:19:45 -08:00
return unless $type && $key && $plaintext;
if ($type =~ m/^en(c|crypt)?$/) {
2015-02-23 10:19:45 -08:00
# To encrypt we receive the plaintext as is and pass it to the RC4 function.
my $encrypted = RC4($key, $plaintext);
# To avoid problems with non printable characters, we transform the result using encode_base64()
2015-02-23 12:15:17 -08:00
$result = encode_base64($encrypted);
2015-02-23 10:19:45 -08:00
chomp $result;
2015-03-04 12:27:31 -08:00
$operation = "RC4 Encrypt";
2015-02-23 10:19:45 -08:00
} elsif ($type =~ m/^de(c|crypt)?$/) {
2015-02-23 10:19:45 -08:00
#To decrypt we do the reverse process, we take the plaintext, transform it using decode_base64()
my $decoded = decode_base64($plaintext);
# Then we pass it to the RC4 funcion to be decrypted.
2015-02-23 12:15:17 -08:00
$result = RC4($key, $decoded);
2015-02-23 10:19:45 -08:00
# No need to encode again, this result is show as is.
2015-03-04 12:27:31 -08:00
$operation = "RC4 Decrypt";
2015-02-23 23:54:43 -08:00
} else {
return;
2015-02-23 10:19:45 -08:00
}
return "$operation: $plaintext, with key: $key is $result",
structured_answer => {
2015-02-23 12:15:17 -08:00
operation => $operation,
input => [html_enc($plaintext) . ", Key: $key"],
result => $result
2015-02-23 12:15:17 -08:00
};
2015-02-23 10:19:45 -08:00
};
1;