tags:

views:

125

answers:

4

Ive been asked to convert some C++ code so that we can use it in a C# application. This snippet of code is used to decrypt a registration licence key which is embedded and passed about in configuration files.

It looks to me like encrypting the string 2 bytes (correction) at a time and for the life of me, I cant work out how to do something similar in C#.

void APIENTRY EncryptRegBuffer(LPSTR StrInput,int SizeInput,LPSTR StrOut)
{
#define   SEMENTE 17
#define   COMUL   37
    WORD  randomic=SEMENTE;
    WORD *pw;
    int   i;

    memcpy(StrOut,StrInput,SizeInput);
    StrOut[SizeInput]=NULO;
    pw=(WORD *) StrOut;
    for(i=0; i < (SizeInput/2); ++i) {
     randomic*=COMUL;
     *pw+=randomic;
     ++pw;
    }
}

Can someone advise me on the methods use to perform these kinds of operations on a string using C#?

A: 

look at this answer

it was about bits then now bytes

Fredou
Sorry, but that question/answer has nothing to do with code in this question.
Binary Worrier
@the -1 vote, he changed his question, was about bit now bytes
Fredou
Downvote removed.
Binary Worrier
+2  A: 

It's actually encoding the string by two bytes (WORD size) at a time. An alternative way of writing this is:

int j=0;
for(i=0; i<SizeInput/2; ++i) {
  randomic *= COMUL;
  StrOut[j] += randomic;
  j += 2;
}
Stephen Doyle
A: 

I don't see where you think it's two "bits" at a time. It appears to be working with whole bytes. All you should need to get started is:

byte[] bytes = ASCII.GetBytes(inputString);
John Fisher
+1  A: 

How about something like:

public string EncryptRegBuffer(string input)
{
    const UInt16 SEMENTE = 17;
    const UInt16 COMUL = 37;

    int randomic = SEMENTE;
    string output = "";

    foreach (char c in input) {
        randomic *= COMUL;
        output += (char)(c + randomic);
    }
    return output;
}
Jonas Elfström