473,385 Members | 1,720 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,385 software developers and data experts.

Encryption in C# gives different result from encryption in Java

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. :) Anyway, we have existing code in Java
that encrypts a string using DES. However, when we encrypt in C# the string
is encrypted differently.

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

The code 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);

Any ideas?
Nov 17 '05 #1
1 1440
Carolyn Vo wrote:
The code in C# is:

byte[] bytIn = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncrypt );

The code in Java is:

byte[] enc = ecipher.doFinal(str.getBytes("UTF8")); Any ideas?


I'm not sure, but it looks like you are using different encodings for
your input strings. In the C# version you are encoding the string as
ASCII but in Java you are using UTF8

Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
2
by: Carolyn Vo | last post by:
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...
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...
19
by: klenwell | last post by:
Another request for comments here. I'd like to accomplish something like the scheme outlined at this page here: http://tinyurl.com/3dtcdr In a nutshell, the form uses javascript to hash...
9
by: Betikci Boris | last post by:
I get bored last night and wrote a script that uses xor for encrypt- decrypt, however it woks fine under linux 2.6.25, text and documents are ok, but fails on compressed files *.jpg, *.pdf , etc ....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.