473,508 Members | 3,235 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Decrypt string encrypted in Java

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

Similar topics

1
3950
by: Benoît | last post by:
Hi, I have generated two keys : "C:>openssl req -nodes -new -x509 -keyout ben.key -out ben.crt -days 3650" I try to encrypt/decrypt a string like "JOHN" with these asymetrics keys. With the...
3
29201
by: Jimski | last post by:
Hello all, I am having a problem where I get an error message when I call FlushFinalBlock when decrypting my encrypted text. I am using the Rijndael algorithm. The error message is "Length...
4
9069
by: Hrvoje Voda | last post by:
Does anyone knows a good example of how to encrypt/decrypt a string? Hrcko
8
8154
by: Gidi | last post by:
Hi, Is there Buid-In fuction in C# that Encrypt and Decrypt strings? i have a textbox which i'm writing into file, and i want to encrypt it before writing, i'm not looking for something fancy,...
0
3349
by: Hannibal111111 | last post by:
I found this code on a site for doing string encryption/decryption. The string will encrypt fine, but I get this error when I try to decrypt. Any idea why? I posted the code below. The error...
3
8210
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
1
4816
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
2
17936
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of...
0
4565
by: FarooqRafiq | last post by:
Hi, My requirement is that a string is encrypted in VB.NET and sent to PHP, PHP decrypts the string (till here the logic is working) and then the PHP should encrtyp (where i am having problems)...
0
7228
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7128
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
7332
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,...
0
5635
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1565
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
426
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.