Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C# C# encrypting a file

codergirl

Coder
Hi

I'm trying to convert an encryption and decryption function written in C (not by me) to code in C# that's doing the same (= same result).

Here are the C functions.

C:
//variables
char *thfXORstr = "e!7Í“½<Ànš+ÉÉÞñrN÷55ï!`èõ33l$Âp";
unsigned char thfEncrAdd = 43;

//encryption function
char *thfEncryptBlock(char *dest, long size)
{
    int xorlen;
    char *savedest = dest;

    xorlen = strlen(thfXORstr) - 1;
    while(size--)
           {
        *dest ^= thfXORstr[thfXORcount++];
        *dest++ += thfEncrAdd;

        if(thfXORcount > xorlen) thfXORcount = 0;
    }

    return savedest;
}

//decryption function
char huge *thfDecryptBlock(char huge *dest, long size)
{
    int xorlen;
    char huge *savedest = dest;

    xorlen = strlen(thfXORstr) - 1;
    while(size--)
            {
        *dest -= thfEncrAdd;
        *dest++ ^= thfXORstr[thfXORcount++];

        if(thfXORcount > xorlen) thfXORcount = 0;
    }

    return savedest;
}


Here are the C# functions I tried to make but I'm not sure if they are doing the same as the C code.
Specific things I'm not sure if they are doing the same:

For the 2 variables used:

- is a "char *" in C the same as a "String" in C#?

- is a "unsigned char" in C the same as a "byte" in C#?


For the encryption and decryption functions:

- is the XOR operation correctly coded in C# by using the foreach loop? I convert the String first to a byte array and then loop through it with the foreach loop (see code below)

- is the Addition and Subtraction correctly coded in C#? (see code below)

So the goal is to make code in C# that's doing exactly the same as the C code.
The C code functions have input parameter of type "char *" but I used a FileStream in C# (which is OK for me because the code will be run on files on hard drive).


C#:
//variables
private const String thfXORstr = "e!7Í“½<Ànš+ÉÉÞñrN÷55ï!`èõ33l$Âp";

private byte thfEncrAdd = 43;

//encryption function
void EncryptFile(string inputFile, string outputFile)
{
    var fileStreamInput = new FileStream(inputFile, FileMode.Open);
    var fileStreamOutput = new FileStream(outputFile, FileMode.Create);
  
    byte[] buffer = new byte[4096];
    while (true)
    {
        int bytesRead = fileStreamInput.Read(buffer);
        if (bytesRead == 0)
            break;
        EncryptBytes(buffer, bytesRead);
        fileStreamOutput.Write(buffer, 0, bytesRead);
    }

    fileStreamInput.Close();
    fileStreamOutput.Close();
}

void EncryptBytes(byte[] buffer, int count)
{
    for (int i = 0; i < count; i++)
    {
        //XOR
        byte[] bytes = Encoding.ASCII.GetBytes(thfXORstr);
        foreach (byte b in bytes)
        {
            //Debug.WriteLine(b);
            buffer[i] = (byte)(buffer[i] ^ b);
        }

        //Addition
        buffer[i] = (byte)(buffer[i] + thfEncrAdd);
    } 
}

//decryption function
void DecryptFile(string inputFile, string outputFile)
{
    var fileStreamInput = new FileStream(inputFile, FileMode.Open);
    var fileStreamOutput = new FileStream(outputFile, FileMode.Create);

    byte[] buffer = new byte[4096];
    while (true)
    {
        int bytesRead = fileStreamInput.Read(buffer);
        if (bytesRead == 0)
            break;
        DecryptBytes(buffer, bytesRead);
        fileStreamOutput.Write(buffer, 0, bytesRead);
    }

    fileStreamInput.Close();
    fileStreamOutput.Close();
}

void DecryptBytes(byte[] buffer, int count)
{
    for (int i = 0; i < count; i++)
    {
        //Subtraction
        buffer[i] = (byte)(buffer[i] - thfEncrAdd);

        //XOR
        byte[] bytes = Encoding.ASCII.GetBytes(thfXORstr);
        foreach (byte b in bytes)
        {
            //Debug.WriteLine(b);
            buffer[i] = (byte)(buffer[i] ^ b);
        }           
    }
}
 
Last edited:
I would like to help but there's too many unknowns for me. For example, despite some decades of C experience, I had never heard of huge pointers. Could this be C code that is very old and/or for a specific platform ? My Visual Studio 2022 C compiler flags this as a syntax error. I also don't understand the role of the variable thfXORcount which is neither defined nor getting initialized. Does that C code actually work, can you compile and debug it ? And then use the encrypted/decrypted data as input for your C# code to verify it does the same, and if not, debug the C# code ?
I don't quite get the C# code either. Why do the encrypt/decrypt functions have two loops where the C originals have only one ? Does the C# code have compile errors ?

As for the questions, I'll take a very lame shot at them:

- is a "char *" in C the same as a "String" in C#?
No. You cannot compare a C# class to C primitive pointer, even though both ultimately store a sequence of characters.

- is a "unsigned char" in C the same as a "byte" in C#?
For all practical purposes, yes. Although char is meant to be a printable character and byte an arbitrary printable number.

- is the XOR operation correctly coded in C# by using the foreach loop? I convert the String first to a byte array and then loop through it with the foreach loop (see code below)
I don't think so. The C loop through the source string and xor's each character with the corresponding character of thfXORstr. The C# code also loops through the source string but xor's each character with all the characters in thfXORstr in turn. Seems like different behavior to me.

- is the Addition and Subtraction correctly coded in C#? (see code below)
Seems to me it is. Why do you ask ? Are you getting compile errors on it ?

I'm not sure anything I wrote helps, or even makes sense... TBH I don't really understand half of what is going on here 😮
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom