키를 사용하여 각 문자에 대한 암호화된 문자 집합을 생성할 때 해당 문자가 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"
Your 는 z
문자열의 62번째 문자입니다 charset
. 오프셋 모듈로 62를 계산 중입니다. 이는 결과 값이 0
다음 범위 에 있을 수 있음을 의미합니다 61
. awk
의 문자열 오프셋이 필요하기 때문에 1
해야 할 일은 결과 값에 추가하여 오프셋이 .. 범위 내에 1
있도록 계산하는 것입니다 .1
62
newch = substr(charset, ( (i+key)%62 )+1, 1)
답변2
이 substr()
함수는 문자열에서 하위 문자열을 선택합니다. 두 번째 인수로 주어진 위치는 0이 아닌 1부터 세기 시작하며, 모듈로 연산자를 사용하면 %62
0에서 61 사이의 정수를 제공합니다.
substr()
이는 통화를 수정해야 함을 의미합니다.
newch = substr(charset, 1 + ((i + key)%62), 1)
또는 마법 상수를 피하기 위해,
newch = substr(charset, 1 + ((i + key)%length(charset)), 1)