patterncsharpMinor
Are these C# and Python code snippets functionally identical?
Viewed 0 times
thesearesnippetsandidenticalpythoncodefunctionally
Problem
I have a bit of Python source code that I want to recreate in C#. The code is about reading and decrypting a binary file. I have tested the function on an existing file and it runs without errors; while the resulting string is not garbled or anything, it does not appear to be useful to me.
But that is outside the scope of this question. I only want to know if I have translated the function correctly to C# so it does the same as in Python.
The Python code:
Here is the C# code I wrote:
```
string path = _path;
string fileName = _fileName;
char[] chars = fileName.ToCharArray();
int key = 0;
foreach (char c in chars)
{
key += c;
}
StringBuilder builder = new StringBuilder();
using (FileStream file = new FileStream(path, FileMode.Open))
{
bool tick = true;
while (true)
{
int i = file.ReadByte();
if (i == -1)
break;
if (tick)
key += 2;
else
key -= 2;
tick = !tick;
//The next 2 lines are for parsing the short, equivalent(?) to struct.unpack('h', byte)
i <<= 8;
i += file.ReadByte();
i ^= key;
byte b = (byte)i;
char c = (char)
But that is outside the scope of this question. I only want to know if I have translated the function correctly to C# so it does the same as in Python.
The Python code:
filename = os.path.basename(path_and_filename)
key = 0
for char in filename:
key = key + ord(char)
f = open(path_and_filename, 'rb')
results = ''
tick = 1
while True:
byte = f.read(2)
if byte:
if tick:
key = key + 2
else:
key = key - 2
tick = not tick
res = struct.unpack('h', byte)[0] ^ key
results = results + chr(res)
continue
break
f.close()
return resultspath_and_filename is an absolute path to the encrypted file.Here is the C# code I wrote:
```
string path = _path;
string fileName = _fileName;
char[] chars = fileName.ToCharArray();
int key = 0;
foreach (char c in chars)
{
key += c;
}
StringBuilder builder = new StringBuilder();
using (FileStream file = new FileStream(path, FileMode.Open))
{
bool tick = true;
while (true)
{
int i = file.ReadByte();
if (i == -1)
break;
if (tick)
key += 2;
else
key -= 2;
tick = !tick;
//The next 2 lines are for parsing the short, equivalent(?) to struct.unpack('h', byte)
i <<= 8;
i += file.ReadByte();
i ^= key;
byte b = (byte)i;
char c = (char)
Solution
This section seems wrong:
you're shifting the value in
Whats with this? It is casting an int to a byte to a char and it is truncating.
so you're better off ignoring the first byte and instead everything after
could be:
In Python
And IMHO it is a bad idea to roll your own encryption/decryption like this. Use a trusted known solution instead.
Question: I only want to know if I have translated the function correctly to C# so it does the same as in Python
Answer Its more of a transliteration rather than a translation but they should give the same output. If I were to re-write the python into c# it would look different as it seems inefficient to me.
//The next 2 lines are for parsing the short, equivalent(?) to struct.unpack('h', byte)
i <<= 8;
i += file.ReadByte();you're shifting the value in
i 8 bits left and then reading another byte. I'm guessing because the original code read it in 2 bytes at a time. If your file isn't the right length then this will break it here. what about:i <<= 8;
int secondByte = file.ReadByte()
if(secondByte != -1)
{
i += secondByte ;
}Whats with this? It is casting an int to a byte to a char and it is truncating.
byte b = (byte)i;
char c = (char)b;so you're better off ignoring the first byte and instead everything after
tick = !tick;could be:
i = file.ReadByte();
char c = (char)(i ^ (key & 255));
builder.Append(c);In Python
chr() will throw an exception if the value is greater than 255 anyway. And IMHO it is a bad idea to roll your own encryption/decryption like this. Use a trusted known solution instead.
Question: I only want to know if I have translated the function correctly to C# so it does the same as in Python
Answer Its more of a transliteration rather than a translation but they should give the same output. If I were to re-write the python into c# it would look different as it seems inefficient to me.
Code Snippets
//The next 2 lines are for parsing the short, equivalent(?) to struct.unpack('h', byte)
i <<= 8;
i += file.ReadByte();i <<= 8;
int secondByte = file.ReadByte()
if(secondByte != -1)
{
i += secondByte ;
}byte b = (byte)i;
char c = (char)b;i = file.ReadByte();
char c = (char)(i ^ (key & 255));
builder.Append(c);Context
StackExchange Code Review Q#45359, answer score: 4
Revisions (0)
No revisions yet.