def __DecryptAndGetFactorb(encrypted_part_1_lower: bytes, encrypted_part_2: bytes, derived_half_1: bytes, derived_half_2: bytes) -> bytes: """ Decrypt and get back factorb. Args: encrypted_part_1_lower (bytes): Lower part of first encrypted part encrypted_part_2 (bytes) : Second encrypted part derived_half_1 (bytes) : First half of derived key derived_half_2 (bytes) : Second half of derived key Returns: bytes: Factorb """ # Use derived_half_2 as AES key aes_dec = AesEcbDecrypter(derived_half_2) aes_dec.AutoUnPad(False) # Decrypt the second part and get back the higher parts of seedb and encrypted half 1 decrypted_part_2 = BytesUtils.Xor(aes_dec.Decrypt(encrypted_part_2), derived_half_1[16:]) encrypted_part_1_higher = decrypted_part_2[:8] seedb_part_2 = decrypted_part_2[8:] # Decrypt the first part to get the lower part of seedb seedb_part_1 = BytesUtils.Xor(aes_dec.Decrypt(encrypted_part_1_lower + encrypted_part_1_higher), derived_half_1[:16]) # Rebuild the complete seedb seedb = seedb_part_1 + seedb_part_2 # Compute factorb from seedb return CryptoUtils.DoubleSha256(seedb)
def __EncryptSeedb(seedb: bytes, derived_half_1: bytes, derived_half_2: bytes) -> Tuple[bytes, bytes]: """ Encrypt seedb in two parts. Args: seedb (bytes) : Seedb derived_half_1 (bytes): First half of derived key derived_half_2 (bytes): Second half of derived key Returns: tuple[bytes, bytes]: Two encrypted parts """ # Use derived_half_2 as AES key aes_enc = AesEcbEncrypter(derived_half_2) aes_enc.AutoPad(False) # Encrypt the first part: seedb[0...15] xor derived_half_1[0...15] encrypted_part_1 = aes_enc.Encrypt(BytesUtils.Xor(seedb[:16], derived_half_1[:16])) # Encrypt the second part: (encrypted_part_1[8...15] + seedb[16...23])) xor derivedhalf1[16...31] encrypted_part_2 = aes_enc.Encrypt(BytesUtils.Xor(encrypted_part_1[8:] + seedb[16:], derived_half_1[16:])) return encrypted_part_1, encrypted_part_2
def __EncryptPrivateKey(priv_key_bytes: bytes, derived_half_1: bytes, derived_half_2: bytes) -> Tuple[bytes, bytes]: """ Encrypt private key in two halves. Args: priv_key_bytes (bytes): Private key derived_half_1 (bytes): First half of derived key derived_half_2 (bytes): Second half of derived key Returns: tuple[bytes, bytes]: Two encrypted halves """ # Use derived_half_2 as AES key aes_enc = AesEcbEncrypter(derived_half_2) aes_enc.AutoPad(False) # Encrypt the first half: priv_key[0...15] xor derived_half_1[0...15] encrypted_half_1 = aes_enc.Encrypt( BytesUtils.Xor(priv_key_bytes[:16], derived_half_1[:16])) # Encrypt the second half: priv_key[16...31] xor derived_half_1[16...31] encrypted_half_2 = aes_enc.Encrypt( BytesUtils.Xor(priv_key_bytes[16:], derived_half_1[16:])) return encrypted_half_1, encrypted_half_2
def __DecryptAndGetPrivKey(encrypted_half_1: bytes, encrypted_half_2: bytes, derived_half_1: bytes, derived_half_2: bytes) -> bytes: """ Decrypt and get back private key. Args: encrypted_half_1 (bytes): First encrypted half encrypted_half_2 (bytes): Second encrypted half derived_half_1 (bytes) : First half of derived key derived_half_2 (bytes) : Second half of derived key Returns: bytes: Decrypted private key """ # Use derived_half_2 as AES key aes_dec = AesEcbDecrypter(derived_half_2) aes_dec.AutoUnPad(False) # Decrypt using AES256Decrypt decrypted_half_1 = aes_dec.Decrypt(encrypted_half_1) decrypted_half_2 = aes_dec.Decrypt(encrypted_half_2) # Get the private key back by XORing bytes return BytesUtils.Xor(decrypted_half_1 + decrypted_half_2, derived_half_1)