# RSA decrypt long messages (javascript)

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

I posted this on stackoverflow too but I figured someone here on crypto might be able to help as well.

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

function encrypt(signedCert, msg) {
key = new RSAKey();
m = asciiToHex(msg);
m = new BigInteger(m, 16)
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 :)

-
Your formatting problem was that you attempted to use LaTeX () formatting instead of code formatting (indent by four spaces). –  CodesInChaos Oct 24 '13 at 7:30
Please don't cross-post –  CodesInChaos Oct 24 '13 at 7:37
@CodesInChaos I cross-posted in the hopes that someone here might have the answer - since no one has answered or commented in stackoverflow). this is equal parts a progamming question (for stackoverflow) and a crypto question (for crypto.stack). so I figured it was appropriate for both. –  jonsnowed Oct 24 '13 at 7:48

Hi, thanks for your answer however, I don't care about the security, I am not doing this for real life, this is a task for a university subject. doPrivate and doPublic are the only methods available to us. –  jonsnowed Oct 24 '13 at 6:45
In that case, the reason it doesn't work for long messages is that the modulus is only a finite size. Thus you can only use it to encrypt messages of length up to $log_2(n)$ Once you have messages larger than $log_2(n)$, you need to break up your plaintexts in to blocks of size $log_2(n)$ and encrypt each one separately. This is horribly insecure, but will help you complete your assignment. –  Simon Johnson Oct 24 '13 at 8:12
upon some further research I did discover that the message cannot be bigger than $n$ so then I had the same thought: split the message into blocks and encrypt/decrypt separately. I have a very light knowledge of javascript though. I think this must involve creating an array but.. I'm not sure. This subject is very.. complicated. 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! –  jonsnowed Oct 24 '13 at 8:19