[Tutor] What are these things urandom() returns?

Alan Gauld alan.gauld at btinternet.com
Tue Oct 10 12:22:30 CEST 2006


> source. The returned data should be unpredictable enough for
> cryptographic applications, though its exact quality depends on the
> OS implementation.

> >>> from os import urandom
> >>> urandom(10)
> '\xc0\xf0\xea\x0c\xdd\x95u at Z\x89'
> >>>
>
> ???

Its a string of bytes which you can use to encrypt data.
The simplest form of reversible encryption is the xor operation.
You simply xor the crypto string with the data to get an encrypted 
string.

To return to the data xor the data with the original cryptioo string
and you get the original data back.

Look at a 4 bit pattern to see this in action:

Data =     1010
Crypto =   0010
Data xor Crypto result = 1000

Now apply xor to result:

1000 xor 0010 = 1010, the original data...

Of course much more sophiisticated algorithms are used in
modern cryptography but the general principle applies, you
apply some random stream of crypto key to a set of data and
get an encrypted result. You then apply the same set of crypto
data (or a set that can reliable be derived from it) to the
encrypted stream to retrieve the original data. Examples
are DES and RSA.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld





More information about the Tutor mailing list