Пример #1
0
def encrypt(value, key):
    # skip32 generates 4 bytes output from 4 bytes input
    _encrypt = True
    skip32.skip32(key, value, _encrypt)

    # raw bytes aren't suitable for a Code 128 barcode though,
    # so convert it to base58 encoding
    # which is just some alphanumeric and numeric chars and is
    # designed to be vaguely human.  this takes our 4 bytes and turns it into 11ish bytes
    encrypted_value = base64.encodebytes(value).decode('ascii')

    # important note: because we are not an even multiple of 3 bytes, base64 needs to pad
    # the resulting string with equals signs.  we can strip them out knowing that our length is 4 bytes
    # IF YOU CHANGE THE LENGTH OF THE ENCRYPTED DATA FROM 4 BYTES, THIS WILL NO LONGER WORK.
    encrypted_value = encrypted_value.replace('==\n', '')

    return encrypted_value
Пример #2
0
def decrypt(value, key):
    # raw bytes aren't suitable for a Code 128 barcode though,
    # so convert it to base64 encoding
    # which is just some alphanumeric and numeric chars and is
    # designed to be vaguely human.  this takes our 4 bytes and turns it into 6ish bytes

    # important note: because we are not an even multiple of 3 bytes, base64 needs to pad
    # the resulting string with equals signs.  we can strip them out knowing that our length is 4 bytes
    # IF YOU CHANGE THE LENGTH OF THE ENCRYPTED DATA FROM 4 BYTES, THIS WILL NO LONGER WORK.
    value += '==\n'

    decoded = base64.decodebytes(value.encode('ascii'))

    # skip32 generates 4 bytes output from 4 bytes input
    _encrypt = False
    decrytped = bytearray(decoded)
    skip32.skip32(key, decrytped, _encrypt)

    return decrytped