def create_LM_hashed_password(passwd): "setup LanManager password" "create LanManager hashed password" lm_pw = '\000' * 14 passwd = string.upper(passwd) if len(passwd) < 14: lm_pw = passwd + lm_pw[len(passwd) - 14:] else: lm_pw = passwd[0:14] # do hash magic_lst = [0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25] magic_str = utils.lst2str(magic_lst) res = '' dobj = des.DES(lm_pw[0:7]) res = res + dobj.encrypt(magic_str) dobj = des.DES(lm_pw[7:14]) res = res + dobj.encrypt(magic_str) # addig zeros to get 21 bytes string res = res + '\000\000\000\000\000' return res
def challenge_response(challenge, password_hash): """ChallengeResponse""" zpassword_hash = password_hash.ljust(21, '\x00') response = '' des_obj = des.DES(zpassword_hash[0:7]) response += des_obj.encrypt(challenge) des_obj = des.DES(zpassword_hash[7:14]) response += des_obj.encrypt(challenge) des_obj = des.DES(zpassword_hash[14:21]) response += des_obj.encrypt(challenge) return response
def calc_resp(keys_str, plain_text): "keys_str - hashed password" "plain_text - nonce from server" res = '' dobj = des.DES(keys_str[0:7]) res = res + dobj.encrypt(plain_text[0:8]) dobj = des.DES(keys_str[7:14]) res = res + dobj.encrypt(plain_text[0:8]) dobj = des.DES(keys_str[14:21]) res = res + dobj.encrypt(plain_text[0:8]) return res
def dataReceived(self, data): a = data.split(':') if len(a) > 1: count = a[0] command = a[1] content = a[2] #count #'change content to paw' if (count == 'DES'): d = des.DES() d.input_key(des.key) #8 paw = d.decode(content) elif (count == 'RSA'): #paw=str(rsa.n+rsa.e+rsa.d) #paw=rsa.a() paw = rsa.decrypt(content, rsa.n, rsa.e, rsa.d) #str sqlkey = infomsg.sqlmatch(command, paw) if (sqlkey == 'Y'): msg = command + ' has joined!' pri = count + ' "Usr:'******' Port:(' + content + ') Paw:' + paw + '" Logined' else: msg = sqlkey pri = count + ' "Usr:'******' Port:(' + content + ') Paw:' + paw + '" Error!!' #printf infomsg.ing = pri infomsg.state = 1 #printf #message for c in self.factory.clients: c.message(msg)
def OnButtonloginButton(self, event): times = time.strftime("%H:%M:%S", time.localtime()) try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(('localhost', 8001)) self.printftext.SetLabel('Client to Server:Connecting!' + times) #send if (self.genToggleButtonc.GetLabel() == 'DES'): paw = self.textCtrlpassword.GetValue() #DES d = des.DES() d.input_key(des.key) #8 paw = d.encode(paw) #DES sock.send('DES:' + self.textCtrluser.GetValue() + ':' + paw) elif (self.genToggleButtonc.GetLabel() == 'RSA'): paw = self.textCtrlpassword.GetValue() #paws=str(rsa.n+rsa.e) #paws=rsa.a() pawing = str(paw) paws = rsa.encrypt(pawing, rsa.n, rsa.e) sock.send('RSA:' + self.textCtrluser.GetValue() + ':' + paws) #recv self.staticTextrev.SetLabel(sock.recv(1024)) #print sock.recv(1024) sock.close() except: self.printftext.SetLabel('Client to Server:Disconnected!') #print 'Error' #error input event.Skip()
def create_LM_hashed_password_v1(passwd): "setup LanManager password" "create LanManager hashed password" # fix the password length to 14 bytes passwd = string.upper(passwd) lm_pw = passwd + '\0' * (14 - len(passwd)) lm_pw = passwd[0:14] # do hash magic_str = "KGS!@#$%" # page 57 in [MS-NLMP] res = '' dobj = des.DES(lm_pw[0:7]) res = res + dobj.encrypt(magic_str) dobj = des.DES(lm_pw[7:14]) res = res + dobj.encrypt(magic_str) return res
def test_encrypt_raw_int(): d = des.DES() # http://extranet.cryptomathic.com/descalc/index?key=0e329232ea6d0d73&iv=0000000000000000&input=0123456789ABCDEF&mode=ecb&action=Encrypt&output=31AA59FEB64386A6 # Same key and msg from the website produce website_enc_msg = 0x31AA59FEB64386A6 key = 0x0e329232ea6d0d73 int_msg = 0x0123456789ABCDEF int_enc_msg = d.encrypt(int_msg, key) assert int_enc_msg == website_enc_msg, "these should be the same!"
def test_if_encrypt_decrypt_produces_same(): d = des.DES() msg = "test" hex_msg = msg.encode() int_msg = int.from_bytes(hex_msg, byteorder="big") key = 0x0e329232ea6d0d73 int_enc_msg = d.encrypt(int_msg, key) int_dec_msg = d.encrypt(int_enc_msg, key, decrypt=True) assert int_msg == int_dec_msg, "these should be the same!"
def calc_resp(password_hash, server_challenge): """calc_resp generates the LM response given a 16-byte password hash and the challenge from the Type-2 message. @param password_hash 16-byte password hash @param server_challenge 8-byte challenge from Type-2 message returns 24-byte buffer to contain the LM response upon return """ # padding with zeros to make the hash 21 bytes long password_hash = password_hash + '\0' * (21 - len(password_hash)) res = '' dobj = des.DES(password_hash[0:7]) res = res + dobj.encrypt(server_challenge[0:8]) dobj = des.DES(password_hash[7:14]) res = res + dobj.encrypt(server_challenge[0:8]) dobj = des.DES(password_hash[14:21]) res = res + dobj.encrypt(server_challenge[0:8]) return res
def create_LM_hashed_password_v1(passwd): "setup LanManager password" "create LanManager hashed password" # if the passwd provided is already a hash, we just return the first half if re.match(r'^[\w]{32}:[\w]{32}$', passwd): return binascii.unhexlify(passwd.split(':')[0]) # fix the password length to 14 bytes passwd = string.upper(passwd) lm_pw = passwd + '\0' * (14 - len(passwd)) lm_pw = passwd[0:14] # do hash magic_str = "KGS!@#$%" # page 57 in [MS-NLMP] res = '' dobj = des.DES(lm_pw[0:7]) res = res + dobj.encrypt(magic_str) dobj = des.DES(lm_pw[7:14]) res = res + dobj.encrypt(magic_str) return res
def challenge_response(challenge, password_hash): """ ChallengeResponse( IN 8-octet Challenge, IN 16-octet PasswordHash, OUT 24-octet Response ) { Set ZPasswordHash to PasswordHash zero-padded to 21 octets DesEncrypt( Challenge, 1st 7-octets of ZPasswordHash, giving 1st 8-octets of Response ) DesEncrypt( Challenge, 2nd 7-octets of ZPasswordHash, giving 2nd 8-octets of Response ) DesEncrypt( Challenge, 3rd 7-octets of ZPasswordHash, giving 3rd 8-octets of Response ) } """ zpassword_hash = password_hash # while len(zpassword_hash)<21: # zpassword_hash+="\0" response = "" des_obj = des.DES(zpassword_hash[0:7]) response += des_obj.encrypt(challenge) des_obj = des.DES(zpassword_hash[7:14]) response += des_obj.encrypt(challenge) des_obj = des.DES(zpassword_hash[14:21]) response += des_obj.encrypt(challenge) return response
def recover_initial_des_key(k, clear, ref): base_key = des.permutation(des.permutation(k, des.PC2inv, 48), des.PC1inv, 56) for guess in range(256): candidate_k = base_key | make_missing_bit_mask(guess) # Applying the parity before encryption is not necessary, because the # bits are not used in DES. # We apply the parity after finding the correct key because the # user is likely to expect a key with parity bits. if ref == des.DES(clear, candidate_k): return apply_parity(candidate_k) return None
def artificial_des(): PLAINTEXT = 0x0102030405060708 KEY = 0x1C8529CEA240AE4F ref = des.DES(PLAINTEXT, KEY) outputs = [] for i in range(32): r = 15 print(f"Faulting at round {r}") faulted = des.DES(PLAINTEXT, KEY, r, i % 32) outputs.append(faulted) k = recover_des_key(ref, outputs) subkeys = des.keySchedule(KEY) print(f"Recovered last round key: {hex(k)}") assert subkeys[-1] == k real_k = recover_initial_des_key(k, PLAINTEXT, ref) print(f"Real key: {hex(real_k)}") assert real_k == KEY
def decrypt(message): #call the DES class toy = des.DES() #split the binary into 8-bit chunks (needed for DES class) entries = splitIntoGroups(message, 8) decryptedMessages = [] #decrypt each individual chunk for i in range(len(entries)): decryption = toy.Decryption(entries[i]) decryptedMessages.append(decryption) #concatenate the decryptions decryptedMessage = "".join(decryptedMessages) #turn from binary to ASCII decryptedMessage = text_from_bits(decryptedMessage) return decryptedMessage
def encrypt(message): #call the DES class toy = des.DES() #turn the ascii to binary binary = text_to_bits(message) #split the binary into 8-bit chunks (needed for DES class) entries = splitIntoGroups(binary, 8) encryptedEntries = [] #encrypt each individual chunk for i in range(len(entries)): encryptedMessage = toy.Encryption(entries[i]) encryptedEntries.append(encryptedMessage) #concatenate the encryptions finalEncryptedMessage = "".join(encryptedEntries) return finalEncryptedMessage
def des_hash(clear): """ DesHash( IN 7-octet Clear, OUT 8-octet Cypher ) { /* * Make Cypher an irreversibly encrypted form of Clear by * encrypting known text using Clear as the secret key. * The known text consists of the string * * KGS!@#$% */ Set StdText to "KGS!@#$%" DesEncrypt( StdText, Clear, giving Cypher ) } """ des_obj = des.DES(clear) return des_obj.encrypt(r"KGS!@#$%")
def __init__(self, methodName='runTest'): super().__init__(methodName) self.cipher = des.DES()
def __init__(self, key1, key2, key3): self.des1 = des.DES(key1) self.des2 = des.DES(key2) self.des3 = des.DES(key3)
def des_hash(clear): """DesEncrypt""" des_obj = des.DES(clear) return des_obj.encrypt('KGS!@#$%')
a = int(''.join(["%d" % x for x in sam]), 2) #converting bin list array to integer #print(sam,a) msg_bits[-1] = msg_bits[-1][:-( a * 8 )] # removing the padding bits, a - padding in bytes (a*8) bits for i in msg_bits: msgs = bitarray_to_str(i) msg += msgs return msg if __name__ == '__main__': key = "secre_tr" iv = "whatever" msg = "This is my message. There are many like it but this one is mine. " cipher = '' d = des.DES() print("\nOriginal Plaintext: ", msg) blkChain = BlockChain(key, iv, VernamEncrypt, VernamDecrypt, BlockChain.CBC) cipherblks = blkChain.encrypt(msg) #print("Ciphertext:") for blk in cipherblks: cipher = cipher + bitarray_to_str(blk) print("Encrypted Ciphertext: ", cipher) #print("Decrypted:") msg = blkChain.decrypt(cipherblks) print("Decrypted Plaintext: ", msg)