0
votes

My C# 3DES encryption is not matching the third party API I'm using. Is there anything wrong with my code?

static void Main(string[] args)
{
String sharedSec = "654A7EA2C9914A0B972937F4EA45FED3"; 

byte[] byteArraySharedSec = Convert.FromBase64String(sharedSec);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();

tdes.KeySize = 192;
tdes.Key = byteArraySharedSec;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;

ICryptoTransform ict = tdes.CreateEncryptor();
ICryptoTransform dct = tdes.CreateDecryptor();

Console.WriteLine("SharedSec: {0}", sharedSec);
Console.WriteLine("byteArraySharedSec: {0}\n", ToReadableByteArray(byteArraySharedSec));

long unixTimestamp = 1299481200; 

byte[] unixTimestampByte = BitConverter.GetBytes(unixTimestamp);

Console.WriteLine("Timestamp: {0}, length: {1} ", ToReadableByteArray(unixTimestampByte), unixTimestampByte.Length);

byte[] result = ict.TransformFinalBlock(unixTimestampByte, 0, unixTimestampByte.Length);

Console.WriteLine("After 3DES encrypting: {0}, length {1}\n\n  ", ToReadableByteArray(result), result.Length); 
}

static public string ToReadableByteArray(byte[] bytes)
{
    return string.Join(",", bytes);
}

The output (you can see byteArraySharedSec is correct but encrypting is not):

SharedSec: 654A7EA2C9914A0B972937F4EA45FED3

byteArraySharedSec: 235,158,0,236,64,54,11,223,117,224,13,1,247,189,189,223,177,120,16,14,57,20,64,247

Timestamp: 112,130,116,77,0,0,0,0, length: 8

After 3DES encrypting: 213,60,183,244,171,116,202,205,233,17,226,8,70,9,111,43, length 16

API Doc has given this example:

The 3DES encryption uses:

  • 192 bit key

  • ECB Mode

  • PKCS7 Padding

Example SHARED_SECRET: 654A7EA2C9914A0B972937F4EA45FED3

  • Convert SHARED_SECRET to byte array by Base64 decoding. This is the 3DES key: { 235, 158, 0, 236, 64, 54, 11, 223, 117, 224, 13, 1, 247, 189, 189, 223, 177, 120, 16, 14, 57, 20, 64, 247}

  • Example timestamp (7AM 7th March 2011 GMT): 1299481200

  • 3DES encrypt (ECB mode, PKCS7 Padding) the timestamp using the 3DES key : 128 bit (16 byte) result for this example { 82, 191, 213, 179, 179, 73, 1, 218, 247, 68, 254, 199, 19, 159, 1, 138}

1
You're probably encrypting a different value than the example gave you. Try treating the timestamp as string, and use UTF8 encoding to get its bytes representation. byte[] unixTimestampByte = Encoding.UTF8.GetBytes(unixTimestamp.ToString()); See if it's any different... - IronGeek
Hah! That was it. @IronGeek Write that comment as an answer and I'll accept it. - Daniel Ryan
3DES does not have a 192-bit key, it has a 168-bit key in 24-bytes, the LSB of ewach byte is ignored. - zaph
Good to know. I've removed "tdes.KeySize = 192" from my code. - Daniel Ryan

1 Answers

2
votes

You're encrypting a different value than the example gave you.

Treating the timestamp as string, and using the UTF8 encoding to get its bytes representation should gave you the same result:

...
byte[] unixTimestampByte = Encoding.UTF8.GetBytes(unixTimestamp.ToString());
...