crypto/rsa problems

911 views
Skip to first unread message

Nick

unread,
Jun 21, 2013, 1:26:20 PM6/21/13
to golan...@googlegroups.com
Hello,
I'm trying to port some crypto code over to communicate with a closed-source server that has the private key to decrypt the data I send (I only have the public key).
The code should encrypt a random session key with the public key of the server, but the server seems to reject it (for some reason I can't find out).
I have it working in C# and JavaScript though. Are there any differences between the code in C#, JavaScript and Go?

C#:
var rsa = new System.Security.Cryptography.RSACryptoServiceProvider();
rsa.ImportParameters(keyParameters);
var result = rsa.Encrypt(sessionKey, true);

JavaScript (on NodeJS using the 'ursa' library):
var result = publicKey.encrypt(sessionKey);
(where the publicKey is already a parsed ursa Public Key)

And Go: 
result, err := rsa.EncryptOAEP(crc32.New(crc32.IEEETable), rand.Reader, publicKey, sessionKey, nil)

They all seem equivalent, but as said before, the Go one is rejected. I have verified that all three keys are the same.

I'm using Go 1.1.1 on Windows 7 64bit.


agl

unread,
Jun 21, 2013, 4:49:10 PM6/21/13
to golan...@googlegroups.com

On Friday, June 21, 2013 1:26:20 PM UTC-4, Nick wrote:
Hello,
I'm trying to port some crypto code over to communicate with a closed-source server that has the private key to decrypt the data I send (I only have the public key).
The code should encrypt a random session key with the public key of the server, but the server seems to reject it (for some reason I can't find out).
I have it working in C# and JavaScript though. Are there any differences between the code in C#, JavaScript and Go?

C#:
var rsa = new System.Security.Cryptography.RSACryptoServiceProvider();
rsa.ImportParameters(keyParameters);
var result = rsa.Encrypt(sessionKey, true);

JavaScript (on NodeJS using the 'ursa' library):
var result = publicKey.encrypt(sessionKey);
(where the publicKey is already a parsed ursa Public Key)

And Go: 
result, err := rsa.EncryptOAEP(crc32.New(crc32.IEEETable), rand.Reader, publicKey, sessionKey, nil)

OAEP is the more modern RSA padding. It's likely that you want to call http://golang.org/pkg/crypto/rsa/#EncryptPKCS1v15


Cheers

AGL

Nick

unread,
Jun 21, 2013, 4:57:20 PM6/21/13
to golan...@googlegroups.com
Unfortunately, that doesn't seem to work either. Also, the last parameter in the C# encrypt call enables OAEP too (MSDN docs). For the NodeJS library, the default padding which is mentioned in the docs is RSA_PKCS1_OAEP_PADDING.

Michael Gehring

unread,
Jun 21, 2013, 6:32:04 PM6/21/13
to Nick, golan...@googlegroups.com
On Fri, Jun 21, 2013 at 10:26:20AM -0700, Nick wrote:
> And Go:
> result, err := rsa.EncryptOAEP(crc32.New(crc32.IEEETable), rand.Reader,
> publicKey, sessionKey, nil)

Try sha-1 (crypto/sha1) as hash function. crc32 ist almost certainly wrong.

Nick

unread,
Jun 22, 2013, 2:38:18 AM6/22/13
to golan...@googlegroups.com, Nick, m...@ebfe.org
Many thanks, that was it. 

Nick

unread,
Jun 22, 2013, 1:30:24 PM6/22/13
to golan...@googlegroups.com, Nick, m...@ebfe.org
I have a question though. Why is it that the Go API allows to pass a custom hash function while other libraries in other languages don't? Is this a special feature?
Message has been deleted

Frithjof Schulze

unread,
Jun 23, 2013, 3:16:34 AM6/23/13
to golan...@googlegroups.com, Nick, m...@ebfe.org
Please ignore what I said, which was just wrong.

Frithjof Schulze

unread,
Jun 23, 2013, 4:17:16 AM6/23/13
to golan...@googlegroups.com, Nick, m...@ebfe.org
To expand on what was wrong: I talked about the second argument, which is actually used for blinding. The hash function is needed for the OAEP padding scheme. 
Still the same comments apply. OAEP needs one (or two) cryptographic hash function, and there is no good reason to hardcode sha-1, especially as people are moving away to other hash functions like sha-256.

If you google around you can actually find posts of people, who are trying to use sha-256 in C#.


- Frithjof
Reply all
Reply to author
Forward
0 new messages