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

Decrypt string encrypted in Java

P: n/a
Hi there! I have a string that was encrypted in Java using the classes
DESKeySpec, SecretKeyFactory, SecretKey, and Cipher. It looks like using the
SecretKeyFactory puts a transparent layer on top of the bytes from our key so
when I try to decrypt using the classes in C#, I get different and invalid
data. Has anyone ever been able to decrypt in C# what was originally
decrypted in Java? I've read the samples from Frank Fang but they don't
work.
Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Carolyn Vo <Ca*******@discussions.microsoft.com> wrote:
Hi there! I have a string that was encrypted in Java using the classes
DESKeySpec, SecretKeyFactory, SecretKey, and Cipher. It looks like using the
SecretKeyFactory puts a transparent layer on top of the bytes from our key so
when I try to decrypt using the classes in C#, I get different and invalid
data. Has anyone ever been able to decrypt in C# what was originally
decrypted in Java? I've read the samples from Frank Fang but they don't
work.


Well, encryption generally works on bytes rather than strings - have
you managed to decrypt the *bytes* to the same bytes which ended up
being encrypted in Java?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2

P: n/a
I am translating the key we have into bytes. This is the code written in Java:
DESKeySpec desKeySpec = new DESKeySpec(epsKey.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ETYPE);
SecretKey key = keyFactory.generateSecret(desKeySpec);
ecipher = Cipher.getInstance(ETYPE);
dcipher = Cipher.getInstance(ETYPE);
ecipher.init(Cipher.ENCRYPT_MODE, key);
dcipher.init(Cipher.DECRYPT_MODE, key);

And this is the code to decrypt:
byte[] dec = new BASE64Decoder().decodeBuffer(encodedStr);
byte[] utf8 = dcipher.doFinal(dec);
String decryptedStr = new String(utf8, "UTF8");

What would be the C# equivalent of handling all this? There is no
equivalent in C# of the SecretKeyFactory class, unfortunately.

"Jon Skeet [C# MVP]" wrote:
Carolyn Vo <Ca*******@discussions.microsoft.com> wrote:
Hi there! I have a string that was encrypted in Java using the classes
DESKeySpec, SecretKeyFactory, SecretKey, and Cipher. It looks like using the
SecretKeyFactory puts a transparent layer on top of the bytes from our key so
when I try to decrypt using the classes in C#, I get different and invalid
data. Has anyone ever been able to decrypt in C# what was originally
decrypted in Java? I've read the samples from Frank Fang but they don't
work.


Well, encryption generally works on bytes rather than strings - have
you managed to decrypt the *bytes* to the same bytes which ended up
being encrypted in Java?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 17 '05 #3

P: n/a
Carolyn Vo <Ca*******@discussions.microsoft.com> wrote:
I am translating the key we have into bytes. This is the code written in Java:
DESKeySpec desKeySpec = new DESKeySpec(epsKey.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ETYPE);
SecretKey key = keyFactory.generateSecret(desKeySpec);
ecipher = Cipher.getInstance(ETYPE);
dcipher = Cipher.getInstance(ETYPE);
ecipher.init(Cipher.ENCRYPT_MODE, key);
dcipher.init(Cipher.DECRYPT_MODE, key);

And this is the code to decrypt:
byte[] dec = new BASE64Decoder().decodeBuffer(encodedStr);
byte[] utf8 = dcipher.doFinal(dec);
String decryptedStr = new String(utf8, "UTF8");

What would be the C# equivalent of handling all this? There is no
equivalent in C# of the SecretKeyFactory class, unfortunately.


Unfortunately I don't know the various encryption libraries terribly
well - I was hoping it would be a simple encoding issue.

Could you post the C# you've got which gives you the wrong answer
though? It could still be something simple...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4

P: n/a
On Fri, 16 Sep 2005 10:13:07 -0700, "Carolyn Vo"
<Ca*******@discussions.microsoft.com> wrote:
I am translating the key we have into bytes. This is the code written in Java:
DESKeySpec desKeySpec = new DESKeySpec(epsKey.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ETYPE);
SecretKey key = keyFactory.generateSecret(desKeySpec);
ecipher = Cipher.getInstance(ETYPE);
dcipher = Cipher.getInstance(ETYPE);
ecipher.init(Cipher.ENCRYPT_MODE, key);
dcipher.init(Cipher.DECRYPT_MODE, key);

And this is the code to decrypt:
byte[] dec = new BASE64Decoder().decodeBuffer(encodedStr);
byte[] utf8 = dcipher.doFinal(dec);
String decryptedStr = new String(utf8, "UTF8");

What would be the C# equivalent of handling all this? There is no
equivalent in C# of the SecretKeyFactory class, unfortunately.
Looking at the encrypting code, it seems to be using DES to encode the
plaintext. Looking at the decrypting code, it seems to be using
Base64 to decode. If this is the case then I am not surprised that
you are not able to decode the message. You will need a DES decoder
to decode the message, base64 will not help here.

rossum

"Jon Skeet [C# MVP]" wrote:
Carolyn Vo <Ca*******@discussions.microsoft.com> wrote:
> Hi there! I have a string that was encrypted in Java using the classes
> DESKeySpec, SecretKeyFactory, SecretKey, and Cipher. It looks like using the
> SecretKeyFactory puts a transparent layer on top of the bytes from our key so
> when I try to decrypt using the classes in C#, I get different and invalid
> data. Has anyone ever been able to decrypt in C# what was originally
> decrypted in Java? I've read the samples from Frank Fang but they don't
> work.


Well, encryption generally works on bytes rather than strings - have
you managed to decrypt the *bytes* to the same bytes which ended up
being encrypted in Java?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

The ultimate truth is that there is no ultimate truth
Nov 17 '05 #5

P: n/a
rossum <ro******@coldmail.com> wrote:
On Fri, 16 Sep 2005 10:13:07 -0700, "Carolyn Vo"
<Ca*******@discussions.microsoft.com> wrote:
I am translating the key we have into bytes. This is the code written in Java:
DESKeySpec desKeySpec = new DESKeySpec(epsKey.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ETYPE);
SecretKey key = keyFactory.generateSecret(desKeySpec);
ecipher = Cipher.getInstance(ETYPE);
dcipher = Cipher.getInstance(ETYPE);
ecipher.init(Cipher.ENCRYPT_MODE, key);
dcipher.init(Cipher.DECRYPT_MODE, key);

And this is the code to decrypt:
byte[] dec = new BASE64Decoder().decodeBuffer(encodedStr);
byte[] utf8 = dcipher.doFinal(dec);
String decryptedStr = new String(utf8, "UTF8");

What would be the C# equivalent of handling all this? There is no
equivalent in C# of the SecretKeyFactory class, unfortunately.


Looking at the encrypting code, it seems to be using DES to encode the
plaintext.


No code to encrypt the plaintext has actually been posted. I assume
that the UTF-8 encoding of the plaintext string is encoded, then the
result is base-64 encoded to get back from the encrypted binary form to
a string.

As part of the decryption, the OP needs to get back from the base-64
encoded string to the encrypted binary, which then needs decrypting
with DES to get back to the unencrypted binary, which then needs
decoding from UTF-8 to the original plaintext string.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6

P: n/a
Where epsKey is the key we use to encrypt and decrypt with, and strData is
the string to decrypt....

public static string DecryptData(string strData)
{
string strResult;
try
{
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
InitKey(DES, epsKey);
DES.Key = dec_Key;
DES.IV = dec_Key;

byte[] byteIn = Convert.FromBase64String(strData);
MemoryStream ms = new MemoryStream(byteIn);

//Create a DES decryptor from the DES instance.
ICryptoTransform desdecrypt = DES.CreateDecryptor();
//Create crypto stream set to read and do a
//DES decryption transform on incoming bytes.
CryptoStream cryptostreamDecr = new CryptoStream(ms,
desdecrypt,
CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cryptostreamDecr);
strResult = sr.ReadToEnd();
sr.Close();
}
catch (Exception exc)
{
throw exc;
}
return strResult;
}

Thanks to everyone who tried to help! :)
Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.