Skip to content

Instantly share code, notes, and snippets.

@djpnewton
Last active August 15, 2017 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djpnewton/31e37a43a3a6c1b1272e0f87368d591e to your computer and use it in GitHub Desktop.
Save djpnewton/31e37a43a3a6c1b1272e0f87368d591e to your computer and use it in GitHub Desktop.
simple method of splitting a key into a 2 of 3 set of keys
#!/usr/bin/env python
import os
import binascii
def random_number(bits):
return int(binascii.hexlify(os.urandom(bits/8)), 16)
def test(original_key, bits):
#
# we are going to try and create 3 parts of a key (key_a, key_b, key_c)
# of which any 2 of the 3 parts can be used to recreate the original key
#
# key_a: a random 'bit_size' bit number smaller then our original key
key_a = original_key
while key_a >= original_key:
key_a = random_number(bits)
# key_b: another number which when added to key_a will result in original_key
key_b = original_key - key_a
# key_c: two components:
# - xor_a, xor of the original_key and key_a
# - xor_b, xor of the original_key and key_b
xor_a = original_key ^ key_a
xor_b = original_key ^ key_b
key_c = (xor_a, xor_b)
# test
print "original key :", hex(original_key)
print "key_a :", hex(key_a)
print "key_b :", hex(key_b)
print "key_c(xor_a) :", hex(key_c[0])
print "key_c(xor_b) :", hex(key_c[1])
print "key_a+key_b :", hex(key_a + key_b)
print "key_a^key_c(xor_a):", hex(key_a ^ key_c[0])
print "key_c+key_c(xor_b):", hex(key_b ^ key_c[1])
assert(key_a + key_b == original_key)
assert(key_a ^ key_c[0] == original_key)
assert(key_b ^ key_c[1] == original_key)
if __name__ == "__main__":
#test(0x4B6150645367566B597033733676397924423F4528482B4D6251655468576D5A, 256)
test(random_number(256), 256)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment