def performanceDecrypt(name: str, encrypted: bytearray, cipher: AES, run: int): # out = "Decrypt ... " start = time.time() for i in range(0, 5000): cipher.decrypt(encrypted) elapsed = time.time() - start data["Decrypt"].append("{:.10f}".format(elapsed / 5000.0))
def decrypt_message(self, image_path, accessor_token): """Load the image and decrypt the block of text. If it fails, return an empty string rather than False. """ try: message = self.db.messages.find_one({'message':image_path}) if accessor_token in message['accessors']: result_map = base64.b64decode(message['result_map']) result_map = ast.literal_eval(result_map) message = [] if self.env == 'test': image = Image.open(image_path).getdata() else: im = StringIO(urllib2.urlopen(image_path).read()) image = Image.open(im).getdata() width, height = image.size for y in range(height): c = image.getpixel((0, y)) try: c_idx = [v[1] for v in result_map].index(c) message.append(result_map[c_idx][0]) except ValueError: raise cipher_text = AES.decrypt(''.join(message)).strip() return cipher_text else: return '' except TypeError: raise
def aes_ecb_decrypt(ctext, key, unpad=True): if HAVE_CRYPTO: cipher = AES.new(key, mode=AES.MODE_ECB) else: cipher = AES(key, mode="ECB") ptext = cipher.decrypt(ctext) return unpad_pkcs7_aes(ptext) if unpad else ptext
def decrypt_message(self, image_path, accessor_token): """Load the image and decrypt the block of text. If it fails, return an empty string rather than False. """ try: message = self.db.messages.find_one({'message': image_path}) if accessor_token in message['accessors']: result_map = base64.b64decode(message['result_map']) result_map = ast.literal_eval(result_map) message = [] if self.env == 'test': image = Image.open(image_path).getdata() else: im = StringIO(urllib2.urlopen(image_path).read()) image = Image.open(im).getdata() width, height = image.size for y in range(height): c = image.getpixel((0, y)) try: c_idx = [v[1] for v in result_map].index(c) message.append(result_map[c_idx][0]) except ValueError: raise cipher_text = AES.decrypt(''.join(message)).strip() return cipher_text else: return '' except TypeError: raise
def decrypt(cipher_text, aes_cipher_key): aes_key = rsa_decrypt(aes_cipher_key, n, d, 15) aes_key = aes_key[0:5] master_key = long(aes_key) a = AES(master_key) result = a.decrypt(cipher_text) print str(result).decode("hex")
def decrypt_msg(msg): ''' Input: Message from server Output: Decrypted message Uses each node key in a reversed order to decrypt the message ''' for key in reversed(KEYS): msg = AES.decrypt(msg, key) print(msg)
def decryptData(key, data): hashedIV = data[:16] data = data[16:] ECBCipher = AES.new(key, AES_MODE_ECB, "") iv = AES.decrypt(hashedIV) CBCCipher = AES.new(key, AES_MODE_CBC, iv) return CBCCipher.decrypt(data)
def decrypt_message(self, image_path): """ Load the image. Decrypt a block of text. Currently testing with AES """ message = '' image = Image.open(image_path).getdata() width, height = image.size for y in range(height): c = image.getpixel((0, y)) c_idx = [v[1] for v in self.char_array].index(c) message += self.char_array[c_idx][0] cipher_text = AES.decrypt(message).strip() return cipher_text
def decrypt(self, ciphertext): AES.decrypt(self, ciphertext) ciphertextBytes = stringToBytes(ciphertext) chainBytes = stringToBytes(self.IV) #CBC Mode: For each block... for x in range(len(ciphertextBytes) / 16): #Decrypt it blockBytes = ciphertextBytes[x * 16:(x * 16) + 16] blockString = bytesToString(blockBytes) decryptedBytes = stringToBytes(self.rijndael.decrypt(blockString)) #XOR with the chaining block and overwrite the input with output for y in range(16): decryptedBytes[y] ^= chainBytes[y] ciphertextBytes[(x * 16) + y] = decryptedBytes[y] #Set the next chaining block chainBytes = blockBytes self.IV = bytesToString(chainBytes) return bytesToString(ciphertextBytes)
def decrypt(self, ciphertext): AES.decrypt(self, ciphertext) ciphertextBytes = stringToBytes(ciphertext) chainBytes = stringToBytes(self.IV) #CBC Mode: For each block... for x in range(len(ciphertextBytes)/16): #Decrypt it blockBytes = ciphertextBytes[x*16 : (x*16)+16] blockString = bytesToString(blockBytes) decryptedBytes = stringToBytes(self.rijndael.decrypt(blockString)) #XOR with the chaining block and overwrite the input with output for y in range(16): decryptedBytes[y] ^= chainBytes[y] ciphertextBytes[(x*16)+y] = decryptedBytes[y] #Set the next chaining block chainBytes = blockBytes self.IV = bytesToString(chainBytes) return bytesToString(ciphertextBytes)
def recieve_keys(): ''' Input: None Output: None Recieves AES keys that the nodes use and the first nodes port ''' global NODE_PORT, KEYS sock = create_socket() sock.bind((HOST, CLIENT_PORT)) sock.listen(2) conn, addr = sock.accept() message = conn.recv(1024) #key0, key1, key2, node_port message = AES.decrypt(message, DIR_KEY).split("###") KEYS = [message[i] for i in range(3)] NODE_PORT = message[3] print(KEYS, NODE_PORT)
def decrypt_pass(cpassword): ''' AES key for cpassword decryption: http://msdn.microsoft.com/en-us/library/2c15cbf0-f086-4c74-8b70-1f2fa45dd4be%28v=PROT.13%29#endNote2 ''' key = (b'\x4e\x99\x06\xe8' b'\xfc\xb6\x6c\xc9' b'\xfa\xf4\x93\x10' b'\x62\x0f\xfe\xe8' b'\xf4\x96\xe8\x06' b'\xcc\x05\x79\x90' b'\x20\x9b\x09\xa4' b'\x33\xb6\x6c\x1b') cpass_len = len(cpassword) padded_pass = (cpassword + "=" * ((4 - cpass_len % 4) % 4)) password = b64decode(padded_pass) decrypter = AES(key, AES.MODE_CBC, '\x00' * 16) return decrypter.decrypt(password)
def aes_cbc_decrypt(ctext, key, iv=b"\x00" * 16, unpad=True): if HAVE_CRYPTO: # `AES.MODE_ECB` isn't a typo; we're implementing CBC by hand. cipher = AES.new(key, AES.MODE_ECB) ptext, ct_block = bytearray(), iv for i in range(0, len(ctext), 16): next_ct_block = ctext[i:i + 16] pt_block = repeating_key_xor(cipher.decrypt(next_ct_block), ct_block) ct_block = next_ct_block ptext.extend(pt_block) ptext = bytes(ptext) else: cipher = AES(key, mode="CBC", iv=iv) ptext = cipher.decrypt(ctext) return unpad_pkcs7_aes(ptext) if unpad else ptext
def decrypt(self, ciphertext): AES.decrypt(self, ciphertext) ciphertext = bytes(ciphertext) return bytearray(self.context.decrypt(ciphertext))
self.key = key def encrypt(self, raw): raw = pad(raw) iv = Random.new().read(AES.block_size) cipher = AES.new(self.key, AES.MODE_CBC, iv) return base64.b64encode(iv + cipher.encrypt(raw)) def decrypt(self, enc): enc = base64.b64decode(enc) iv = enc[:16] cipher = AES.new(self.key, AES.MODE_CBC, iv) return unpad(cipher.decrypt(enc[16:])) # Reading the aes256 encrypted zip with open('SourceCode.zip.aes256', 'rb') as f: EncryptedRaw = f.read() # Decrypting the aes256 data AES = AESCipher(key) DecryptedRaw = AES.decrypt(EncryptedRaw) # Saving the DecryptedRaw data to a zip file with open('SourceCode.zip', 'wb+') as f: f.write(DecryptedRaw) # Unziping SourceCode.zip with zipfile.ZipFile("SourceCode.zip", "r") as zip_ref: zip_ref.extractall("SourceCode")
print("Usage: %s cpassword " % sys.argv[0]) exit(-1) if (len(sys.argv) != 2): print("wrong number of args") usage() cpassword = sys.argv[1] cpassword += "===" if len(cpassword) % 4: cpassword = cpassword[0:-(len(cpassword) % 4)] # this is the sysvol key published by Microsoft here: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-gppref/2c15cbf0-f086-4c74-8b70-1f2fa45dd4be key = bytes.fromhex( '4e9906e8fcb66cc9faf49310620ffee8f496e806cc057990209b09a433b66c1b') key_display = ':'.join("{:02x}".format(c) for c in (key)) #uncomment for debug info #print ("\tkey={}".format(key_display)) #print ("\tcpassword={}".format(cpassword)) AES = AES.new(key, AES.MODE_CBC, ("\x00" * 16).encode("utf8")) cipher = base64.b64decode(cpassword) plain = AES.decrypt(cipher) pad_len = plain[-1] utf_16_password = plain[:-pad_len] print("password is, within quote marks:\"{}\"".format( utf_16_password.decode('utf-16le')))
def __decode(aes: AES, cipher_text: bytes) -> bytes: return aes.decrypt(cipher_text).rstrip(__PADDING)
block = xor(block, self.get_round_key(0)) return block def aes128_reverse_keyschedule(key, round): for i in range(round, 0, -1): for j in range(15, 3, -1): key[j] ^= key[j - 4] for j in range(3, -1, -1): key[j] ^= aes_sbox[key[12 + (j + 1) % 4]] ^ (0 if j else aes_Rcon[i]) return key if __name__ == '__main__': print 'Running test...' import Crypto.Cipher.AES key = ''.join(map(chr, range(16))) plain = ''.join(map(chr, range(16, 32))) assert len(key) == len(plain) == 16 cipher = AES(key) cipher2 = Crypto.Cipher.AES.new(key) assert (list(cipher.encrypt(plain)) == map(ord, cipher2.encrypt(plain))) assert (list(cipher.decrypt(plain)) == map(ord, cipher2.decrypt(plain))) for round in range(11): assert aes128_reverse_keyschedule(cipher.get_round_key(round), round) == ary(key)
mode = sys.argv[5] if (mode != 'e' and mode != 'd'): print("bad mode\n") usage() key_display = "%x" % int(key) print("\tinfile={}".format(infile)) print("\toutfile={}".format(outfile)) print("\tkey={}".format(key_display)) print("\tiv=" + ':'.join("{:02x}".format(c) for c in (iv))) print("\tmode={}".format(mode)) AES = AES.new(key.encode("utf-8"), AES.MODE_CBC, iv) print("opening {}".format(infile)) with open(infile, "rb") as f: d = f.read() if (mode == 'e'): print("\tAES ciphering ...", end="", flush=True) d = AES.encrypt(d) else: print("\tAES deciphering ...", end="", flush=True) d = AES.decrypt(d) print("done") print("writing to {}".format(outfile)) with open(outfile, "wb") as f: f.write(d)