Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 如何在Perl中为Caesar密码的数字分配字母?_String_Perl_Encryption - Fatal编程技术网

String 如何在Perl中为Caesar密码的数字分配字母?

String 如何在Perl中为Caesar密码的数字分配字母?,string,perl,encryption,String,Perl,Encryption,我写了一个程序,对字母表中的字母对应的数字进行加密和解密,但我该如何做才能在我请求输入时将每个字母分配给它的数字,然后执行操作并打印加密和解密消息,而不需要几行代码?这是我的节目: print "Caesar's Cipher\n\n"; print "Reference:\n\n"; print "A B C D E F G H I J K L M\n"; print "0 1 2 3 4 5 6 7 8 9

我写了一个程序,对字母表中的字母对应的数字进行加密和解密,但我该如何做才能在我请求输入时将每个字母分配给它的数字,然后执行操作并打印加密和解密消息,而不需要几行代码?这是我的节目:

print "Caesar's Cipher\n\n";
print "Reference:\n\n";
print "A   B   C   D   E   F   G   H   I   J   K   L    M\n";
print "0   1   2   3   4   5   6   7   8   9   10  11   12\n\n";
print "N   O   P   Q   R   S   T   U   V   W   X   Y    Z\n";
print "13  14  15  16  17  18  19  20  21  22  23  24   25\n";
print "\nEnter a Message (User numbers separated by space):\n";
$string = <>; 

@sarray = split(" ",$string);

foreach $x (@sarray){
    if ($x >=0 && $x <= 25){
        $x = ($x+3)%26;
    } else {
        print "Entered incorrect message.\n"; 
        die;
    }
}
print "\nEncryption: \n";
print "@sarray\n";

foreach $x (@sarray){
    if ($x >=0 && $x <= 25){
        $x = ($x-3)%26;
    } else {
        print "Entered incorrect message.\n"; 
        die;
    }
}

print "Decryption: \n";
print "@sarray\n";
打印“凯撒密码\n\n”;
打印“参考:\n\n”;
打印“A B C D E F G H I J K L M\n”;
打印“01 2 3 4 5 6 7 8 9 10 11 12\n\n”;
打印“无P Q R S T U V W X Y Z\N”;
打印“13 14 15 16 17 19 20 21 22 23 24 25”;
打印“\n输入消息(用户编号以空格分隔):\n”;
$string=;
@sarray=split(“,$string);
外汇$x(@sarray){

如果(xx>=0 & x=0 & & x xp*),你必须考虑上、下、数字、加上空格字符和标点符号。现在你只处理大写字母alpha。你需要一个散列,将字符映射到数字,另一个映射到另一个。
$inputChar = character to be encoded
$charset = " ABCDEFGHI...Zabcdef...z0123456789!@#$%^&*...";
$code = index($charset,$char);
# encode here as in your example using length($charset) instead of 26
$outputChar = substr($charset,$code,1);

将此逻辑应用于邮件中的所有字符以生成加密邮件。

以上Jim提供的解决方案超出了要求,因为OP只需要从A到Z的字母字符。实现此功能的更简单方法是,以J为例进行搜索:

my @alpha = ('A'..'Z');
my $s = 'J';
my( $index ) = grep{ $alpha[ $_ ] eq $s } 0..$#alpha;