0
$\begingroup$

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)
    //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 :)

$\endgroup$
2
  • 3
    $\begingroup$ Please don't cross-post $\endgroup$ Oct 24, 2013 at 7:37
  • $\begingroup$ @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. $\endgroup$
    – user8339
    Oct 24, 2013 at 7:48

1 Answer 1

4
$\begingroup$

I don't know much about the library you're using but it looks like you're trying to encrypt/decrypt the cipher-text using RSA directly.

Using RSA in this way isn't secure unless extreme care is taken.

This isn't the correct way to use RSA. Typically, what you do is use RSA to encrypt a secret key. This secret key is then used with a conventional symmetric cipher, such as AES, to encrypt your payload. Then your payload can be any length you like.

There are standards such as PCKS#1 v2.0 that deal with how to do this securely.

$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$
    – user8339
    Oct 24, 2013 at 6:45
  • 1
    $\begingroup$ 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. $\endgroup$ Oct 24, 2013 at 8:12
  • $\begingroup$ 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! $\endgroup$
    – user8339
    Oct 24, 2013 at 8:19