Я пытаюсь зашифровать строку с использованием секрета в PHP.
С помощью сценария bash на Mac сверните зашифрованную строку и расшифруйте ее, чтобы использовать.
Я пытался изменить base64_encode с помощью bin2hex, но, к сожалению, это не сработало.
Все работает нормально до тех пор, пока я не заменю исходную строку из моего теста на текст в формате XML PLIST.
Кто-нибудь может указать мне правильное направление, пожалуйста?
PHP-код:
Скрипт Bash:
#!/bin/bash
mySecret='YourSecretPhrase'
# Fetch the encoded string from the PHP script via curl
curl_results=$(curl -s http://localhost/test/encode_4/index.php)
# Split the encoded string by "::" using awk
curl_results=$( echo ${curl_results} | base64 -d )
sleep 0.1;
encrypted_data_base64=$(echo "${curl_results}" | awk -F '::' '{print $1}')
iv_base64=$(echo "${curl_results}" | awk -F '::' '{print $2}')z
# Check if encrypted data or IV is empty
if [ -z "${iv_base64}" ] || [ -z "${encrypted_data_base64}" ]; then
echo "Error: Encrypted data or IV is missing."
exit 1
fi
## Decode the Base64-encoded encrypted data (this is correct)
encrypted_data=$(echo "${encrypted_data_base64}" | base64 -d)
sleep 0.1;
iv_hex=$(echo "${iv_base64}" | base64 -d)
sleep 0.1;
## Convert the secret phrase into a hex key using SHA-256 (matches what PHP is doing)
key=$(echo -n "${mySecret}" | openssl dgst -sha256 -binary | xxd -p)
## Remove any newlines or spaces from the key (very important)
key=$(echo "$key" | tr -d '\n')
sleep 0.5;
## Decrypt the data using openssl with the derived key and the IV (IV is already in hex, no need to convert)
decrypted_string=$(echo -n "${encrypted_data}" | openssl enc -aes-256-cbc -d -K "${key}" -iv "${iv_hex}" -nopad 2>/dev/null)
# Check for decryption errors
if [ $? -ne 0 ]; then
echo "Decryption failed."
exit 1
fi
decrypted_string=$(echo "${decrypted_string}" | base64 -d)
# Print the final decrypted message
echo "Decrypted String: "
echo ""
echo "${decrypted_string}"
PLIST (XML):
key_1
one
key_2
two
Подробнее здесь: https://stackoverflow.com/questions/790 ... ac-decrypt
На PHP зашифруйте строку секретным ключом, а на Mac расшифруйте ее с помощью сценария bash. ⇐ Php
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
На PHP зашифруйте строку секретным ключом, а на Mac расшифруйте ее с помощью сценария bash.
Anonymous » » в форуме Php - 0 Ответы
- 10 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Зашифруйте параметр запроса во Flutter и расшифруйте в PHP, чтобы получить данные
Anonymous » » в форуме Php - 0 Ответы
- 12 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Как я могу рассчитать хеш SHA-256 строки с секретным ключом в Android?
Anonymous » » в форуме Android - 0 Ответы
- 0 Просмотры
-
Последнее сообщение Anonymous
-