我想通过 RC4 的蛮力数组值找到 RC4 的内部状态,但我不知道如何蛮力它。
#include <stdio.h>
#include <string.h>
typedef unsigned long ULONG;
void rc4_init(unsigned char *s, unsigned char *key, unsigned long Len)
{
int i = 0, j = 0;
char k[256] = { 0 };
unsigned char tmp = 0;
for (i = 0; i<256; i++) {
s[i] = i;
k[i] = key[i%Len];
}
for (i = 0; i<256; i++) {
j = (j + s[i] + k[i]) % 256;
tmp = s[i];
s[i] = s[j];
s[j] = tmp;
}
}
void rc4_crypt(unsigned char *s, unsigned char *Data, unsigned long Len)
{
int i = 0, j = 0, t = 0;
unsigned long k = 0;
unsigned char tmp;
for (k = 0; k<Len; k++) {
i = (i + 1) % 256;
j = (j + s[i]) % 256;
tmp = s[i];
s[i] = s[j];
s[j] = tmp;
t = (s[i] + s[j]) % 256;
Data[k] ^= s[t];
printf("%d\n ", Data[k] ^= s[t]); //May be I have to brute force here
}
}
int main()
{
unsigned char s[256] = { 0 }; //S-box
char key[256] = { "12345678" };
char pData[512] = "testRC4";
ULONG len = strlen(pData);
printf("key : %s\n", key);
printf("raw : %s\n", pData);
rc4_init(s, (unsigned char *)key, strlen(key));
rc4_crypt(s, (unsigned char *)pData, len);
printf("encrypt : %s\n", pData);
rc4_init(s, (unsigned char *)key, strlen(key));
rc4_crypt(s, (unsigned char *)pData, len);
printf("decrypt : %s\n", pData);
getchar();
return 0;
}
在行 printf("%d\n ", Data[k] ^= s[t]); 像这样显示价值。
116 , 101 , 115 , 116 , 82 , 67 , 52
我不确定我必须暴力破解这个值,或者我必须为暴力破解编辑哪一行才能找到 RC4 的内部状态。请帮我。