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);