Thread: Cypher algorithm in C.

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    3

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,248
    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.
    Last edited by laserlight; 2 Weeks Ago at 12:35 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    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. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    703
    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[64] = {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);
    }
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ROT13 Cypher
    By Sourabh Verma in forum C Programming
    Replies: 6
    Last Post: 02-14-2013, 11:52 AM
  2. caesar cypher
    By me77 in forum C Programming
    Replies: 17
    Last Post: 04-03-2009, 07:48 AM
  3. Algorithm Help
    By (TNT) in forum Tech Board
    Replies: 2
    Last Post: 07-28-2007, 02:22 AM
  4. Caesar Cypher
    By Tommo in forum C Programming
    Replies: 10
    Last Post: 06-29-2007, 12:21 PM
  5. my grandfather's chess algorithm can beat your grandfather's chess algorithm...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 08-17-2001, 06:52 PM

Tags for this Thread