- Edited
When I try and decode the sodium algorithm $plaintext and $plaintext echo out empty. The encoding portion is working and inserting into my db.
encode using sodium
$key = sodium_crypto_secretbox_keygen();
$nonce = random_bytes( SODIUM_CRYPTO_SECRETBOX_NONCEBYTES );
$encrypted_result = sodium_crypto_secretbox($_POST[testpublish], $nonce, $key );
$encodedpublish = base64_encode( $nonce . $encrypted_result );
$nonce1 = random_bytes( SODIUM_CRYPTO_SECRETBOX_NONCEBYTES );
$encrypted_result2 = sodium_crypto_secretbox($_POST[testAPI], $nonce1, $key );
$encodedapi = base64_encode( $nonce1 . $encrypted_result2 );
decode using sodium
$key1 = sodium_crypto_secretbox_keygen();
$decoded3 = base64_decode($row['testpublish']); //$encodedpublish is equal to $row[testpublish]
$nonce3 = mb_substr($decoded3, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, '8bit');
$encrypted_result3 = mb_substr($decoded3, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, null, '8bit');
$plaintext = sodium_crypto_secretbox_open($encrypted_result3, $nonce3, $key1);
echo $plaintext;
$key2 = sodium_crypto_secretbox_keygen();
$decoded4 = base64_decode($row['testAPI']); //$encodedapi is equal to $row[testAPI]
$nonce4 = mb_substr($decoded4, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, '8bit');
$encrypted_result4 = mb_substr($decoded4, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, null, '8bit');
$plaintext2 = sodium_crypto_secretbox_open($encrypted_result4, $nonce4, $key2);
echo $plaintext2;