From 790176964d6cdc8a3aa7361c4c10aeb71ae44f1a Mon Sep 17 00:00:00 2001 From: tracer Date: Thu, 22 Sep 2022 18:54:23 +0200 Subject: [PATCH] added corrected key import from config --- src/Controller/EncryptionController.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Controller/EncryptionController.php b/src/Controller/EncryptionController.php index 826e385..404d65f 100644 --- a/src/Controller/EncryptionController.php +++ b/src/Controller/EncryptionController.php @@ -20,11 +20,13 @@ class EncryptionController */ function safeEncrypt(string $message, string $key): string { + $binKey = sodium_hex2bin(string: $key); $nonce = random_bytes(length: SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); - $cipher = base64_encode(string: $nonce . sodium_crypto_secretbox(message: $message, nonce: $nonce, key: $key)); + $cipher = base64_encode(string: $nonce . sodium_crypto_secretbox(message: $message, nonce: $nonce, key: $binKey)); sodium_memzero(string: $message); sodium_memzero(string: $key); + sodium_memzero(string: $binKey); return $cipher; } @@ -55,7 +57,7 @@ class EncryptionController $plain = sodium_crypto_secretbox_open(ciphertext: $ciphertext, nonce: $nonce, key: $binKey); if ($plain === false) { - throw new Exception(message: 'The message was tampered with in transit'); + throw new Exception(message: ' Incorrect key.'); } sodium_memzero(string: $ciphertext); sodium_memzero(string: $key);