'DES encryption in C#, same input different output when freshly compiled, compared to old binary
the below code was used several years ago to generate a binary that was used for encrypting some files, using DES algorithm (I know DES is weak and AES is recommended, but those are not my requirements).
the binary has been encrypting those files, which were successfully decrypted by the submission portal.
DES being a bijective algorithm for a single key, a single input should always yield the same output, and one output can only be obtained from one input.
However, recompiling the code today, the output file obtained is different than what the old binary used to yield. Meaning if i tried submitting the new file on the test portal, it would fail.
However, the code itself has not changed.
As to why I want to recompile the code:
My goal was to migrate the function to another language, and seeing as I was unable to obtain the same output, decided to try step-by-step debugging it in C# to see where the difference was coming from - except the old and new C# binaries are also returning different outputs...
Edit 1:
the input and outputs as requested:
input: hello world
old out: 5ELrrBHwxbNDT9Clywhhvg==
new out: RYZbVdhS+c5nRErRgZxZiw==
but since the key above is not the real key anyway, not sure it would be of help.
Edit 2:
tried all different combinations of CipherMode and PaddingMode, no match still.
Could the .NET / Framework / C# version matter? (newbie question, first time using C#)
although it shouldn't since the underlying algorithm is the same. Any ideas?
the C# code:
String strCryptKey = "12345678";
Byte[] strIVkey = new Byte[8];
strIVkey[0] = 0x13;
strIVkey[1] = 0x14;
strIVkey[2] = 0x15;
strIVkey[3] = 0x16;
strIVkey[4] = 0x17;
strIVkey[5] = 0x18;
strIVkey[6] = 0x19;
strIVkey[7] = 0x20;
string sourceString;
using (var streamReader = new StreamReader(args[0], Encoding.UTF8))
{
sourceString = streamReader.ReadToEnd();
byte[] sourceArray = Encoding.UTF8.GetBytes(sourceString);
MemoryStream tempM = new MemoryStream();
DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(
tempM,
des.CreateEncryptor(Encoding.UTF8.GetBytes(strCryptKey), strIVkey),
CryptoStreamMode.Write
);
encStream.Write(sourceArray, 0, sourceArray.Length);
encStream.FlushFinalBlock();
String encryptedString = Convert.ToBase64String(tempM.ToArray());
File.WriteAllText(args[1], encryptedString);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
