2015-10-12 52 views
1

我想通過RC4的蠻力數組值來查找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的狀態。請幫幫我。

+0

'數據[K]^= S [噸]; printf(「%d \ n」,Data [k]^= s [t]);'打印'Data [k]'並且不修改它。你想要做什麼? – MikeCAT

回答

0

116,101,115,116,82,67,52是 「testRC4」,即,未加密值

你的printf()語句實際上是改變你的數據恢復到原來由於ASCII。 ...

Data[t] ^= s[t] 

它既是一個賦值,又有一個r值。因此,您首先要更改Data [t],然後獲取新值並將其打印出來。但就在此之前,您使用相同的表達式來設置值。 ((N^Y)^ Y)== N.

更改您的printf爲...

printf("%d\n",Data[t]);