# Thread: Cypher algorithm in C.

1. ## Cypher algorithm in C.

I cant figure out how this function decyphers a 6 letter string.

Code:
```void cypher_5(long param_1)

{
int iVar1;

int iVar2;

iVar1 = string_length();

if (iVar1 != 6) {

printf("error");
}

iVar2 = 0;

iVar1 = 0;

while (iVar1 < 6) {

iVar2 = iVar2 + *(int *)(array.3597 + (ulong)((uint)*(byte *)(param_1 + iVar1) & 0xf) * 4);

iVar1 = iVar1 + 1;

}

if (iVar2 != 0x31) {

printf("error");

}

return;

}``` 2. I suggest that you post a compilable program rather than just a fragment with unexplained parts. I can make guesses based on what I see, but why guess when you can post the definitions of param_1 and array.3597? Or string_length, for that matter. 3. unfortunately this is reversed engenieered code,i only have access to the code in assembly and used ghidra to get the source code in C. 4. It's not "decyphering" anything. It's calculating a sum and then complaining if it's not 0x31.

With some assumptions, I think the code is better expressed like this (I've given variables somewhat arbitrary names, particularly "table").
Code:
```#include <stdio.h>

typedef unsigned char byte;
typedef unsigned int  uint;
typedef unsigned long ulong;

int  array = {1, 2, 3, 4, 5, 6, 7, 8, 9};
byte table[]   = {0, 1, 2, 3, 4, 5};

void cypher_5(byte *tab) {
//if (string_length() != 6) printf("error\n");

int sum = 0;
for (int i = 0; i < 6; ++i)
sum += array[ (uint)tab[i] & 0xf ];

//        sum += *(int*)(array +  // this is assuming array is a byte array
//                        (ulong)( (uint)*(byte*)(tab + i) & 0xf ) * 4
//                      );

printf("%d\n", (int)sum);

if (sum != 0x31) printf("error\n");
}

int main() {
cypher_5(table);
}``` Popular pages Recent additions c language, cypher, function, ivar1, ivar2 