Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Perceive Cryptography Algorythm In Python 2.7
- Perform a series of Diffie-Hellman exchanges to
- perceive a common data point without communicating it
- then use this data point as the modulo for encryption,
- signing, establishment of SSL-like channels via shared
- Pseudeo-random Number Generation, and exploration of
- periodic elements [with a grain of salt]
- This Software Is Licensed As Sharia Law
- """
- import hashlib
- import random
- from binascii import hexlify # For debug output
- import json
- import sys
- # If a secure random number generator is unavailable, exit with an error.
- try:
- import ssl
- random_function = ssl.RAND_bytes
- random_provider = "Python SSL"
- except (AttributeError, ImportError):
- import OpenSSL
- random_function = OpenSSL.rand.bytes
- random_provider = "OpenSSL"
- def xor(data, key):
- return bytearray(a^b for a, b in zip(*map(bytearray, [data, key])))
- def quad(a, x, b, c):
- blocksize = x.bit_length() // 8 + 1
- blocks = [a[i:i+blocksize] for i in range(0, len(a), blocksize)]
- y = bytearray()
- pseudo = pow(x, b, c)
- for i in range(len(blocks)):
- pseudo = pow(pseudo, b, c)
- y += xor(blocks[i], to_bytes(pseudo, pseudo.bit_length() // 8 + 1, byteorder="big"))
- return y
- def to_bytes(n, length, byteorder="big"):
- h = '%x' %n
- s = ('0'*(len(h) % 2) + h).zfill(length*2).decode('hex')
- return s if byteorder =='big' else s[::-1]
- class DiffieHellman(object):
- """
- A reference implementation of the Diffie-Hellman protocol.
- By default, this class uses 1536-bit MODP Group From RFC3526.
- """
- def __init__(self, privateKey=None, keyLength=2048):
- """
- Generate the public and private keys.
- """
- min_keyLength = 180
- self.diffie_base = 481 # base g from Diffie's protocol
- self.starter_base = 5
- self.starter_prime = 733
- self.__fingerprint = None
- self.__secret = random.randint(1,sys.maxint)
- self.__g_po = None
- self.__g_pO = None
- self.__g_Po = None
- self.__ephemeralKey = None
- self.__initiator = True
- self.publicKey = None
- if(keyLength < min_keyLength):
- print("Error: keyLength is too small. Setting to minimum.")
- self.keyLength = min_keyLength
- else:
- self.keyLength = keyLength
- # RFC3526 1536-bit MODP Group
- self.prime = int('0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC7'
- '4020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437'
- '4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDE'
- 'E386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598'
- 'DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED'
- '529077096966D670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF', 16)
- if privateKey is None:
- self.privateKey = self.genPrivateKey(keyLength)
- else:
- self.privateKey = privateKey
- self.publicKey = self.genPublicKey() # regenerate this after establishBase()
- def establishBase(self, challenge=None):
- if challenge is None:
- return pow(self.starter_base, self.__secret, self.starter_prime)
- else:
- self.diffie_base = pow(challenge, self.__secret, self.starter_prime)
- self.genPublicKey() # regenerate public key after changing base
- def genPrivateKey(self, bits):
- """
- Generate a private key using a secure random number generator.
- """
- return self.genRandom(bits)
- def genPublicKey(self):
- """
- Generate a public key X with g**x % p.
- """
- return pow(self.diffie_base, self.privateKey, self.prime)
- def genRandom(self, bits):
- """
- Generate a random number with the specified number of bits
- """
- _rand = 0
- _bytes = bits // 8 + 8
- while(_rand.bit_length() < bits):
- try:
- # Python 3
- _rand = int.from_bytes(random_function(_bytes), byteorder='big')
- except:
- # Python 2
- _rand = int(OpenSSL.rand.bytes(_bytes).encode('hex'), 16)
- return _rand
- def genKey(self, otherKey):
- """
- Derive the shared secret, then hash it to obtain the shared key.
- """
- self.sharedSecret = self.genSecret(self.privateKey, otherKey)
- # Convert the shared secret (int) to an array of bytes in network order
- # Otherwise hashlib can't hash it.
- try:
- _sharedSecretBytes = self.sharedSecret.to_bytes(self.sharedSecret.bit_length() // 8 + 1, byteorder="big")
- except AttributeError:
- _sharedSecretBytes = str(self.sharedSecret)
- s = hashlib.sha256()
- s.update(bytes(_sharedSecretBytes))
- self.key = s.digest()
- def checkPublicKey(self, otherKey):
- """
- Check the other party's public key to make sure it's valid.
- Since a safe prime is used, verify that the Legendre symbol == 1
- """
- if(otherKey > 2 and otherKey < self.prime - 1):
- if(pow(otherKey, (self.prime - 1)//2, self.prime) == 1):
- return True
- return False
- def genSecret(self, privateKey, otherKey):
- """
- Check to make sure the public key is valid, then combine it with the
- private key to generate a shared secret.
- """
- if(self.checkPublicKey(otherKey) == True):
- sharedSecret = pow(otherKey, privateKey, self.prime)
- return sharedSecret
- else:
- raise Exception("Invalid public key.")
- def showParams(self):
- """
- Show the parameters of the Diffie Hellman agreement.
- """
- print("Parameters:")
- print("Prime[{0}]: {1}".format(self.prime.bit_length(), self.prime))
- print("Private key[{0}]: {1}\n".format(self.privateKey.bit_length(), self.privateKey))
- print("Public key[{0}]: {1}".format(self.publicKey.bit_length(), self.publicKey))
- def showResults(self):
- """
- Show the results of a Diffie-Hellman exchange.
- """
- print("Results:")
- print("Shared secret[{0}]: {1}".format(self.sharedSecret.bit_length(), self.sharedSecret))
- print("Shared key[{0}]: {1}".format(len(self.key), hexlify(self.key)))
- def getKey(self):
- """
- Return the shared secret key
- """
- return self.key
- def get_fingerprint(self):
- if self.__fingerprint:
- return self.__fingerprint
- publicKey = self.publicKey
- return self.__make_fingerprint(publicKey)
- def __make_fingerprint(self, pub_key):
- self.__fingerprint = self.__hash(pub_key)
- return self.__fingerprint
- def __hash(self, *values):
- """Hashes the concatenation of values
- each element in values must be of type int
- """
- s = hashlib.sha256()
- for value in values:
- try:
- value_bytes = to_bytes(value, value.bit_length() // 8 + 1, byteorder="big")
- except AttributeError:
- value_bytes = str(value)
- s.update(bytes(value_bytes))
- return s.digest()
- def establishAccord(self, party_ephemeral, party_signature=None):
- if party_signature is not None:
- self.handshake(party_ephemeral, party_signature)
- def getEphemeralPublicKey(self):
- return self.__ephemeralKey.publicKey
- def establishEphemeralListener(self):
- self.__ephemeralKey = DiffieHellman()
- self.__initiator = False
- def handshake(self, party_ephemeral, party_signature):
- if self.__initiator:
- self.__ephemeralKey = DiffieHellman()
- fingerprint = self.__make_fingerprint(party_signature)
- self.__ephemeralKey.genKey(party_ephemeral)
- self.__g_po = self.__ephemeralKey.getSecret()
- self.__ephemeralKey.genKey(party_signature)
- self.__g_pO = self.__ephemeralKey.getSecret()
- self.genKey(party_ephemeral)
- self.__g_Po = self.getSecret()
- self.__make_secret()
- def getSecret(self):
- """
- Return the preHashed key
- """
- return self.sharedSecret
- def __make_secret(self):
- if self.__initiator:
- self.__secret = self.__hash(self.__g_po, self.__g_pO, self.__g_Po)
- else:
- self.__secret = self.__hash(self.__g_po,self.__g_Po, self.__g_pO)
- def get_secret(self):
- return self.__secret
- if __name__=="__main__":
- """
- Run an example Diffie-Hellman exchange
- """
- a = DiffieHellman()
- b = DiffieHellman()
- a_challenge = a.establishBase()
- b_challenge = b.establishBase()
- a.establishBase(b_challenge)
- b.establishBase(a_challenge)
- print("Established Modulo", a.diffie_base)
- b.establishEphemeralListener()
- a.genPublicKey()
- b.genPublicKey()
- a.genKey(b.publicKey)
- b.genKey(a.publicKey)
- # a.showParams()
- # a.showResults()
- # b.showParams()
- # b.showResults()
- if(a.getKey() == b.getKey()):
- print("Shared keys match.")
- print("Key:", hexlify(a.key))
- a_absoluteZero = int(a.getKey().encode('hex'), 16)
- b_absoluteZero = int(b.getKey().encode('hex'), 16)
- else:
- print("Shared secrets didn't match!")
- print("Shared secret A: ", a.genSecret(b.publicKey))
- print("Shared secret B: ", b.genSecret(a.publicKey))
- a.establishAccord(b.getEphemeralPublicKey(), b.publicKey)
- b.establishAccord(a.getEphemeralPublicKey(), a.publicKey)
- if(a.get_secret() == b.get_secret()):
- print("Shared secrets match.")
- print("Key:", hexlify(a.get_secret()))
- a_oxygen = int(a.get_secret().encode('hex'), 16)
- b_oxygen = int(b.get_secret().encode('hex'), 16)
- else:
- print("Shared secrets didn't match!")
- print("Shared secret A: ", a.get_secret())
- print("Shared secret B: ", b.get_secret())
- a.establishAccord(b.getEphemeralPublicKey(), b.publicKey)
- b.establishAccord(a.getEphemeralPublicKey(), a.publicKey)
- if(a.get_secret() == b.get_secret()):
- print("Shared secrets match.")
- print("Key:", hexlify(a.get_secret()))
- a_oxygen = int(a.get_secret().encode('hex'), 16)
- b_oxygen = int(b.get_secret().encode('hex'), 16)
- else:
- print("Shared secrets didn't match!")
- print("Shared secret A: ", a.get_secret())
- print("Shared secret B: ", b.get_secret())
- a_carbon = pow(a.diffie_base, a.privateKey, a_oxygen)
- b_carbon = pow(b.diffie_base, b.privateKey, b_oxygen) # carbon-like
- # b_carbon = a_carbon # If they share one world
- a_lithium = pow(b_carbon, a.privateKey, a_oxygen) # it oxidizes
- b_lithium = pow(a_carbon, b.privateKey, b_oxygen)
- # b_lithium = a_lithium # If they share one galaxy
- a_helium = int("Hello World".encode('hex'), 16)
- a_sodium = pow(a_absoluteZero, a_lithium, a_oxygen)
- a_potassium = pow(a_sodium, a_lithium, a_oxygen)
- a_boron = pow(a_potassium, a_helium, a_sodium)
- a_beryllium = pow(a_boron, a_lithium, a_sodium)
- b_sodium = pow(b_absoluteZero, b_lithium, b_oxygen)
- b_potassium = pow(b_sodium, b_lithium, b_oxygen)
- b_boron = pow(b_potassium, a_helium, b_sodium) # B is just a listener to the msg here exactly like Eve
- b_beryllium = pow(b_boron, b_lithium, b_sodium)
- a_fluorine = pow(a_boron, a.privateKey, a.publicKey) # Prove To The Demon That The Signs Are Different
- b_fluorine = pow(a_boron, b.privateKey, a.publicKey)
- b_magnesium = pow(a_fluorine, b.privateKey, a.publicKey)
- a_magnesium = pow(b_fluorine, a.privateKey, a.publicKey)
- if(a_magnesium == b_magnesium):
- print("Shared signature verifier A matches.")
- print("Key: {}".format(hex(a_magnesium)))
- else:
- print("Shared signature verifiers didn't match!")
- print("Shared signature A: ", a_magnesium)
- print("Shared signature B: ", b_magnesium)
- b_fluorine = pow(b_boron, b.privateKey, b.publicKey) # Prove To The Demon That The Signs Are Different
- a_fluorine = pow(b_boron, a.privateKey, b.publicKey)
- a_magnesium = pow(b_fluorine, a.privateKey, b.publicKey)
- b_magnesium = pow(a_fluorine, b.privateKey, b.publicKey)
- a_magnesium2 = pow(a_magnesium, a.publicKey, b.publicKey)
- a_magnesium3 = pow(a_magnesium, b.publicKey, a.publicKey)
- b_magnesium2 = pow(b_magnesium, a.publicKey, b.publicKey)
- b_magnesium3 = pow(b_magnesium, b.publicKey, a.publicKey)
- a_aluminum = pow(a_magnesium, a_magnesium2, a_magnesium3)
- b_aluminum = pow(b_magnesium, b_magnesium2, b_magnesium3)
- if(a_aluminum == b_aluminum):
- print("Aluminum Rectifier matches.")
- print("Key: {}".format(hex(a_aluminum)))
- else:
- print("Aluminum Rectifier didn't match!")
- print("Rectifier A: ", a_aluminum)
- print("Rectifier B: ", b_aluminum)
- a_pseudo = pow(a_aluminum, a_potassium, a_boron)
- b_pseudo = pow(b_aluminum, b_potassium, b_boron)
- for i in range(2220): # Verify pseudo-random numbers
- a_pseudo = pow(a_pseudo, a_potassium, a_boron)
- b_pseudo = pow(b_pseudo, b_potassium, b_boron)
- if(a_pseudo == b_pseudo):
- print("Pseudorandom generator {} matches.".format(i))
- print("Key: {}".format(hex(a_pseudo)))
- else:
- print("Pseudorandom generator {} didn't match!".format(i))
- print("Pseudorandom A: ", a_pseudo)
- print("Pseudorandom B: ", b_pseudo)
- if(a_magnesium == b_magnesium):
- print("Shared signature verifier B matches.")
- print("Key: {}".format(hex(b_magnesium)))
- else:
- print("Shared signature verifiers didn't match!")
- print("Shared signature A: ", a_magnesium)
- print("Shared signature B: ", b_magnesium)
- print("sodium: {}\n{}".format(a_sodium, b_sodium))
- print("beryllium: {}\n{}".format(a_beryllium, b_beryllium))
- print("lithium: {}\n{}".format(a_lithium, b_lithium))
- print("potassium: {}\n{}".format(a_potassium, b_potassium))
- print("boron: {}\n{}".format(a_boron, b_boron))
- a_sulfur = pow(a_potassium, a_oxygen, a_sodium) # We Are At The 7th Numeric Integral Here
- a_nitrogen = pow(a_sulfur, a_oxygen, a_sodium)
- b_sulfur = pow(b_potassium, b_oxygen, b_sodium) # We Are At The 7th Numeric Integral Here
- b_nitrogen = pow(a_nitrogen, b_oxygen, b_sodium)
- print("sulfur: {}\n{}".format(a_sulfur, b_sulfur))
- print("nitrogen: {}\n{}".format(a_nitrogen, b_nitrogen))
- print(pow(a_nitrogen, a_sulfur, b_nitrogen))
- print(pow(b_nitrogen, a_sulfur, a_sodium))
- exit()
- a_helium = int("Hello World 1".encode('hex'), 16)
- a_silicon = pow(a_absoluteZero, a_nitrogen, a_oxygen)
- a_phosphorous = pow(a_silicon, a_nitrogen, a_oxygen)
- a_chlorine = pow(a_phosphorous, a_helium, a_silicon)
- a_argon = pow(a_chlorine, a_nitrogen, a_silicon) # First Soft Metal
- b_hydrogen = pow(b_magnesium, 411, b_sodium) # We Are At The 7th Numeric Integral Here
- b_nitrogen = pow(b_hydrogen, 411, b_sodium)
- b_helium = int("Hello Other World 1".encode('hex'), 16)
- b_silicon = pow(b_absoluteZero, b_nitrogen, b_oxygen)
- b_phosphorous = pow(b_silicon, b_nitrogen, b_oxygen)
- b_chlorine = pow(b_phosphorous, b_helium, b_silicon)
- b_argon = pow(b_chlorine, b_nitrogen, b_silicon) # First Soft Metal
- # Eighth Numeric Integral - A & B did not agree on a carbon so none of these should match
- a_potassium = pow(a_absoluteZero, a_carbon, a_silicon)
- a_calcium = pow(a_potassium, a_carbon, a_silicon)
- a_scandium = pow(a_nitrogen, a_carbon, a_silicon)
- a_titanium = pow(a_scandium, a_carbon, a_silicon)
- a_vanadium = pow(a_argon, a_carbon, a_silicon)
- a_chromium = pow(a_vanadium, a_carbon, a_silicon)
- a_manganese = pow(a_magnesium, a_carbon, a_silicon)
- a_iron = pow(a_manganese, a_carbon, a_silicon)
- b_potassium = pow(b_absoluteZero, b_carbon, b_silicon)
- b_calcium = pow(b_potassium, b_carbon, b_silicon)
- b_scandium = pow(b_nitrogen, b_carbon, b_silicon)
- b_titanium = pow(b_scandium, b_carbon, b_silicon)
- b_vanadium = pow(b_argon, b_carbon, b_silicon)
- b_chromium = pow(b_vanadium, b_carbon, b_silicon)
- b_manganese = pow(b_magnesium, b_carbon, b_silicon)
- b_iron = pow(b_manganese, b_carbon, b_silicon)
- print("potassium: {}\n{}".format(a_potassium, b_potassium))
- print("calcium: {}\n{}".format(a_calcium, b_calcium))
- print("scandium: {}\n{}".format(a_scandium, b_scandium))
- print("titanium: {}\n{}".format(a_titanium, b_titanium))
- print("vanadium: {}\n{}".format(a_vanadium, b_vanadium))
- print("chromium: {}\n{}".format(a_chromium, b_chromium))
- print("manganese: {}\n{}".format(a_manganese, b_manganese))
- print("iron: {}\n{}".format(a_iron, b_iron))
- a_cobalt = pow(a_absoluteZero, a_carbon, a_argon)
- a_nickel = pow(a_cobalt, a_carbon, a_argon)
- b_cobalt = pow(b_absoluteZero, b_carbon, b_argon)
- b_nickel = pow(b_cobalt, b_carbon, b_argon)
- print("cobalt: {}\n{}".format(a_cobalt, b_cobalt))
- print("nickel: {}\n{}".format(a_nickel, b_nickel))
- a_copper = pow(a_manganese, a_calcium, a_scandium)
- b_copper = pow(b_manganese, b_calcium, b_scandium)
- a_zinc = pow(a_copper, a_calcium, a_scandium)
- b_zinc = pow(b_copper, b_calcium, b_scandium)
- print("copper: {}\n{}".format(a_copper, b_copper))
- print("zinc: {}\n{}".format(a_zinc, b_zinc))
- a_gallium = pow(a_zinc, b_cobalt, a_nitrogen)
- a_germanium = pow(a_gallium, b_cobalt, a_nitrogen)
- b_gallium = pow(b_zinc, a_cobalt, b_nitrogen)
- b_germanium = pow(b_gallium, a_cobalt, b_nitrogen)
- print("gallium: {}\n{}".format(a_gallium, b_gallium))
- print("germanium: {}\n{}".format(a_germanium, b_germanium))
- # Experiments Below
- b_sulfur = pow(pow(b_phosphorous, b_nitrogen, b_silicon), b_nitrogen, b_silicon)
- a_sulfur = pow(pow(a_phosphorous, a_nitrogen, a_silicon), a_nitrogen, a_silicon)
- if(a_sulfur == b_sulfur):
- print("Sulfur Generation 1 Matches.")
- print("Key: {}".format(hex(b_sulfur)))
- else:
- print("Sulfur verifiers didn't match!")
- print("Dumb Wire A: ", a_sulfur)
- print("Dumb Wire B: ", b_sulfur)
- print("silicon: {}\n{}".format(a_silicon, b_silicon))
- print("argon: {}\n{}".format(a_argon, b_argon))
- print("nitrogen: {}\n{}".format(a_nitrogen, b_nitrogen))
- print("phosphorous: {}\n{}".format(a_phosphorous, b_phosphorous))
- print("chlorine: {}\n{}".format(a_chlorine, b_chlorine))
- exit()
- b_sulfur2 = pow(b_phosphorous, b_sulfur, b_silicon)
- a_sulfur2 = pow(a_phosphorous, a_sulfur, a_silicon)
- if(a_sulfur2 == b_sulfur2):
- print("Sulfur Generation 2 Matches.")
- print("Key: {}".format(hex(b_sulfur2)))
- else:
- print("Sulfur verifiers didn't match!")
- print("Dumb Wire A: ", a_sulfur2)
- print("Dumb Wire B: ", b_sulfur2)
- b_sulfur3 = pow(b_phosphorous, b_sulfur2, b_silicon)
- a_sulfur3 = pow(a_phosphorous, a_sulfur2, a_silicon)
- if(a_sulfur3 == b_sulfur3):
- print("Sulfur Generation 3 Matches.")
- print("Key: {}".format(hex(b_sulfur3)))
- else:
- print("Damped Wire verifiers didn't match!")
- print("Dumb Wire A: ", a_sulfur3)
- print("Dumb Wire B: ", b_sulfur3)
- a_dN = pow(a_silicon, a_oxygen, a_sulfur3)
- a_dD = pow(a_silicon, a_oxygen, a_sulfur2)
- b_dN = pow(b_silicon, b_oxygen, b_sulfur3)
- b_dD = pow(b_silicon, b_oxygen, b_sulfur2)
- print(a_dN)
- print(a_dD)
- print(b_dN)
- print(b_dD)
- a_gN = pow(a_dN, a_oxygen, a_sulfur3)
- a_gD = pow(a_dD, a_oxygen, a_sulfur2)
- b_gN = pow(b_dN, b_oxygen, b_sulfur3)
- b_gD = pow(b_dD, b_oxygen, b_sulfur2)
- print(a_gN)
- print(a_gD)
- print(b_gN)
- print(b_gD)
- a_rN = pow(a_gN, a_hydrogen, a_nitrogen)
- a_rD = pow(a_gD, a_hydrogen, a_nitrogen)
- b_rN = pow(b_gN, b_hydrogen, b_nitrogen)
- b_rD = pow(b_gD, b_hydrogen, b_nitrogen)
- print(pow(a_rN, a_hydrogen, a_nitrogen))
- print(pow(a_rD, a_hydrogen, a_nitrogen))
- print(pow(b_rN, b_hydrogen, b_nitrogen))
- print(pow(b_rD, b_hydrogen, b_nitrogen))
- a_germanium = pow(a_silicon, a_hydrogen, a_sulfur)
- b_germanium = pow(b_silicon, b_hydrogen, b_sulfur)
- a_tin = pow(a_germanium, a_hydrogen, a_sulfur)
- b_tin = pow(b_germanium, b_hydrogen, b_sulfur)
- a_lead = pow(a_tin, a_hydrogen, a_sulfur)
- a_puter = pow(a_lead, a_tin, a_sulfur)
- b_lead = pow(b_tin, b_hydrogen, b_sulfur)
- b_puter = pow(b_lead, b_tin, b_sulfur)
- print(a_puter)
- print(b_puter)
- print(pow(a_puter, a_tin, a_lead))
- print(pow(b_puter, b_tin, b_lead))
- print(pow(a_germanium, a_tin, a_lead))
- print(pow(b_germanium, b_tin, b_lead)) # Equality Proves That A & B Share A Moon
- print(pow(pow(a_carbon, a_magnesium, a_nitrogen), a_magnesium, a_nitrogen))
- print(pow(pow(b_carbon, a_magnesium, b_nitrogen), a_magnesium, a_nitrogen))
Add Comment
Please, Sign In to add comment