Encryption in Java gives different result from encryption in C#

G

Guest

We have code in Java that encrypts a string using DES. However, when we
encrypt in C# the string is encrypted differently. The code to encrypt in
Java is:

DESKeySpec desKeySpec = new DESKeySpec(epsKey.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ETYPE);
SecretKey key = keyFactory.generateSecret(desKeySpec);
ecipher = Cipher.getInstance(ETYPE);
ecipher.init(Cipher.ENCRYPT_MODE, key);
byte[] enc = ecipher.doFinal(str.getBytes("UTF8"));
encryptedString = new BASE64Encoder().encode(enc);

The code in C# is:
DES desAlg = new DESCryptoServiceProvider();
InitKey(epsKey);
desAlg.Key = dec_Key;
desAlg.IV = dec_Key;
byte[] bytIn = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncrypt);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
ICryptoTransform encrypto = desAlg.CreateEncryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
ms.Close();
cs.Close();
byte[] resArray = ms.ToArray();
encryptedString = System.Convert.ToBase64String(resArray);

Any ideas?
 
R

rossum

We have code in Java that encrypts a string using DES. However, when we
encrypt in C# the string is encrypted differently. The code to encrypt in
Java is:

DESKeySpec desKeySpec = new DESKeySpec(epsKey.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ETYPE);
SecretKey key = keyFactory.generateSecret(desKeySpec);
ecipher = Cipher.getInstance(ETYPE);
ecipher.init(Cipher.ENCRYPT_MODE, key);
byte[] enc = ecipher.doFinal(str.getBytes("UTF8"));
encryptedString = new BASE64Encoder().encode(enc);

The code in C# is:
DES desAlg = new DESCryptoServiceProvider();
InitKey(epsKey);
desAlg.Key = dec_Key;
desAlg.IV = dec_Key;
byte[] bytIn = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncrypt);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
ICryptoTransform encrypto = desAlg.CreateEncryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
ms.Close();
cs.Close();
byte[] resArray = ms.ToArray();
encryptedString = System.Convert.ToBase64String(resArray);

Any ideas?

1 Is Java UTF8 exactly the same as C# ASCII?

2 C# tends to use \r, or \r\n, for the end of a line. I suspect that
Java might use \n. Have you tried it with a short plaintext, less
than one line?

3 Encode a series of different plaintexts, each one character long.
See which match and which fail.

4 How does each system deal with short blocks? Try encoding text
which is an exact multiple of the block size. Try again, with a
single extra character added to the plaintext.

rossum



The ultimate truth is that there is no ultimate truth
 
S

Steve Alpert

rossum said:
We have code in Java that encrypts a string using DES. However, when we
encrypt in C# the string is encrypted differently. The code to encrypt in
Java is:

DESKeySpec desKeySpec = new DESKeySpec(epsKey.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ETYPE);
SecretKey key = keyFactory.generateSecret(desKeySpec);
ecipher = Cipher.getInstance(ETYPE);
ecipher.init(Cipher.ENCRYPT_MODE, key);
byte[] enc = ecipher.doFinal(str.getBytes("UTF8"));
encryptedString = new BASE64Encoder().encode(enc);

The code in C# is:
DES desAlg = new DESCryptoServiceProvider();
InitKey(epsKey);
desAlg.Key = dec_Key;
desAlg.IV = dec_Key;
byte[] bytIn = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncrypt);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
ICryptoTransform encrypto = desAlg.CreateEncryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
ms.Close();
cs.Close();
byte[] resArray = ms.ToArray();
encryptedString = System.Convert.ToBase64String(resArray);

Any ideas?


1 Is Java UTF8 exactly the same as C# ASCII?

2 C# tends to use \r, or \r\n, for the end of a line. I suspect that
Java might use \n. Have you tried it with a short plaintext, less
than one line?

3 Encode a series of different plaintexts, each one character long.
See which match and which fail.

4 How does each system deal with short blocks? Try encoding text
which is an exact multiple of the block size. Try again, with a
single extra character added to the plaintext.

rossum

My notes from an unrelated case which may have bearing here...

This is a known issue with the Java side of the encoding/decoding. More
specifically it is a padding problem with java. This should be indicated by the
error message. While you are already padding the string with spaces to ensure
that it fits the 256 bit block encryption requirements, it appears you also need
to do is add a further 16 characters to the end of the already padded string.
These characters have to be the 16th character on the ascii table known as the
'Data Link Escape' character.
While I have yet to find any java documentation that mentions this very
important detail this does appear to be the resolution for the Java/Caché
encoding with AES.

HTH,
/steveA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top