Java DES .NET DES

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am trying to write a program in C# that connects to a service program using Java. I have the spec but was unable to connect so using wireshark I captured the data and then using netbeans was able to create a Java program that produced the same output
as their service. I then create a small C# program but it does not work. The first block of the DES is correct but the second block is not. I have tried several different methods but where the second block is always different none of
them match the Java version. I even tried a JavaScript version and it is different than all the others. Basically the last 8 bytes is different in the below example. Anybody have any suggestions?
Last 8 bytes of Java version = 1d:b9:c9:7b:2a:0d:5a:3d:
Last 8 bytes of C# version = cb:14:3f:5c:87:a9:f4:e1:
Java code:

<div style="color:Black;background-color:White; <pre>
String passPhrase = <span style="color:#A31515; "J1v7R2e7";
String message = <span style="color:#A31515; "1:1;2:1;3:APSERVER;4:00-14-5E-87-51-4B;5:ADMIN;6:VT;7:123;8:20110421;9:101109;10:3314954025;11:2000";

String algorithm = <span style="color:#A31515; "DES";
Key key = KeyGenerator.getInstance(algorithm).generateKey();
Cipher cipher = Cipher.getInstance(algorithm);

KeySpec keySpec = <span style="color:Blue; new DESKeySpec(passPhrase.getBytes());
key = SecretKeyFactory.getInstance(algorithm).generateSecret(keySpec);

System.out.println(message);

byte[] encryptionBytes = null;
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] inputBytes = message.getBytes();
encryptionBytes = cipher.doFinal(inputBytes);

<span style="color:Blue; for(byte b : encryptionBytes)
{
System.out.format(<span style="color:#A31515; "%02x:", b);
}
System.out.println();

cipher.init(Cipher.DECRYPT_MODE, key);
byte[] recoveredBytes = cipher.doFinal(encryptionBytes);
String recovered = <span style="color:Blue; new String(recoveredBytes);
System.out.println(recovered);

[/code]

Java output:

<div style="color:Black;background-color:White; <pre>
1:1;2:1;3:APSERVER;4:00-14-5E-87-51-4B;5:ADMIN;6:VT;7:123;8:20110421;9:101109;10:3314954025;11:2000
45:95:38:1c:39:6c:c8:50:37:8e:28:d2:cf:3b:77:0d:64:b2:c4:c3:4f:40:6a:9f:08:7e:34:58:a0:63:a7:d4:7a:58:07:37:f0:dd:d8:bf:aa:a2:f9:8a:f2:8c:58:15:ee:c5:4e:89:1c:30:e9:b4:ca:97:ad:1f:2a:af:60:d7:49:2c:04:3b:c7:11:bb:63:2b:5e:d1:bd:49:71:3a:8e:e6:8d:88:b3:05:4b:e2:46:c6:3a:44:5e:ef:33:25:2d:1d:b9:c9:7b:2a:0d:5a:3d:
1:1;2:1;3:APSERVER;4:00-14-5E-87-51-4B;5:ADMIN;6:VT;7:123;8:20110421;9:101109;10:3314954025;11:2000

[/code]

C# code:

<div style="color:Black;background-color:White; <pre>
MemoryStream fout = <span style="color:Blue; new MemoryStream();

message = <span style="color:#A31515; "1:1;2:1;3:APSERVER;4:00-14-5E-87-51-4B;5:ADMIN;6:VT;7:123;8:20110421;9:101109;10:3314954025;11:2000";
<span style="color:Blue; string key = <span style="color:#A31515; "J1v7R2e7";

DES des = <span style="color:Blue; new DESCryptoServiceProvider();
des.Mode = CipherMode.ECB;
<span style="color:Green; //des.Padding = PaddingMode.PKCS7;
des.Key = ASCIIEncoding.ASCII.GetBytes(key);
des.IV = ASCIIEncoding.ASCII.GetBytes(key);
<span style="color:Blue; using (CryptoStream stream = <span style="color:Blue; new CryptoStream(fout, des.CreateEncryptor(), CryptoStreamMode.Write))
{
<span style="color:Blue; using (StreamWriter writer = <span style="color:Blue; new StreamWriter(stream))
writer.WriteLine(message);
}

msg = fout.ToArray();

StringBuilder sb = <span style="color:Blue; new StringBuilder();
<span style="color:Blue; foreach (<span style="color:Blue; byte b <span style="color:Blue; in msg)
sb.AppendFormat(<span style="color:#A31515; "{0:x2}:", b);

Console.WriteLine(message);
Console.WriteLine(sb.ToString());
[/code]

C# output:

<div style="color:Black;background-color:White; <pre>
1:1;2:1;3:APSERVER;4:00-14-5E-87-51-4B;5:ADMIN;6:VT;7:123;8:20110421;9:101109;10:3314954025;11:2000

45:95:38:1c:39:6c:c8:50:37:8e:28:d2:cf:3b:77:0d:64:b2:c4:c3:4f:40:6a:9f:08:7e:34:58:a0:63:a7:d4:7a:58:07:37:f0:dd:d8:bf:aa:a2:f9:8a:f2:8c:58:15:ee:c5:4e:89:1c:30:e9:b4:ca:97:ad:1f:2a:af:60:d7:49:2c:04:3b:c7:11:bb:63:2b:5e:d1:bd:49:71:3a:8e:e6:8d:88:b3:05:4b:e2:46:c6:3a:44:5e:ef:33:25:2d:cb:14:3f:5c:87:a9:f4:e1:

[/code]
<hr class="sig John J. Hughes II<br/>
http://www.functioninternational.com www.functioninternational.com

View the full article
 
Back
Top