聲明:無論以下情況如何,均與Affine Cipher相關。對於不知道它的人,這是一種使用數學函數Ax + B根據字母表中字母索引來移動給定明文中的每個字母的加密方法。C中的仿射密碼無法正常工作
我寫了一個代碼,使用仿射密碼對給定的明文或加密文本進行加密和解密。它由以下三個功能組成:
char *encryption(char Plaintext[MAXSIZE], int A, int B);
int modularinverse(int number, int modulo);
char *decryption(char Ciphered[MAXSIZE], int A, int B, int inverse);
解密函數中涉及到我站點的部分。大約三四個字母是錯過計算的。
讓我們考慮下面的明文: 「A B C d e」 的
使用加密函數:
char *encryption(char Plaintext[MAXSIZE], int A, int B) {
static char Ciphered[MAXSIZE];
int i;
int y;
int index;
for (i = 0; i < strlen(Plaintext) - 1; i++) {
if (Plaintext[i] == ' ') {
Ciphered[i] = ' ';
} else {
index = (int)Plaintext[i] - 'a';
y = (A * index + B) % 26;
Ciphered[i] = (char)y + 97;
}
}
return Ciphered;
}
它變成明文分爲: 「F米T A H」。哪個是對的。
解密明文應該明顯地給出:「a b c d e」。相反,它給出了:「a b c J e」。
char *decryption(char Ciphered[MAXSIZE], int A, int B, int inverse) {
static char NewPlaintext[MAXSIZE];
int i;
unsigned int x;
int y;
int index;
for (i = 0; i < strlen(Ciphered); i++) {
if (Ciphered[i] == ' ') {
NewPlaintext[i] = ' ';
} else {
index = (int)Ciphered[i] - 'a';
x = inverse * (index - B) % 26;
NewPlaintext[i] = (char)x + 97;
}
}
return NewPlaintext;
}
字母d
由於我不知道的原因而錯過計算。印刷變量index
,inverse
,B
和x
的人物f m t a h
將分別返回這個每個人的價值觀:
5 15 5 0
12 15 5 1
19 15 5 2
0 15 5 -23
7 15 5 4
第一列代表的字母f m t a h
的指標。
第二列代表A=7
的倒數,即15
。 (完全有害,你可以忽略它)。
第三列表示B現在是一個常數(您可以忽略它)。
第四列代表x是inverse*(index-B) % 26
的結果。在該列中的每個數字上加97('a'的ASCII碼)將導致每個字母的ASCII碼。
即0 + 97 = 97這是'a'。由結果解密(f)= a。
但是,如果你能注意到。字母'a'的x的結果是-23。 -23 + 97 = 74這是ASCII碼中的J。它應該是100,因爲它是d的ASCII碼。因此x的結果應該是3而不是-23。
這個錯過計算背後的原因是嗡嗡我,我還沒有想出是什麼原因造成的。
我假設你正在學習編碼。如果是這樣,自己解決問題,學習如何調試非常重要。添加打印語句,跟蹤執行,添加斷點,觀察變量和** think **。 [Richard Feynman](https://en.wikipedia.org/wiki/Richard_Feynman)通過思考固定收音機。想想:「這怎麼可能發生」? – zaph
@zaph我試過了,但我想不通爲什麼編譯器認爲簡單計算的結果是-23。我已經測試過它,它給了我3它應該是。 – Amine
@Amine:編譯器不會*思考*,它會生成代碼,如果'B'大於'index',那麼您編寫的表達式可以具有負值。劃分向'0'截去,'-23%26'評估爲'-23'。 – chqrlie