perl对称加密解密

来源:转载


概述

常见的对称加密有DES,3DES,AES, RC2, RC4, RC5和Blowfish等,这些在Perl里面都有相应的模块实现。
列举一些如下:
算法模块rpm包cpanDESCrypt::DES   perl-Crypt-DEShttp://search.cpan.org/~dparis/Crypt-DES-2.07/DES.pm3DESCrypt::DES_EDE3perl-Crypt-DES_EDE3http://search.cpan.org/~btrott/Crypt-DES_EDE3-0.01/lib/Crypt/DES_EDE3.pmBlowfishCrypt::Blowfishperl-Crypt-Blowfishhttp://search.cpan.org/~dparis/Crypt-Blowfish-2.14/Blowfish.pmRC4Crypt::RC4perl-Crypt-RC4http://search.cpan.org/~sifukurt/Crypt-RC4-2.02/RC4.pmRC4Crypt::RC4::XSperl-Crypt-RC4-XShttp://search.cpan.org/~oyama/Crypt-RC4-XS-0.02/lib/Crypt/RC4/XS.pmAESCrypt::OpenSSL::AESperl-Crypt-OpenSSL-AEShttp://search.cpan.org/~ttar/Crypt-OpenSSL-AES-0.02/lib/Crypt/OpenSSL/AES.pm

这些基本能满足加密解密需要,使用方法大同小异,以RC4举例。

Perl源码

在centos上测试,首先需要安装支持包perl-Crypt-RC4-XS。

yum install perl-Crypt-RC4-XS

#!/usr/bin/perluse Crypt::RC4::XS;sub rc4_encrypt_hex ($$) { my ($key, $data) = ($_[0], $_[1]); return join('',unpack('H*',RC4($key, $data)));}sub rc4_decrypt_hex ($$) { my ($key, $data) = ($_[0], $_[1]); return RC4($key, pack('H*',$data));}my $passphrase = 'Happy#12';my $plaintest = "Hello,World";my $encrypted = rc4_encrypt_hex($passphrase, $plaintest);my $decrypted = rc4_decrypt_hex($passphrase, $encrypted);print "plain test: ", $plaintest, "/nafter encrypt: ",$encrypted,"/nafter decrypt: ",$decrypted,"/n"

输出结果




分享给朋友:
您可能感兴趣的文章:
随机阅读: