PC Review


Reply
Thread Tools Rate Thread

Decrypt string encrypted in Java

 
 
=?Utf-8?B?Q2Fyb2x5biBWbw==?=
Guest
Posts: n/a
 
      16th Sep 2005
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.
 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      16th Sep 2005
Carolyn Vo <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
=?Utf-8?B?Q2Fyb2x5biBWbw==?=
Guest
Posts: n/a
 
      16th Sep 2005
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 <(E-Mail Removed)> 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 - <(E-Mail Removed)>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
>

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      16th Sep 2005
Carolyn Vo <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
rossum
Guest
Posts: n/a
 
      16th Sep 2005
On Fri, 16 Sep 2005 10:13:07 -0700, "Carolyn Vo"
<(E-Mail Removed)> 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 <(E-Mail Removed)> 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 - <(E-Mail Removed)>
>> 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
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      17th Sep 2005
rossum <(E-Mail Removed)> wrote:
> On Fri, 16 Sep 2005 10:13:07 -0700, "Carolyn Vo"
> <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
=?Utf-8?B?Q2Fyb2x5biBWbw==?=
Guest
Posts: n/a
 
      22nd Sep 2005
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!
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I decrypt an encrypted sheet? =?Utf-8?B?QWxleA==?= Microsoft Excel Misc 3 28th Aug 2007 12:06 PM
how to decrypt an encrypted file =?Utf-8?B?SGlwcG8=?= Windows XP General 2 28th Mar 2007 01:15 AM
Decrypt encrypted files Kolin Tregaskes Windows XP Help 1 16th Jul 2006 04:37 PM
Decrypt encrypted xml file on PPC Dalli Microsoft Dot NET Compact Framework 4 22nd Dec 2004 08:08 AM
decrypt xp encrypted sudipt Windows XP Security 2 23rd Jun 2004 04:25 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:40 PM.