Пример #1
0
    def __init__(self, io: IO, encrypt_key: Any, nonce_or_iv: Any,
                 total_origin_len: int):
        encrypt_key = padding_key(encrypt_key, self.BLOCK_SIZE * 2)
        nonce_or_iv = padding_key(nonce_or_iv, self.BLOCK_SIZE)
        super().__init__(io, encrypt_key, nonce_or_iv, total_origin_len)

        self._crypto = SimpleCryptography(self._encrypt_key)
Пример #2
0
    def __init__(self, io: IO, encrypt_key: bytes, nonce: bytes,
                 total_origin_len: int):
        encrypt_key = padding_key(encrypt_key, self.BLOCK_SIZE * 2)
        nonce = padding_key(nonce, self.BLOCK_SIZE)
        super().__init__(io, encrypt_key, nonce, total_origin_len)

        self._crypto = ChaCha20Cryptography(self._encrypt_key,
                                            self._nonce_or_iv)
Пример #3
0
def test_padding_key():
    key = os.urandom(5)
    pad_key = padding_key(key, 10)
    assert pad_key == key + b"\xff" * 5

    pad_key = padding_key(key, 11, value=b"\x00")
    assert pad_key == key + b"\x00" * 6

    pad_key = padding_key(key, 12, value=b"")
    assert len(pad_key) == 12
Пример #4
0
def _decryptio_version1(total_head: bytes, io: IO,
                        encrypt_password: bytes) -> Optional[DecryptIO]:
    encrypt_password = padding_key(encrypt_password, 32)

    if len(total_head) < ENCRYPT_HEAD_LEN:
        return

    # Version 1
    b_mc, magic_code, nonce_or_iv, total_origin_len = parse_head(total_head)
    b_mc = aes256cbc_decrypt(b_mc, encrypt_password,
                             random_bytes(16, encrypt_password))
    total_origin_len = u8x8_to_u64(total_origin_len)

    if b_mc != BAIDUPCS_PY_CRYPTO_MAGIC_CODE:
        return

    if magic_code == SimpleEncryptIO.MAGIC_CODE:
        return SimpleDecryptIO(io, encrypt_password, b"", total_origin_len)
    elif magic_code == ChaCha20EncryptIO.MAGIC_CODE:
        return ChaCha20DecryptIO(io, encrypt_password, nonce_or_iv,
                                 total_origin_len)
    elif magic_code == AES256CBCEncryptIO.MAGIC_CODE:
        return AES256CBCDecryptIO(io, encrypt_password, nonce_or_iv,
                                  total_origin_len)
    else:
        logging.warning(f"Unknown magic_code: {magic_code}")
        return
Пример #5
0
    def __init__(self, io: IO, encrypt_key: bytes, iv: bytes,
                 total_origin_len: int):
        encrypt_key = padding_key(encrypt_key, self.BLOCK_SIZE * 2)
        iv = padding_key(iv, self.BLOCK_SIZE)
        super().__init__(io, encrypt_key, iv, total_origin_len)

        self._crypto = AES256CBCCryptography(self._encrypt_key,
                                             self._nonce_or_iv)

        self._encrypted_io_offset = 0
        self._encrypted_io_len = padding_size(total_origin_len,
                                              self.BLOCK_SIZE)
        self._encrypted_data_padded = total_origin_len != self._encrypted_io_len

        self._encrypted_cache = bytearray()
        self._decrypted_cache = bytearray()
Пример #6
0
def to_decryptio(io: IO, encrypt_key: Any):
    if not encrypt_key:
        return io

    encrypt_key = padding_key(encrypt_key, 32)

    head = io.read(ENCRYPT_HEAD_LEN)
    if len(head) != ENCRYPT_HEAD_LEN:
        io.seek(0, 0)
        return io

    b_mc, magic_code, nonce_or_iv, total_origin_len = parse_head(head)
    b_mc = aes265cbc_decrypt(b_mc, encrypt_key, random_bytes(16, encrypt_key))
    total_origin_len = u8x8_to_u64(total_origin_len)

    if b_mc != BAIDUPCS_PY_CRYPTO_MAGIC_CODE:
        io.seek(0, 0)
        return io

    if magic_code == SimpleEncryptIO.MAGIC_CODE:
        return SimpleDecryptIO(io, encrypt_key, total_origin_len)
    elif magic_code == ChaCha20EncryptIO.MAGIC_CODE:
        return ChaCha20DecryptIO(io, encrypt_key, nonce_or_iv,
                                 total_origin_len)
    elif magic_code == AES256CBCEncryptIO.MAGIC_CODE:
        return AES256CBCDecryptIO(io, encrypt_key, nonce_or_iv,
                                  total_origin_len)
    else:
        logging.warning(f"Unknown magic_code: {magic_code}")
        io.seek(0, 0)
        return io
Пример #7
0
    def __init__(self, io: IO, encrypt_key: Any, iv: Any,
                 total_origin_len: int):
        encrypt_key = padding_key(encrypt_key, self.BLOCK_SIZE * 2)
        iv = padding_key(iv, self.BLOCK_SIZE)
        super().__init__(io, encrypt_key, iv, total_origin_len)

        self._crypto = AES256CBCCryptography(self._encrypt_key,
                                             self._nonce_or_iv)

        # The offset of encrypted origin content
        self._origin_io_offset = 0
        # Origin content cache
        self._origin_cache = bytearray()

        # Total length of final encrypted content
        self._encrypted_io_len = padding_size(self._io_len, self.BLOCK_SIZE)
        # Encrypted content cache
        self._encrypted_cache = bytearray()

        self._need_data_padded = self._total_origin_len != self._encrypted_io_len
Пример #8
0
    def total_head(self) -> bytes:
        """
        Version 1:
        aes256cbc_encrypt(`BAIDUPCS_PY_CRYPTO_MAGIC_CODE 32bytes`, encrypt_password, random_bytes(16, encrypt_password)) +
        `encrypt algorithm code 1bytes` +
        `nonce or iv 16bytes` +
        `total_origin_len 8bytes`

        Version 2:
        aes256cbc_encrypt(
            `BAIDUPCS_PY_CRYPTO_MAGIC_CODE 32bytes` +
            `encrypt algorithm code 1bytes` +
            `nonce_or_iv 16bytes`
            `total_origin_len 8bytes`
        ) +
        `salt 8bytes`

        Version 3:
        aes256cbc_encrypt(
            `BAIDUPCS_PY_CRYPTO_MAGIC_CODE 32bytes` +
            `encrypt algorithm code 1bytes` +
            `salt 8bytes` + `random padding 8bytes`
            `total_origin_len 8bytes`
        ) +
        `salt 8bytes`
        """

        if self._total_head:
            return self._total_head

        assert len(self._salt) == 8

        ori_head = padding_key(
            BAIDUPCS_PY_CRYPTO_MAGIC_CODE + self.MAGIC_CODE + self._salt +
            random_sys_bytes(8) + u64_to_u8x8(self._total_origin_len),
            PADDED_ENCRYPT_HEAD_LEN,
            value=b"",
        )

        # AES256CBC Encrypt head
        self._total_head = (aes256cbc_encrypt(
            ori_head, self._encrypt_key_for_head, self._iv_for_head) +
                            self._salt_for_head)
        return self._total_head
Пример #9
0
    def __init__(self, io: IO, encrypt_key: Any, total_origin_len: int):
        encrypt_key = padding_key(encrypt_key, 32)
        super().__init__(io, encrypt_key, None, total_origin_len)

        self._crypto = SimpleCryptography(self._encrypt_key)