在使用密钥为每个字母创建加密字符集时,该字母z
似乎没有添加到变量中。代码有什么问题吗?请指出。
我尝试z
在 for 循环中添加额外的字符并增加计数器,但z
没有连接到字符串。
BEGIN {
charset="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
printf("Enter your choice:\n1. Encryption.\n2. Decryption.\n")
getline choice < "-"
print "Enter Key:"
getline key < "-"
for(i=1;i<=62;i++) {
newch = substr(charset,(i+key)%62,1)
encharset = encharset newch
print encharset
}
}
输出
Enter your choice:
1. Encryption.
2. Decryption.
1
Enter Key:
19
J
JK
JKL
JKLM
JKLMN
JKLMNO
JKLMNOP
JKLMNOPQ
JKLMNOPQR
JKLMNOPQRS
JKLMNOPQRST
JKLMNOPQRSTU
JKLMNOPQRSTUV
JKLMNOPQRSTUVW
JKLMNOPQRSTUVWX
JKLMNOPQRSTUVWXY
JKLMNOPQRSTUVWXYZ
JKLMNOPQRSTUVWXYZa
JKLMNOPQRSTUVWXYZab
JKLMNOPQRSTUVWXYZabc
JKLMNOPQRSTUVWXYZabcd
JKLMNOPQRSTUVWXYZabcde
JKLMNOPQRSTUVWXYZabcdef
JKLMNOPQRSTUVWXYZabcdefg
JKLMNOPQRSTUVWXYZabcdefgh
JKLMNOPQRSTUVWXYZabcdefghi
JKLMNOPQRSTUVWXYZabcdefghij
JKLMNOPQRSTUVWXYZabcdefghijk
JKLMNOPQRSTUVWXYZabcdefghijkl
JKLMNOPQRSTUVWXYZabcdefghijklm
JKLMNOPQRSTUVWXYZabcdefghijklmn
JKLMNOPQRSTUVWXYZabcdefghijklmno
JKLMNOPQRSTUVWXYZabcdefghijklmnop
JKLMNOPQRSTUVWXYZabcdefghijklmnopq
JKLMNOPQRSTUVWXYZabcdefghijklmnopqr
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrs
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrst
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstu
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwx
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy #from this line "z" is not appearing in the string
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy01
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy012
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0123
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy01234
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy012345
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0123456
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy01234567
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy012345678
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0123456789
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0123456789A
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0123456789AB
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0123456789ABC
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0123456789ABCD
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0123456789ABCDE
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0123456789ABCDEF
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0123456789ABCDEFG
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0123456789ABCDEFGH
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0123456789ABCDEFGHI
JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0123456789ABCDEFGHI
答案1
您没有正确使用模运算符。
echo 0 62 | awk '{ print $1 % $2 }' # "0"
echo 1 62 | awk '{ print $1 % $2 }' # "1"
echo 61 62 | awk '{ print $1 % $2 }' # "61"
echo 62 62 | awk '{ print $1 % $2 }' # "0" not "62"
Yourz
是字符串的第 62 个字符charset
。您正在计算以 62 为模的偏移量,这意味着您的结果值可以在0
...范围内61
。由于awk
需要来自 的字符串偏移量1
,因此您需要做的是添加1
到结果值以计算偏移量,使其位于1
..范围内62
:
newch = substr(charset, ( (i+key)%62 )+1, 1)
答案2
该substr()
函数从字符串中挑选出一个子字符串。作为第二个参数给出的位置是从 1 开始计数,而不是从 0 开始,而模运算符在用作 时%62
,会给出 0 到 61 之间的整数。
这意味着您必须修改您的substr()
调用
newch = substr(charset, 1 + ((i + key)%62), 1)
或者,为了避免魔法常数,
newch = substr(charset, 1 + ((i + key)%length(charset)), 1)