ransar
ransar

Reputation: 41

how to generate 16 bytes random number in python and xor them

Im writing my own version of ssl and in order to create a master key, I need to create 2 random numbers of 16 bytes and xor them. can someone help me doing so?

Upvotes: 3

Views: 12518

Answers (3)

hiro protagonist
hiro protagonist

Reputation: 46891

i hope you do this for scientific purposes... ssl is huge. and - as always in crypto - a lot can go wrong with an implementation... good luck! but as an effort to study/improve e.g. openssl, that would be a very welcome effort!

generating random bytes:

starting from python 3.6 there is the secrets module in python. secrets.token_bytes(16) will output 16 random bytes.

from secrets import token_bytes
print(token_bytes(16))

for python <= 3.5:

import os
print(os.urandom(16))

xoring bytes

in order to xor the bytes a and b (which both have length 16)

byteorder = "little"
bytesize = 16
tmp_int = int.from_bytes(a, byteorder) ^ int.from_bytes(b, byteorder)
return tmp_int.to_bytes(16, byteorder)

Upvotes: 7

It is often operating system and computer (i.e. hardware) specific.

On Linux, you could use /dev/random (read 16 bytes from it) but read random(4) first.

Be very careful, it is a very sensitive issue and a lot of things can go silently wrong.

BTW, I don't think that rewriting SSL from scratch is reasonable (except for learning purposes).

Upvotes: 1

jimijimjim
jimijimjim

Reputation: 625

What about

int(os.urandom(16).encode('hex'),16) ^ int(os.urandom(16).encode('hex'),16)

Upvotes: 2

Related Questions