0

I am attempting to encrypty/decrypt a long message in RSA using javascript (with crypto-js and jsbn libraries).

so far, to encrypty/decrypt short messages I have the following code:

function encrypt(signedCert, msg) {
    key = new RSAKey();
    m = asciiToHex(msg);
    m = new BigInteger(m, 16)
    //n and e retrieved from the digital certificate
    key.setPublic(signedCert.msg.subject.pk.n, signedCert.msg.subject.pk.e);
    var ctxt = key.doPublic(m).toString(16);
    return ctxt;
}

function decrypt(sk, ctxt) {
    key = new RSAKey();
    c = new BigInteger(ctxt, 16);
    key.setPrivate(sk.n, sk.e, sk.d);
    var ptxt = key.doPrivate(c).toString(16);
    var ptxt = hexToAscii(ptxt);
    return ptxt;
}

this works like a charm when the message is short. but, for the life of me, I can't figure out how to encrypt/decrypt when the message is long!

can anyone help? thanks :)

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
  • 1
    Please don't [cross-post](http://crypto.stackexchange.com/questions/11239/rsa-decrypt-long-messages-javascript) – CodesInChaos Oct 24 '13 at 07:37
  • @CodesInChaos please see my reply in crypto.stack –  Oct 24 '13 at 07:59
  • 1) Even if it fits several sites, you need to pick one of them 2) The crypto tags don't have many users. So you can't really talk about nobody answering before a day or so has passed. – CodesInChaos Oct 24 '13 at 08:03
  • @CodesInChaos well I thought I'd take my chances since I'm on a time limit and need help desperately –  Oct 24 '13 at 08:13

1 Answers1

1

You don't quantify "short" and "long", but I would guess that your "long" messages exceed the maximum message size for the key you are using.

Due to the construction of the RSA algorithm it is not possible to encrypt a message that is larger than the RSA key size. The key size is generally measured in bits, so divide the key size by 8 to get the maximum message that can be encrypted with a particular key. E.g. a 2048 bit key can encrypt a message up to 2048 / 8 = 256 bytes.

For a more in-depth description of this limitation you may wish to read my answer to a similar question here: Message length restriction in RSA.

Community
  • 1
  • 1
Iridium
  • 23,323
  • 6
  • 52
  • 74
  • Hi iridium! thanks for the answer, however I don't have much choice in this. this is a task for a university subject. all they specify is to come up with a way to encrypt/decrypt arbitrarily long messages. basically what happens is: if the message is short, it encrypts/decrypts fine. but if the message is long: I encrypt, get a ciphertext returned, then go to decrypt but instead of my original message I get gibberish! –  Oct 24 '13 at 07:54
  • now, based on your answer (new comment because too long) I am now thinking I might be able to split the long messages into sections no bigger than the key/8 (create an array perhaps). encrypt each section, send ciphertext as a whole, split cipher text in decryption and decrypt each section then return plain text as whole? what do you think? –  Oct 24 '13 at 07:57
  • It's somewhat less than b/8. You can't use the last bit and if you use proper padding, that costs another 40 to 70 bytes. – CodesInChaos Oct 24 '13 at 08:01
  • 2
    The usual approach, rather than encrypting the message using RSA is to generate a random key for a symmetric algorithm (e.g. AES), use the symmetric algorithm to encrypt the message, and then encrypt the symmetric key using RSA. This overcomes both the RSA message size limit, and the fact that RSA is considerably slower than most symmetric algorithms. If however you are required to encrypt long messages using *only* RSA, then splitting the message would probably suffice. – Iridium Oct 24 '13 at 08:04
  • the problem is, I'm not a heavy user of javascript. This subject is very.. frustrating. They repeatedly say it's not a programming subject, just theory, but time and time again in this assignment I'm faced with javascript issues I can't resolve because I don't deal with javascript very often! I'm not entirely sure using AES is what they're after. this task is supposed to be about asymmetrical ciphers. that being said, I'm at a loss as to how to achieve the splitting of the message etc in javascript. –  Oct 24 '13 at 08:29
  • @jonsnowed Asymmetrical ciphers just don't encrypt large messages in any real world application, full stop. However, judging by the fact that they are teaching JavaScript cryptography, they could be after something equally silly here. Splitting the message into blocks and encrypting each block separately is analogous to ECB mode for symmetric ciphers, and that could be the solution. – ntoskrnl Oct 24 '13 at 11:35
  • @ntoskrnl "equally silly" ... nice. yes splitting into blocks seems ideal but I honestly don't have the javascript knowledge to implement this. read somewhere else that encrypting the message with AES then encrypting the AES key with RSA would work except... again, javascript barrier and time constraint so I thought: encrypting the message with simpler RCA then encrypting the RC4 key with RSA would be a suitable replacement. thus far I have the encryption working. decryption is not cooperating at all. –  Oct 24 '13 at 11:54
  • @ntoskrnl my partner just asked a question about our failing decryption. here is the link (take a look if you'd perhaps have some idea about how to get it working). http://stackoverflow.com/questions/19565089/combined-rc4-rsa-encrypt-decrypt-for-long-messages-javascript –  Oct 24 '13 at 11:56
  • @ntoskrnl oops classic typo in the first comment: *encrypting the message with simpler RC4 –  Oct 24 '13 at 12:02