#reactjs #python-3.x #react-native #encryption #aes
#reactjs #python-3.x #react-native #шифрование #aes
Вопрос:
Я пытаюсь передать зашифрованные данные с клиента React Native на сервер Python. Я использую react-native-aes-crypto
модуль для того же. Сначала я пытался следовать этой статье, но застрял, когда tweetnacl-util
отказался работать с React Native. Я browserify
пытался использовать собственный crypto
модуль с JavaScript, но это неправильно расшифровывалось с помощью Python.
Я сгенерировал ключ, используя os.urandom(16)
Python, и использовал его с обеих сторон.
Теперь этот ключ также выдает ошибку
Error: exception decoding Hex string: invalid characters encountered in Hex string
promiseMethodWrapper@http://10.0.2.2:8081/index.bundle?platform=androidamp;dev=trueamp;minify=false:2244:45
На стороне клиента:
const key = 'a random key'
const encryptData = (text, key) => {
return Aes.randomKey(16).then(iv => {
return Aes.encrypt(text, key, iv).then(cipher => ({
cipher,
iv,
}))
})
}
socket.onopen = () => {
encryptData('Some random text', key).then(({cipher, iv})=>{
socket.send(JSON.stringify({'cipher': cipher, 'iv': iv}))
})
}
Серверная часть:
# this code was taken straight from the article. I don't know what to do with it
async def recieve_messages(websocket, path):
secret_key = b'a random key'
encrypted = await websocket.recv()
encrypted = json.loads(encrypted)
encrypted = encrypted["cipher"]
print(encrypted)
encrypted = encrypted.split(':')
# We decode the two bits independently
nonce = b64decode(encrypted[0])
encrypted = b64decode(encrypted[1])
# We create a SecretBox, making sure that out secret_key is in bytes
box = SecretBox(bytes(secret_key, encoding='utf8'))
decrypted = box.decrypt(encrypted, nonce).decode('utf-8')
print(decrypted)
await websocket.close(1000)
start_server = websockets.serve(recieve_messages, "localhost", 5000)
asyncio.get_event_loop().run_until_complete(start_server)
try:
print("Server is running")
asyncio.get_event_loop().run_forever()
except KeyboardInterrupt:
print("Server is stopping")
Я новичок в AES в целом, и я застрял здесь. Любая помощь будет оценена.
Комментарии:
1. Какая строка кода выдает — Ошибка: исключение при декодировании шестнадцатеричной строки: недопустимые символы, встречающиеся в шестнадцатеричной строке??
2. Это
Aes.encrypt()
приводит к этому. Когда я используюAes.pbkdf2()
, как указано в документах, это работает, но я не могу каждый раз генерировать ключ для этого проекта. Я не могу передать ключ во время обмена данными через websocket.