Decrypt string encrypted in Java

G

Guest

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.
 
J

Jon Skeet [C# MVP]

Carolyn Vo said:
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?
 
G

Guest

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.
 
J

Jon Skeet [C# MVP]

Carolyn Vo said:
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...
 
R

rossum

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



The ultimate truth is that there is no ultimate truth
 
J

Jon Skeet [C# MVP]

rossum said:
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.
 
G

Guest

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! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top