Transform class from C++ to C#

0

I am new to C# and I need to use a class I had previously in PHP but now I have it in C++, the problem is that I do not know how to transform it to C# in the correct way, I have been trying but I do not get the correct result.

The class should encrypt me the string I enter, for example if I put "HelloDog", you should return the code "C5WXRH0CX9NE10".

This is my original code in C ++:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

uint32_t stepa(uint32_t ins)
{
    uint32_t res = 0;
    uint32_t dat;
    uint8_t* key = (uint8_t*)"\x1A\x1F\x11\x0A\x1E\x10\x18\x02\x1D\x08\x14\x0F\x1C\x0B\x0D\x04\x13\x17\x00\x0C\x0E\x1B\x06\x12\x15\x03\x09\x07\x16\x01\x19\x05\x12\x1D\x07\x19\x0F\x1F\x16\x1B\x09\x1A\x03\x0D\x13\x0E\x14\x0B\x05\x02\x17\x10\x0A\x18\x1C\x11\x06\x1E\x00\x15\x0C\x08\x04\x01";
    int i;
    for(i = 0; i < 64; i++)
    {
        dat = ins - (ins & 0xFFFFFFFE);
        ins >>= 1;
        if(dat)
        {
            res += dat << key;
        }
        if(!ins)
        {
            return res;
        }
    }
    return res;
}

void stepb(uint32_t ins)
{
    uint8_t* key = (uint8_t*)"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    uint32_t dat;
    int i;
    for(i = 0; i < 7; i++)
    {
        dat =   
        ins -= ((dat * 9) << 2);
        if(ins < 36)
        {
            printf("%c", key[ins]);
        }
        ins = dat;
    }
}

int main(int argc, char** argv)
{
    if(argc < 2)
    {
        if(argc == 1)
        {
            printf("usage: %s password\n", argv[0]);
        }
        return 0;
    }
    uint8_t len;
    uint8_t i;
    uint8_t xtr;
    uint32_t* dat;

    len = strlen(argv[1]);
    xtr = len % 4;
    if(xtr)
    {
        xtr = 4 - xtr;
    }

    dat = (uint32_t*)malloc(len + xtr);
    memcpy((void*)dat, (void*)argv[1], len);
    if(xtr)
    {
        memset((void*)(((uint8_t*)dat) + len), 0, xtr);
        len += xtr;
    }
    len /= 4;
    for(i = 0; i < len; i++)
    {
        stepb(stepa(dat + 0x3e8));
    }
    printf("\n");
    return 0;
}

And this is the code that I tried to use in C #, it seems that everything is fine but I do not receive any value:

static class Test
{
    private static UInt32 stepa (UInt32 ins)
    {
        UInt32 res = 0;
        UInt32 dat;
        byte[] key = Encoding.Default.GetBytes("\x1A\x1F\x11\x0A\x1E\x10\x18\x02\x1D\x08\x14\x0F\x1C\x0B\x0D\x04\x13\x17\x00\x0C\x0E\x1B\x06\x12\x15\x03\x09\x07\x16\x01\x19\x05\x12\x1D\x07\x19\x0F\x1F\x16\x1B\x09\x1A\x03\x0D\x13\x0E\x14\x0B\x05\x02\x17\x10\x0A\x18\x1C\x11\x06\x1E\x00\x15\x0C\x08\x04\x01");
        int i;
        for (i = 0; i < 64; i++)
        {
            dat = ins - (ins & 0xFFFFFFFE);
            ins >>= 1;
            if (string.IsNullOrEmpty(dat.ToString()))
            {
                res += dat << key[i];
            }
            if (string.IsNullOrEmpty(ins.ToString()))
            {
                return res;
            }
        }
        return res;
    }

    private static void stepb (UInt32 ins)
    {
        byte[] key = Encoding.Default.GetBytes("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
        UInt32 dat;
        int i;
        for (i = 0; i < 7; i++)
        {
            dat = (UInt32)(((UInt64)ins) * 0x38e38e39) >> 35;
            ins -= ((dat * 9) << 2);
            if (ins < 36)
            {
                Console.WriteLine("%c", key[ins]);
            }
            ins = dat;
        }
    }

    public static int encrypt(string argv)
    {
        int len = argv.Length;
        int xtr = len % 4;
        if (string.IsNullOrEmpty(xtr.ToString()))
        {
            xtr = 4 - xtr;
            len += xtr;
        }
        len /= 4;
        for (int i = 0; i < len; i++)
        {
            stepb(stepa((UInt32)argv[i] + 0x3e8));
        }
        return 0;
    }
}
c#
c++
encryption
encoding
asked on Stack Overflow Sep 19, 2018 by Kokox

1 Answer

0

I am not sure if you have invoked the Encrypt function in your program. If not you can use below main function to call :

public static void Main()
{
   Test.encrypt("HelloDog");
}

Also in your logic in stepb function dat value is always zero(I don't find it in C++ too).Hence you get just 48(0) in Console for any input character

Also instead of Console.WriteLine("%c", key[ins]);
you should use Console.Write((char)key[ins]);

answered on Stack Overflow Sep 19, 2018 by novice • edited Sep 19, 2018 by novice

User contributions licensed under CC BY-SA 3.0