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.
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).
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: