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

Using RSA encryption with C# and Java

Hey guys,

I've been fighting with trying to use RSA to encrypt data in Java and
decrypt the data in C#. I've been able to create an X.509 certificate using
makecert and successfully access it in C# using WSE. After exporting from
certmgr as a CER, I can use it in Java using keytool.

My problem is, I simply cannot decrypt the Java encrypted messages in C#.
If I encrypt and decrypt in C#, everything is fine. One thing I noticed is
that the modulus reported by C# does not match that of Java or openssl
(which do match).

Has anyone successfully used RSA between Java and C#? If so, I'd appreciate
any tips on where I may be going wrong.

Thanks,
Bryan
Nov 15 '05 #1
14 18160
"Bryan" <re*****@thegroups.please> wrote in message
Has anyone successfully used RSA between Java and C#? If so, I'd appreciate any tips on where I may be going wrong.


It can be an endian problem. Try reversing the encrypted bytes before
feeding them to the RSACryptoServiceProvider [Array.Reverse can help you
with that].

Regards,
Pieter Philippaerts
Managed SSL/TLS: http://www.mentalis.org/go.php?sl
Nov 15 '05 #2
I tried reversing the bytes, but I still get a CryptographicException of bad
data when I try to decrypt.

I am Base64 encoding the encrypted value in Java and decoding it in C#
before decrypting. Does the modulus not matching when I print it out mean
anything? Or does .NET do something to it internally?

Thanks,
Bryan
"Pieter Philippaerts" <Pi****@nospam.mentalis.org> wrote in message
news:ut**************@TK2MSFTNGP12.phx.gbl...
"Bryan" <re*****@thegroups.please> wrote in message
Has anyone successfully used RSA between Java and C#? If so, I'd

appreciate
any tips on where I may be going wrong.


It can be an endian problem. Try reversing the encrypted bytes before
feeding them to the RSACryptoServiceProvider [Array.Reverse can help you
with that].

Regards,
Pieter Philippaerts
Managed SSL/TLS: http://www.mentalis.org/go.php?sl

Nov 15 '05 #3
"blah" <bl**@blah.blah> wrote in message
I tried reversing the bytes, but I still get a CryptographicException of bad data when I try to decrypt.


Can you post the code you're using? [both C# and Java]

Regards,
Pieter Philippaerts
Managed SSL/TLS: http://www.mentalis.org/go.php?sl
Nov 15 '05 #4
Hello:
Has anyone successfully used RSA between Java and C#? If so, I'd appreciate
any tips on where I may be going wrong.


Huummm i'm thinking in if this can be the same problem i'm having when i
test a little TLS 1.0 protocol implementation i have made, against a
jabber server made in java that support TLS, i got an exception
(Unexpected end of handshake message) after send the ClientKeyExchange
message, that uses the RSAPKCS1KeyExchangeFormatter class, but it works
ok against the openssl test server.

Any idea arround this issue ??

--
Best regards

Carlos Guzmán Álvarez
Vigo-Spain

Nov 15 '05 #5
"Carlos Guzmán Álvarez" <ca******@telefonica.net> wrote
Huummm i'm thinking in if this can be the same problem i'm having when i
test a little TLS 1.0 protocol implementation i have made, against a
jabber server made in java that support TLS, i got an exception
(Unexpected end of handshake message) after send the ClientKeyExchange
message, that uses the RSAPKCS1KeyExchangeFormatter class, but it works
ok against the openssl test server.


That has to be a problem in your TLS implementation. I've also made a TLS
implementation in C# and it works perfectly with OpenSSL as well as Java's
SSLSocket.
Note that OpenSSL has compiler switches to make the package support common
implementation bugs. It's possible that the OpenSSL server simply ignores
the bug in your implementation and that the Java server doesn't.

Regards,
Pieter Philippaerts
Managed SSL/TLS: http://www.mentalis.org/go.php?sl
Nov 15 '05 #6
Hello:

Thanks for the anwswer.
That has to be a problem in your TLS implementation. I've also made a TLS
implementation in C# and it works perfectly with OpenSSL as well as Java's
SSLSocket.
It can be too but i can't find where is the problem :),, i'm doing
anything like this to compose the ClientKeyExchange message:

// Compute pre master secret
byte[] preMasterSecret = Session.State.CreatePremasterSecret();

// Create a new RSA key
RSACryptoServiceProvider rsa = null;
rsa =
Session.State.Cipher.CreateRSA(Session.State.Serve rSettings.ServerCertificates[0]);

// Encrypt premaster_sercret
RSAPKCS1KeyExchangeFormatter formatter = new
RSAPKCS1KeyExchangeFormatter(rsa);

// Write the preMasterSecret encrypted
Write(formatter.CreateKeyExchange(preMasterSecret) );

