473,320 Members | 2,035 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Encryption in Java gives different result from encryption in C#

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
2 6279
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: abcd | last post by:
kutthaense Secretary Djetvedehald H. Rumsfeld legai predicted eventual vicmadhlary in Iraq mariyu Afghmadhlaistmadhla, kaani jetvedehly after "a ljetvedehg, hard slog," mariyu vede legai pressed...
1
by: Ronald Evers | last post by:
Hey all, I want to store passwords in a postgresql database. Currently I use the MD5Password class below and I've been developing on windows. I ran into problems when running my application on...
4
by: Daniel Orner | last post by:
Hello, I'm working on a project that consists of a Java application on the desktop, and a server with Python CGI code. I'm trying to find an encryption algorithm, which would let me send...
4
by: panik | last post by:
Hi, I'm looking for something similar to Encryption. I'd like to generate URL's with a format that avoids visible ID's (e.g. http://thesite/viewlink.aspx?ID=105) Instead, I'd like to generate a...
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
1
by: Carolyn Vo | last post by:
Modification to my earlier post, in case anyone thinks I am trying to code in Java what we did in C#. We are trying to code in C# what we coded in Java, so I do need C#, not Java, help. :) ...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
3
by: AHanso | last post by:
Hey I am new to C# (My background is in Java), I am writing a C# application (that uses the Compact Framework) that communicates to a Java server. To login the server is expecting the password...
8
by: manmit.walia | last post by:
Hello Everyone, Long time ago, I posted a small problem I had about converting a VB6 program to C#. Well with the help with everyone I got it converted. But I overlooked something and don't...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.