CipherSaber 漏洞

标签 c encryption rc4-cipher

所以我实现了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 reopsitory中找到: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);

关于CipherSaber 漏洞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16603562/

相关文章:

c - valgrind 报告奇怪的内存使用情况

algorithm - 密文窃取算法 - 哪一种是正确的?

encryption - 非网页游戏的安全在线高分列表

encryption - Jdk11 支持哪些密码套件算法以及哪个最适合与 TLSv1.2 一起使用

在 C 中创建 HTTP 客户端以下载网页以供离线查看

c - 将 Tcl_Filesystem 替换为副本时,tcl "open"命令不起作用

ruby-on-rails - 如何在 Ruby/Rails Web 服务器环境中将密码安全地存储在配置文件中?

ssl - TLS:硬禁用密码与不列出密码

rc4-cipher - WEP(共享 key 身份验证),如何形成136字节的质询响应?

c++ - 如何确保 SqLite 中的只读事务?