2017-07-07 2 views
0

Mon cryptage C# 3DES ne correspond pas à l'API tierce que j'utilise. Y at-il quelque chose qui ne va pas avec mon code?Le cryptage 3DES ne correspond pas à l'API tierce

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); 
} 

La sortie (vous pouvez voir byteArraySharedSec est correct mais n'est pas encryptage):

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

Horodatage: 112,130,116,77,0,0,0,0, longueur:

Après encryptage 3DES: 213,60,183,244,171,116,202,205,233,17,226,8,70,9,111,43, longueur 16

API Doc a donné cet exemple:

Le cryptage 3DES utilise:

  • 192 panneton

  • mode BCE

  • PKCS7 Padding

Exemple SHARED_SECRET: 654A7EA2C9914A0B972937F4EA45FED3

  • Autre SHARED_SECRET de tableau d'octets par décodage base64. Voici la clé 3DES: {235, 158, 0, 236, 64, 54, 11, 223, 117, 224, 13, 1, 247, 189, 189, 223, 177, 120, 16, 14, 57, 20 , 64, 247}

  • horodatage Exemple (7 heures 7ème Mars 2011 GMT): 1299481200

  • Crypter 3DES (mode ECB, PKCS7 Rembourrage) l'horodatage en utilisant la clé 3DES: 128 bits (16 octets) résultat pour cet exemple {82, 191, 213, 179, 179, 73, 1, 218, 247, 68, 254, 199, 19, 159, 1, 138}

+3

Vous chiffrez probablement une valeur différente de l'exemple que vous a donné. Essayez de traiter le 'timestamp' comme une chaîne, et utilisez le codage' UTF8' pour obtenir sa représentation en octets. 'byte [] unixTimestampByte = Encoding.UTF8.GetBytes (unixTimestamp.ToString());' Voir si c'est différent ... – IronGeek

+0

Hah! C'était ça. @IronGeek Écrivez ce commentaire comme réponse et je l'accepterai. – Zammbi

+1

3DES n'a pas de clé de 192 bits, il a une clé de 168 bits en 24 octets, le LSB de l'octet ewach est ignoré. – zaph

Répondre

2

Vous chiffrez un autre valeur que l'exemple vous a donné.

Le traitement de la timestamp sous forme de chaîne, et en utilisant le codage UTF8 pour obtenir sa représentation octets devrait vous a donné le même résultat:

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

Merci.Je vais me plaindre à la 3ème partie de leur doc flou. – Zammbi