【问题标题】:How to use a set of numbers as the Key for RCA Encryption如何使用一组数字作为 RSA 加密的密钥
【发布时间】:2013-05-01 10:45:28
【问题描述】:

我想知道如何使用一组数字作为 rc4 加密的密钥。 根据互联网和维基,KEY 实际上是一串字母,但使用了字节。但在我的程序中,我需要使用 6 位数字作为 KEY。我应该将其转换为字符串还是如何转换。

Key Sheudling 算法如下所示。

void ksa(u_char *State, u_char *key) {
int byte, i, keylen, j=0;

keylen = (int) strlen((char *) key);

for(i=0; i<256; i++) {
    j = (j + State[i] + key[i%keylen]) % 256;
    swap(&State[i], &State[j]);
}

如何修改代码,或者我应该将数字转换为字符串。

【问题讨论】:

    标签: string rc4-cipher


    【解决方案1】:

    字符串和数字都是字节。这是一个接受无符号字符键的有效 RC4 代码:

    #include<stdio.h>
    #include<string.h>
    
    #define SIZE 256
    
    unsigned char SBox[SIZE];
    int i;
    int j;
    
    void initRC4(unsigned char Key[]);
    unsigned char getByte(void);
    
    void initRC4(unsigned char Key[])
    {
        unsigned char tmp;
        unsigned char KBox[SIZE];
    
        for(i=0;i<SIZE;i++)
            SBox[i]=i;
    
        for(i=0;i<SIZE;i++)
            KBox[i]=Key[i % strnlen(Key,SIZE)];
    
        for(j=0,i=0;i<SIZE;i++)
        {
            j=(j+SBox[i]+KBox[i]) % SIZE;
            tmp=SBox[i];
            SBox[i]=SBox[j];
            SBox[j]=tmp;
        }
    }
    
    unsigned char getByte(void)
    {
        unsigned char tmp;
    
        i=(i+1)%SIZE;
        j=(j+SBox[i])%SIZE;
        tmp=SBox[i];
        SBox[i]=SBox[j];
        SBox[j]=tmp;
    
        return SBox[(SBox[i]+SBox[j])%SIZE];
    }
    

    首先,初始化 RC4 流:

    initRC4(key);
    

    然后你做:

    getByte()
    

    ...它总是从您设置的 RC4 流中返回 1 个字节。

    有一点要记住——字符串中的一个字母并不总是等于 1 个字节。字符串中的整数和数字符号也是如此。真的,在弄乱密码之前,您必须阅读计算机编程简介。

    以下是整数字符串中字节不同的演示:

    #include <string>
    
    int main(int argc, char **argv) {
    
        const int n=67898;
        const std::string str = "67898";    
    
        const int arrayLength = sizeof(int);
        const int stringArrayLength = str.size();
        unsigned char *bytePtr=(unsigned char*)&n;
    
    
        printf("Bytes for integer: ");
        for(int i=0;i<arrayLength;i++)
        {
           printf("%X ", bytePtr[i]);
        }
        printf("\n");
    
        printf("Bytes for string: ");
        for(int i=0;i<stringArrayLength;i++)
        {
           printf("%X ", str.at(i));
        }
        printf("\n");
    
    
        return 0;
    
    }
    

    输出:

    Bytes for integer: 3A 9 1 0
    Bytes for string: 36 37 38 39 38
    

    通常在字符串的末尾会有一个终止字节,因此您可以在字符串大小上加上 +1 个字节。

    【讨论】:

    • 感谢您的代码。但我想知道如何将密钥作为整数集传递。您的 initRC4(key) 接受字符。我可以像这样传递数字吗? initRC4(67898);
    • 67898 在这里是一个整数。这将使用字节 1093A(十进制的 67898 = 十六进制的 1093A)初始化 RC4。我已经用代码更新了我的答案。
    • 非常感谢,现在我明白了它是如何工作的。如果我错了,请纠正我。所以我使用你的 getByte() 方法来获取我的整数的字节,我会将它传递给我的 initRC4() 方法。我说的对吗?
    • 没有。首先,您使用您的密钥(来自字符串或整数的字节)初始化 RC4 流。然后,您使用该 getByte() 函数提取 RC4 流的字节并使用它们。如果您确定您的整数将始终是有效整数(在类型转换之前没有 000034、012345 等),只需从中获取字节并使用它们初始化 RC4 流。
    猜你喜欢
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多