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 learning about crypto, purely for my own interest. As part of that I am implementing a simple 'one time pad' toolset in C.

Naturally I need to generate random numbers to create the pad, and they need to be cryptographically strong.

I know about rand(), but from what I can tell it is not strong.

Is there a library or a standard algorithm that I should be looking at to accomplish this?

Thanks, Tom

share|improve this question
So you want a stream cipher? –  CodesInChaos Apr 11 '12 at 19:39
I don't think so. I am looking for a CSPRNG that I can use in C. Just wondering if there is a standard for this. –  Tom Scrace Apr 11 '12 at 22:55
When you use a PRNG to generate a stream of bytes that you XOR with the plaintext to form ciphertext, that's a stream cipher (just like RC4). That is NOT a one-time pad. –  David Schwartz Apr 12 '12 at 9:22
Oh. Thanks David. Maybe I have misunderstood something here. So, you're saying that the defining feature of the one time pad is that the random bytes of the key are generated by some true source of randomness, as opposed to a PRNG? –  Tom Scrace Apr 12 '12 at 12:30
Essentially, yes. The difference between a one-time pad and a stream cipher is, essentially, that a one-time pad has a key that's the same size as the data encrypted while a stream cipher has a key size that is independent of the plaintext size. As soon as you hook an XOR engine up to a PRNG, it's not a one-time pad. A one-time pad has to hook the XOR engine to the key. –  David Schwartz Apr 13 '12 at 1:51
show 1 more comment

1 Answer 1

Short answer: Don't do this. The security proof for the One Time Pad requires the use of a True Random Bit Generator. The best you can find on a typical multipurpose system without dedicated hardware support for a TRBG is a Cryptographically Secure Random Bit Generator. The difference is that CSRBG is typically a deterministic algorithm that is seeded with good quality entropy the system is able to collect from various states and events. Because of this, using it for generating an OTP would in fact be no more secure than using a black box stream cipher for generating the key stream.

share|improve this answer
Thanks for this Henrick. I know that it's not going to be a truly secure one time pad, but I am only doing this for my own education, and as a bit of fun. Do you know if there is a generally accepted CSRBG C library or algorithm that I should be looking at? Thanks. –  Tom Scrace Apr 11 '12 at 19:10
I think the generally accepted way is to use the platform specific recommended CSRBG that is provided by the OS, at least if you are using modern versions of Windows, Linux or MacOSX. However, if you just want to start with something fairly simple that is easily implemented in C, you could always take a look at the RC4 algorithm. –  Henrick Hellström Apr 11 '12 at 19:44
Thanks again Henrick. I am looking into the PRNG in RC4. Do you know if /dev/random is generally considered to be cryptographically strong? –  Tom Scrace Apr 11 '12 at 21:51
I guess the quality of /dev/random/ is generally considered to be highly version dependent. –  Henrick Hellström Apr 12 '12 at 0:52
Thanks Henrick. I have ended up going with /dev/random and it seems to work pretty well. This is on OS X. –  Tom Scrace Apr 12 '12 at 2:55
show 1 more 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.