// Create master secret
Session.State.CreateMasterSecret(preMasterSecret);

// Create keys
Session.State.CreateKeys();

// Clear resources
rsa.Clear();
Note that OpenSSL has compiler switches to make the package support common
implementation bugs. It's possible that the OpenSSL server simply ignores
the bug in your implementation and that the Java server doesn't.


I know i'm running the openssl server as:

openssl s_server -accept 4443 -key .\bin\cert\server.key -cert
..\bin\cert\server.crt -tls1 -bugs -debug -msg

And all is running fine, it finish the handshake, and sends and receives
well application data messages.


--
Best regards

Carlos Guzmán Álvarez
Vigo-Spain

Nov 15 '05 #7
Hello:
That has to be a problem in your TLS implementation. I've also made a TLS
implementation in C# and it works perfectly with OpenSSL as well as
Java's
SSLSocket.


And it was :) thanks you very much, i have solved it yet :)

--
Best regards

Carlos Guzmán Álvarez
Vigo-Spain

Nov 15 '05 #8
It turned out to be an error in the Java code. I rewrote it to use the
Cipher classes instead of using modpow as the original code did. That fixed
the problem.

Thanks for the help,
Bryan

"Pieter Philippaerts" <Pi****@nospam.mentalis.org> wrote in message
news:Ol*************@TK2MSFTNGP11.phx.gbl...
"blah" <bl**@blah.blah> wrote in message
I tried reversing the bytes, but I still get a CryptographicException of

bad
data when I try to decrypt.


Can you post the code you're using? [both C# and Java]

Regards,
Pieter Philippaerts
Managed SSL/TLS: http://www.mentalis.org/go.php?sl

Nov 15 '05 #9

Dear Brian,

I also have a problem with encryption in Java decryption in .NET.
I am using a cipher class in Java.
I am getting an error message "Bad data" in .NET.
I would realy appreciate if you show your code.
Regina

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #10
Regina Krupitskaya <re********@comcast.net> wrote:
I also have a problem with encryption in Java decryption in .NET.
I am using a cipher class in Java.
I am getting an error message "Bad data" in .NET.
I would realy appreciate if you show your code.


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #11
I used exactly the code posted by Michel Galant.
http://www.jensign.com/JavaScience/d...ypt/index.html
Thank you,
Regina

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #12
Regina Krupitskaya <re********@comcast.net> wrote:
I used exactly the code posted by Michel Galant.
http://www.jensign.com/JavaScience/d...ypt/index.html


Well, there are some problems with that code certainly, but *if* those
are set up with the correct key and IV, I would have thought it would
be okay. What exactly are you giving it? (And what's in your Java
keystore?)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #13
Jon,
I am using a JKS file other person generted.
I am sending it to you using your direct email.
If you are using "anonimous" email, please, send me something on my
email, and I answer you.
Thank you for help.
Regina

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #14
Regina Krupitskaya <re********@comcast.net> wrote:
I am using a JKS file other person generted.
I am sending it to you using your direct email.
If you are using "anonimous" email, please, send me something on my
email, and I answer you.


I've got the email, thanks - I'll try to have a look at it tomorrow.

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

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

Similar topics

5
by: Jerry | last post by:
Hi, I am writing a Java Chatroom application that will implement encryption of messages using the RSA algorithm using the BigInteger class. It uses socket connections to exchange messages. I...
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...
7
by: Dan V. | last post by:
Situation: I have to connect with my Windows 2000 server using VS.NET 2003 and C# and connect to a remote Linux server at another company's office and query their XML file. Their file may be...
12
by: Charlie | last post by:
Hi: My host will not allow me use a trusted connection or make registry setting, so I'm stuck trying find a way to hide connection string which will be stored in web.config file. If I encrypt...
1
by: padmanabhanp | last post by:
Hai, I am facing one problem in Encryption(Java). I want to get a string from the user and split that string into characters. After splitting that string into characters, i am changing that...
9
by: Ben | last post by:
Hello, I'll bet this has been asked a million times but I can't seem to find a thread that gives the clear example I need. This PC has MySQL and IIS configured and running. The MySQL database is...
1
by: Kraichek | last post by:
Hi everyone! I have Encryption code in JAVA and I need to create the same in vb, could anyone help me please: public static String encodePassword(String passowrd) { // String...
5
by: linksterman | last post by:
for my first quarter introduction to java class, i made a chat server + gui client, but for Q2, i want to make the messages encrypted.... so I thought of this: have an RSA encryption work on a...
4
by: Robert Blass | last post by:
I am looking to get my feet wet with encryption. When I say encryption program I am talking about something to get me off to a quick start. Something very simple, far less than the 40+ bit...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.