I hope my question isn't as lazy as the title indicates. What I want to ask is how people literally integrate a PRNG with encrypting material.

I wrote a silly little script in Python a while back that took a 9 digit seed, then produced a list of 'random' numbers using a linear congruential generator. That was cool and all, but when I tried to use that with my silly internet chat program (Also in python) I failed to see what use it had. Hey, I could match letters, symbols, and numbers to the various digits as a sort of substitution cipher, but that didn't seem really secure at all. If substitution was used every-time, then no generator would be better than the other

I googled for a bit about my question, but all the results didn't seem to go into any specifics about how one would use these randomly generated numbers to encrypt anything. Anyhow, I chose to give it a shot on Cypto, and am hoping someone can either point me to an answer so this can be closed, or equally, provide an explanation.

  • $\begingroup$ Ignoring the fact that there are much better algorithms available, I guess it can’t hurt to mention the existence of Alternating Step Generators… which may (or may not) be interesting for you to learn about. $\endgroup$
    – e-sushi
    May 19, 2014 at 15:01

1 Answer 1


Well, first of, a linear congruential generator is not a secure pseudo random number generator at all, but if it's just about the basic understanding, we can ignore how you get that stream of random numbers for now.

So, but can you actually use this? The easiest approach is using the stream of random numbers in a stream cipher and just XOR each bit of the message with the according bit in the stream. For the actual implementation your RNG will provide a fixed number of bits and you need to keep track of how many bits you actually used. And then you transmit the result instead of the message itself.

On the other side you do the same.

An alternative would be to work on 8-bit symbols, and you just add the values together (modulo 256). Pseudo code:

Input: message as char array m[]
rng(): call to the RNG, returns an 8-bit integer array of length r.

j = 0
temp = rng()

for i from 0 to m.length:
  m[i] = (int(m[]) + temp[j]) % 256
  if (j > 255)
    temp = rng()

Maybe this can be made more compact, but it should work for a toy project.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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