0

大家好,我正在制作一个 perl 脚本来加密和解密文本,我刚开始我有这个:

#!/usr/bin/perl

use Crypt::IDEA;

my $key = pack("H32", "0123456789ABCDEF0123456789ABCDEF");
my $cipher = new IDEA $key;
my $palabra= "plaintex";
my $ciphertext = $cipher->encrypt($palabra);  # NB - 8 bytes

print unpack("H16", $ciphertext), "\n";

my $plaintext = $cipher->decrypt($ciphertext);

print $plaintext , "\n";

问题是要加密的文本必须是 8 个字节的长度。为什么?如果我用“plaintext”代替“plaintex”会给我错误。

input must be 8 bytes long at /usr/lib/perl5/site_perl/Crypt/IDEA.pm line 62.
4

2 回答 2

5

Crypt::CBC包裹Crypt::IDEA - 它将允许使用非对齐的数据长度。请参阅Crypt::CBC 的文档

这是因为 IDEA 和许多其他加密算法都是块加密算法。这意味着它们使用指定大小的数据块进行操作,因此必须准备好您加密的数据(用零或其他填充)

于 2012-11-27T21:24:28.633 回答
1

尝试Crypt::CBCeasy

#!/usr/bin/perl --
use strict; use warnings;
use Crypt::CBCeasy qw/ IDEA /;

my $key     = 'shabba';
my $text    = "plaintex"; ## not a file, not -f -r $text
my $crypted = IDEA::encipher( $key, $text );
my $detext  = IDEA::decipher( $key, $crypted );
print join "\n", $key, $text, unpack( 'H*', $crypted ), $detext, '';

__END__
shabba
plaintex
53616c7465645f5fb5ec01275eb466c4b9b69f3edb7568b42c1713416d33b7aa
plaintex
于 2012-11-28T00:23:40.620 回答