def _decrypt_masterpassword(self): """ Decrypt the encrypted masterkey """ aes = AESCipher(self.password) checksum, encrypted_master = self.config[self.config_key].split("$") try: decrypted_master = aes.decrypt(encrypted_master) except Exception: self._raise_wrongmasterpassexception() if checksum != self._derive_checksum(decrypted_master): self._raise_wrongmasterpassexception() self.decrypted_master = decrypted_master
def decrypt_text(self, enctxt): """ Decrypt the content according to BIP38 :param str wif: Encrypted key """ if not self.unlocked(): raise WalletLocked aes = AESCipher(self.masterkey) checksum, encrypted_text = enctxt.split("$") try: decrypted_text = aes.decrypt(encrypted_text) except: raise WrongMasterPasswordException if checksum != self.deriveChecksum(decrypted_text): raise WrongMasterPasswordException return decrypted_text
class Testcases(unittest.TestCase): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.aes = AESCipher("Foobar") def test_str(self): self.assertIsInstance(AESCipher.str_to_bytes("foobar"), bytes) self.assertIsInstance(AESCipher.str_to_bytes(b"foobar"), bytes) def test_key(self): self.assertEqual(base64.b64encode(self.aes.key), b"6BGBj4DZw8ItV3uoPWGWeI5VO7QIU1u0IQXN/3JqYKs=") def test_pad(self): self.assertEqual(base64.b64encode(self.aes._pad(b"123456")), b"MTIzNDU2GhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGho=") def test_unpad(self): self.assertEqual( self.aes._unpad( base64.b64decode( b"MTIzNDU2GhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGho=")), b"123456") def test_padding(self): for n in range(1, 64): name = ''.join( random.choice(string.ascii_lowercase) for _ in range(n)) self.assertEqual( self.aes._unpad(self.aes._pad(bytes(name, "utf-8"))), bytes(name, "utf-8")) def test_encdec(self): for n in range(1, 16): name = ''.join( random.choice(string.ascii_lowercase) for _ in range(64)) self.assertEqual(self.aes.decrypt(self.aes.encrypt(name)), name)