def pad(data, length):
    """
    PKCS #7-style padding with the given block length
    """
    assert length < 256
    assert length > 0
    padlen = length - len(data) % length
    assert padlen <= length
    assert padlen > 0
    return data + padlen * chr(padlen)