string encrypt(const string message) {
srand(time(0));
static int secret = rand() % 32000;
string output;
int i = 0, a = 0, b = 1, c = 0;
for (i = 0; i < message.size(); ++i) {
output += ((int)message[i]) - (secret + c);
c = a + b;
a = b;
b = c;
}
return output;
}
Hey, this is an example of encryption from my teacher. But, I still confuse what does (int)message[i]
mean? and what does the program do on that line?
嘿,这是我老师给我的一个加密例子。但是,我仍然搞不懂(Int)Message[i]是什么意思?那么程序在这条线上做什么呢?
更多回答
It converts the character at position i
in the message to its ASCII value (an integer)
它将消息中位置i的字符转换为其ASCII值(一个整数)
OT: srand (time(0));
do this only once in your program. On most systems time(NULL)
returns a time with second resolution. If called twice or more within the same second, it will reset the seed to the same value, leading to non-random results from rand()
. Besides, C++ have had much better PRNG library functions than those old C functions for the last 12 or so years.
OT:srand(time(0));在你的程序中只做一次。在大多数系统上,time(NULL)返回第二个分辨率的时间。如果在同一秒内调用两次或更多次,它会将种子重置为相同的值,从而导致来自rand()的非随机结果。此外,在过去的12年左右的时间里,C++的PRNG库函数比那些旧的C函数要好得多。
@Weetz Or whatever encoding is used on the target system. ASCII is not mandated anywhere by C++.
@weetz或目标系统上使用的任何编码。在任何地方,C++都不强制使用ASCII。
Correct. It could be unicode or whatever
对,是这样。它可以是Unicode或其他任何格式
OT: does your teacher not indent their code?
OT:你的老师不缩进他们的代码吗?
What does (int)message[i]
mean?
The string message
can be treated like an array. The expression message[i]
references the character at position i
, where the first character is in position zero.
可以将字符串消息视为数组。表达式Message[i]引用位置i处的字符,其中第一个字符位于位置零。
For std::string
, individual characters have type char
.
对于std::字符串,单个字符的类型为char。
When you output a char
, using, for example, std::cout
, it is displayed as a character.
例如,当您使用std::cout输出字符时,它将显示为字符。
When you use a char
in an expression, it is treated as an integer value. If, for instance, the underlying character set is ASCII, then the integer value of character 'A'
is 65. 'B'
is 66, and so on.
当您在表达式中使用字符时,它将被视为整数值。例如,如果基础字符集是ASCII,则字符‘A’的整数值是65。B是66,以此类推。
On most implementations, type char
is a signed integral type, but that is not required. The C++ Standard also allows it to be an unsigned integral type. Each implementation makes its own choice.
在大多数实现中,类型char是带符号的整型,但这不是必需的。C++标准还允许它是无符号整型。每个实现都做出了自己的选择。
(int)
is a C-style type cast. It converts the expression that follows it into an int
. In this case, it converts message[i]
from type char
to type int
. Both are numbers.
(int)是一个C风格的类型转换。它将后面的表达式转换为int。在本例中,它将message[i]从char类型转换为int类型。两者都是数字。
What does the program do on that line?
output += ((int)message[i]) - (secret + c);
This line encrypts the character at position i
of the message
, and appends the result to the end of string output
.
该行对消息位置i处的字符进行加密,并将结果附加到字符串输出的末尾。
For type std::string
, which is the type of variable output
, operator+=
is the append operator. It evaluates the expression that follows +=
, implicitly converts the result to type char
, and appends it to the string identified on the left of +=
.
对于STD::STRING类型,这是变量输出的类型,操作符+=是追加操作符。它计算+=后面的表达式,将结果隐式转换为char类型,并将其附加到+=左侧标识的字符串。
Thanks to @rossum, who identifies the encryption method as a form of stream cipher. It uses some arithmetic to convert the integer value of message[i]
into some other integer value, which is then interpreted as a char
. For a more precise description, see this Wikipedia article.
感谢@Rossum,他将加密方法识别为流密码的一种形式。它使用某种算法将Message[i]的整数值转换为其他一些整数值,然后将其解释为字符。有关更准确的描述,请参阅这篇维基百科文章。
@Neil honed in on the encryption algorithm a little more, pointing out that the "stream" that is subtracted from the plaintext is the Fibonacci sequence, plus a constant, "secret" value.
@Neil进一步研究了加密算法,指出从明文中减去的“流”是斐波那契序列,加上一个常数,“秘密”值。
更多回答
The encryption is a form of stream cipher where each character/byte in the plaintext is combined with the current number in the stream. After each stream number is used, the values of a
, b
and c
are changed.
加密是流密码的一种形式,其中明文中的每个字符/字节与流中的当前数字组合。在使用每个流编号之后,a、b和c的值被改变。
I think stream cipher is used very loosely. You don't need to calculate all the positions in the stream before to calculate the next letter. It's just subtracting the, essentially standard, Fibonacci sequence from all the letters.
我认为流密码的使用非常宽松。在计算下一个字母之前,您不需要计算流中的所有位置。它只是从所有字母中减去本质上标准的斐波纳契序列。
我是一名优秀的程序员,十分优秀!