2015-10-23 36 views
-3

我有2個例程,其中一個發送加密數據,另一個解密它。我不確定解析加密字段的正確方法。在位移和OR操作之後取回原始位

void send_the_encrypt_code (UINT32 field, UINT32 index) 
{ 
    UINT32 encrypt_code = (field << 5 | index); 
    pass_the_encrypt_code (encrypt_code); 
} 

void pass_the_encrypt_code (UINT32 encrypt_code) 
{ 
    UINT32 field ; 
    UINT32 index; 

/* How do I parse the field and index values from the encrypt_code and assign to the local variables field and index in this routine */?? 

} 

在此先感謝。

+0

您確定需要做OR嗎?它不應該是XOR嗎? –

+0

嗨,亞歷克斯。它應該是一個OR操作 – user2618994

+1

嗯,我不確定這個操作是否可以被逆轉......你是從哪裏得到這種加密方法的? (也許我會更好地理解它) –

回答

1

嗯,因爲我上的評論說,我不認爲這是可能恢復這種轉變......

如果您只需要包裝和掩蓋你可以連接的數據的領域和應用一個簡單異或編碼...

會這樣的工作嗎?

請注意,您不能將打包的數據存儲在原始數據大小相同的容器中,因此char *。

char key[] = {'P','A','S','S','K','E','Y','1'}; 

void decrypt (char * encrypted){ 
    UINT32 field, index; 
    UINT32 xor_field, xor_index; 

    memcpy(&xor_field,&encrypted[0],sizeof(xor_field)); 
    memcpy(&xor_index,&encrypted[4],sizeof(xor_index)); 

    memcpy(&field,&key[0],sizeof(field)); 
    memcpy(&index,&key[4],sizeof(index)); 

    field ^= xor_field; 
    index ^= xor_index; 
} 

void encrypt (UINT32 field, UINT32 index){ 
    char encrypted[8]; 
    UINT32 xor_field, xor_index; 

    memcpy(&xor_field,&key[0],sizeof(xor_field)); 
    xor_field ^= field; 
    memcpy(&xor_index,&key[4],sizeof(xor_field)); 
    xor_index ^= index; 

    memcpy(&encrypted[0],&xor_field,sizeof(xor_field)); 
    memcpy(&encrypted[4],&xor_index,sizeof(xor_index)); 

    decrypt(encrypted); 
}