Take the 2-minute tour ×
Cryptography Stack Exchange is a question and answer site for software developers, mathematicians and others interested in cryptography. It's 100% free, no registration required.

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

share|improve this question
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
add comment

1 Answer

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.

share|improve this answer
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
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.