TripleDES decryption works, but has some scrambled text in the middle

房东的猫 提交于 2019-12-24 10:08:47

问题


I'm working on decrypting something I didn't create. The authors encrypted it from a java program, and I'm trying to decrypt it in C#. They have given me the logic behind their encryption (the integer key array, encoding type, and TripleDES), and I've been trying to figure this out.

Long story short, I have been able to decrypt most of the text, however, the middle is always scrambled.

I've researched this a lot trying to figure it out, notably, the issue of Java's negative byte numbers. This might be the reason why my decryption partially works, because half of my given java key integers are negative. However, even when I manually change the byte number to correspond to the C# equivalent, or use BitConverter, I still get the same scrambled result.

The other thing could be that I can't seem to get the UTF-8 encoding to provide better results than ASCII.

The code. I store my 24 byte integers (as strings) in the App.Config and convert them to integers in the code:

I loop through all the strings to get the desired byte array: (ipstr is the n-th string in the loop)

int dii = Convert.ToInt32(ipstr);
byte[] intBytes2 = BitConverter.GetBytes(dii);
if (BitConverter.IsLittleEndian)
    Array.Reverse(intBytes2);
byte[] result = intBytes2;

Then I use the 4th index in the byte conversation to save the fully converted byte array for the tripleDES decryption.

FinalByteArr[j - 1] = result[3];

This seems like a lot of work, but it gets my Java-originated negative string keys to C# 0-256 byte key array.

Then I get all the bytes from their output file:

string path = @"E:\file.txt";
byte[] b1 = File.ReadAllBytes(path);
TripleDESCryptoServiceProvider objDESCrypto = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider objHashMD5 = new MD5CryptoServiceProvider();
byte[] byteHash, byteBuff;
objHashMD5 = null;
objDESCrypto.Key = FinalByteArr;     
objDESCrypto.Mode = CipherMode.ECB;  
objDESCrypto.Padding = PaddingMode.PKCS7;
byteBuff = b1;  
string strDecrypted = ASCIIEncoding.ASCII.GetString(objDESCrypto.CreateDecryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));

The odd thing is, this appears to work, and my file is decrypted for most of the characters. There are obvious words and numbers in the decrypted text. But about 30% of the middle characters are jumbled up. I've tried all the ciphermodes and paddingmodes without any improvement.

I can't post the actual result, because of confidentiality, but it looks a lot like this:

PK   \2\5\1\5\1\8\2\4\2\0\?n?\Rotate??????????????%??8p??(u??Z??%??8p??(u??Z??%??8p??(u??Z\0\0\0\0\0\0\0\0\0\0\0\RotatePKRotate

Even when I use UTF-8 encoding for the last line, I get the same garbled mess in the middle, but instead of question marks I get lots of unique characters.

That's where I'm at. Any advice would be really helpful. I'm very confused, because I'd expect incorrect decryption creds to have nothing decrypted at all. But having some of the words appearing (and some not appearing) is strange.

来源:https://stackoverflow.com/questions/56712755/tripledes-decryption-works-but-has-some-scrambled-text-in-the-middle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!