简体   繁体   English

第二个“if”语句是什么意思(凯撒密码)

[英]What's the meaning of the second “if” statement (caesar cipher)

I recently took an interest in encryption, then I came across this piece code, but I don't understand the meaning of the second "IF" statement.我最近对加密感兴趣,然后我偶然发现了这段代码,但是我不明白第二个“IF”语句的含义。

#include<stdio.h>

int main()
{
    char message[100], ch;
    int i, key;

    printf("Enter a message to encrypt: ");
    gets(message);
    printf("Enter key: ");
    scanf("%d", &key);

    for (i = 0; message[i] != '\0'; ++i) {
        ch = message[i];

        if (ch >= 'A' && ch <= 'Z') {
            ch = ch + key;

            if (ch > 'Z') {
                ch = ch - 'Z' + 'A' - 1;
            }
            
            message[i] = ch;
        }
    }

    printf("Encrypted message: %s", message);

    return 0;
}

One of the fundamental things to understand for that code to make sense is that char represents both a character and a number at the same time.要使该代码有意义,要理解的基本内容之一是char同时代表一个字符和一个数字。

That's because to a C program (and effectively all programs on all computers) characters are just numbers anyway.这是因为对于 C 程序(以及所有计算机上的所有程序)而言,字符无论如何都只是数字。

When you say 'A' you could equivalently use 65 or 0x41 * .当您说'A'时,您可以等效地使用650x41 *

You already make use of that property by adding key to ch to "shift up" the characters (ie if key is 1 then 'A' becomes 'B' ).您已经通过向ch添加key以“向上移动”字符来使用该属性(即,如果key为 1,则'A'变为'B' )。

However, if you add 1 to 'Z' then the result is no longer an upper-case character (it will be [ in ASCII and compatible encodings).但是,如果将 1 添加到'Z' ,则结果不再是大写字符(它将是[在 ASCII 和兼容编码中)。

The second if is responsible to wrap those results back down to 'A' by first subtracting 'Z' (which means the value is now "how many steps above Z were we) and then adding 'A' - 1 (which means that 1 step above Z will result in 'A ').第二个 if 负责通过首先减去'Z' (这意味着现在的值是“我们比 Z 高多少步)然后添加'A' 'A' - 1 (这意味着 1高于Z的步骤将导致'A ”)。

* Assuming this is using ASCII or a compatible encoding, which will hold true on most modern operating systems * 假设这是使用ASCII或兼容的编码,这适用于大多数现代操作系统

Let's rewrite the calculation that is performed if the expression of the if statement is true :让我们重写如果if语句的表达式为true时执行的计算:

ch = ch - 'Z' + 'A' - 1;

to

ch = ch - ('Z' - 'A' + 1);

This should already make more sense.这应该已经更有意义了。 Now the part between parentheses of course never changes value, so lets introduce a constant:现在括号之间的部分当然永远不会改变值,所以让我们引入一个常量:

#define APHABET_SIZE = ('Z' - 'A' + 1)
...
ch = ch - ALPHABET_SIZE;

Ah, so if the value of the character is higher than Z, the last letter of the alphabet, then subtract the alphabet size.啊,所以如果字符的值高于字母表的最后一个字母Z,那么减去字母表的大小。


This is the kind of programming that you should not perform yourself;这是您不应该自己执行的编程; it performs all the functions at once instead of breaking them down, and trades briefness for readability.它一次执行所有功能而不是分解它们,并以简洁性换取可读性。

The Caesar cipher performs modular addition to shift the characters by index.凯撒密码执行模加法以按索引移动字符。 You should instead have a function int charToIndex(char) , then perform the modular addition (or subtraction for decryption) using the % modulo operator and ALPHABET_SIZE , and then have a function char indexToChar(int) to convert it back.您应该有一个 function int charToIndex(char) ,然后使用% modulo 运算符和ALPHABET_SIZE执行模加法(或用于解密的减法),然后有一个 function char indexToChar(int)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM