所以我实现了ciphersaber-1差不多可以了,我可以解密cstest1.cs1但是我很难让cstest2.cs1工作。
输出为:
The Fourth Amendment to the Constitution of the Unite ▀Stat→s of America
"The right o☻ the people to be secure in their persons, houses, papers, and
effects, against unreasonab→e searches an╚A)┤Xx¹▼☻dcðþÈ_#0Uc.?n~J¿|,lómsó£k░7╠▄
íuVRÊ ╣├xð"↕(Gû┤.>!{³♫╚Tƒ}Àõ+»~C;ÔÙ²÷g.qÏø←1ß█yÎßsÈ÷g┐ÅJÔÞ┘Îö║AÝf╔ìêâß╗È;okn│CÚê
õ&æÄ[5&Þ½╔s╦Nå1En♂☻♫ôzÓ9»Á╝ÐÅ├ðzÝÎòeØ%W¶]¤▲´Oá╗e_Ú)╣ó0↑ï^☻P>ù♂¥¯▄‗♦£mUzMצվ~8å
ì½³░Ùã♠,H-tßJ!³*²RóÅ
所以我在初始化状态时一定有个错误奇怪的是,我可以加密和解密长文本没有问题,所以错误是对称的。
我将rc4密码实现为可重入的单字节算法,如rc4.c中所示。
状态存储在rc4_state结构中:
typedef unsigned char rc4_byte;
struct rc4_state_
{
rc4_byte i;
rc4_byte j;
rc4_byte state[256];
};
typedef struct rc4_state_ rc4_state;
状态用rc4_init初始化:
void rc4_init(rc4_state* state, rc4_byte* key, size_t keylen)
{
rc4_byte i, j, n;
i = 0;
do
{
state->state[i] = i;
i++;
}
while (i != 255);
j = 0;
i = 0;
do
{
n = i % keylen;
j += state->state[i] + key[n];
swap(&state->state[i], &state->state[j]);
i++;
}
while (i != 255);
state->i = 0;
state->j = 0;
}
实际的加密/解密在rc4中完成:
rc4_byte rc4(rc4_state* state, rc4_byte in)
{
rc4_byte n;
state->i++;
state->j += state->state[state->i];
swap(&state->state[state->i], &state->state[state->j]);
n = state->state[state->i] + state->state[state->j];
return in ^ state->state[n];
}
为了完整起见,交换:
void swap(rc4_byte* a, rc4_byte* b)
{
rc4_byte t = *a;
*a = *b;
*b = t;
}
我在这件事上已经伤了头两天多了。。。状态,至少对于“asdfg”键是正确的。任何帮助都很好。
整个事情都可以在我的github复发中找到:https://github.com/rioki/ciphersaber/
最佳答案
我在网上搜索时无意中发现了你的问题,但由于你还没有更新your code at GitHub,我想你还是想知道问题是什么。
在这段代码中:
i = 0;
do
{
state->state[i] = i;
i++;
}
while (i != 255);
循环迭代255次后,
i
的值为255,循环将终止。因此,状态缓冲区的最后一个字节未初始化。这很容易解决。只需将
while (i != 255);
更改为while (i);
。关于c - CipherSaber错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16603562/