By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,934 Members | 1,318 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,934 IT Pros & Developers. It's quick & easy.

Encryption in Java gives different result from encryption in C#

P: n/a
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?

Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Thu, 22 Sep 2005 10:39:02 -0700, "Carolyn Vo"
<Ca*******@discussions.microsoft.com> wrote:
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
Nov 17 '05 #2

P: n/a
rossum wrote:
On Thu, 22 Sep 2005 10:39:02 -0700, "Carolyn Vo"
<Ca*******@discussions.microsoft.com> wrote:

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

--
Steve Alpert
my email Fgrir_Nycreg @ vqk.pbz is encrypted with ROT13 (www.rot13.org) and spaces

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.