2

So, in C# I have the following code:

public static void Main (string[] args)
{
    publicKeyXml = "<Modulus>mFCubVhPGG+euHuVQbNObqod/Ji0kRe+oh2OCFR7aV09xYiOklqFQ8jgIgAHvyCcM1JowqfFeJ5jV9up0Lh0eIiv3FPRu14aQS35kMdBLMebSW2DNBkfVsOF3l498WWQS9/THIqIaxbqwRDUxba5btBLTN0/A2y6WWiXl05Xu1c=</Modulus><Exponent>AQAB</Exponent>";
    RSACryptoServiceProvider rSACryptoServiceProvider = new RSACryptoServiceProvider ();
    rSACryptoServiceProvider.FromXmlString (publicKeyXml);
    Console.WriteLine(Convert.ToBase64String (rSACryptoServiceProvider.Encrypt (Encoding.ASCII.GetBytes(args[0]), false)));
}

Which when I use to encrypt a message, it works just fine on a remote server (to which I have no source code for). However, when trying to do a similar thing in Python with PyCrypto, the remote server cannot decrypt.

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5

KEY = RSA.importKey(open('login.key').read()) # Converted to standard format
KEY_CIPHER = PKCS1_v1_5.new(KEY)

testmsg = KEY_CIPHER.encrypt("test msg").encode('base64').replace('\n', '')
# send testmsg down a socket
# Response: {"info":"java.lang.IllegalArgumentException: Could not decrypt.","msg":"Fail"}

Any thoughts as to why this would be the case?

  • What kind of format does login.key have? The signature schemes look identical *at first glance*... The format (and possibly the value) of the keys may differ. – Maarten Bodewes Aug 07 '13 at 10:58
  • I tried two different methods, in this case. One, I found a tool that uses BouncyCastle to convert the XML Pubkey to XML. Another solution is I used PyCrypto's construct with q and e. Neither of these worked. – user2657699 Aug 07 '13 at 15:19
  • Upon further investigation, I stumbled upon http://stackoverflow.com/questions/3260319/interoperability-between-rsacryptoserviceprovider-and-openssl stating the differences of why my solution isn't working. This is however just signing, not encrypting, but maybe it leads to the same incompatibility? – user2657699 Aug 07 '13 at 15:20
  • Erm, in the first comment I meant to say "to PEM." Too late to edit it now. – user2657699 Aug 07 '13 at 15:26
  • What information do you get from the server? – Maarten Bodewes Aug 07 '13 at 15:39
  • Just that it's unable to decrypt the data I sent it. {"info":"java.lang.IllegalArgumentException: Could not decrypt.","msg":"Fail"} – user2657699 Aug 07 '13 at 15:42

2 Answers2

1

OK, in my case it was rather odd. The server side was expecting my stuff backwards. To solve, I simply did this:

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5

KEY = RSA.importKey(open('login.key').read()) # Converted to standard format
KEY_CIPHER = PKCS1_v1_5.new(KEY)

testmsg = KEY_CIPHER.encrypt("test msg").encode('base64').replace('\n', '')
testmsg = "".join(reversed([testmsg[i:i+2] for i in range(0, len(testmsg), 2)]))
0
Modulus = "mFCubVhPGG+euHuVQbNObqod/Ji0kRe+oh2OCFR7aV09xYiOklqFQ8jgIgAHvyCcM1JowqfFeJ5jV9up0Lh0eIiv3FPRu14aQS35kMdBLMebSW2DNBkfVsOF3l498WWQS9/THIqIaxbqwRDUxba5btBLTN0/A2y6WWiXl05Xu1c="
Exponent = "AQAB"

mod_raw = b64decode(Modulus)
exp_raw = b64decode(Exponent)
mod = int(mod_raw.encode('hex'), 16)
exp = int(exp_raw.encode('hex'), 16)
seq = asn1.DerSequence()
seq.append(mod)
seq.append(exp)
der = seq.encode()
keyPub = RSA.importKey(der)
print base64.b64encode(keyPub.encrypt('test msg', 0)[0])
屎克螂
  • 21
  • 3