1

It sounds an easy task but I'm still learning. I have a textarea where I put data and I want to post it to a php script to decrypt it.

Here is my HTML:

 <html>
    <form action="php.php" method="post">
    <textarea id="input" name="input" rows="4" cols="50"</textarea>
    <input type="submit" name="decrypt" class="button" value="decrypt" />
    </form>
    
    <p><?php echo $decrypt; ?></p>
    
    </html>

PHP Code:

<?php
function Decrypt($ciphertext)
{
    $key = 1;
    $c = base64_decode($ciphertext);
    $ivlen = openssl_cipher_iv_length($cipher = "AES-128-CBC");
    $iv = substr($c, 0, $ivlen);
    $hmac = substr($c, $ivlen, $sha2len = 32);
    $ciphertext_raw = substr($c, $ivlen + $sha2len);
    $original_plaintext = openssl_decrypt($ciphertext_raw, $cipher, $key, OPENSSL_RAW_DATA, $iv);
    $calcmac = hash_hmac('sha256', $ciphertext_raw, $key, true);
    if (hash_equals($hmac, $calcmac)) {
        return $original_plaintext;
    }
}
   
if (!empty($_POST)) {
    $decrypt = Decrypt($_POST['decrypt']);
    print_r($_POST);
}

Error I'm receiving "Warning: Undefined array key "decrypt" on line20"

Expected behavior: Output the original plaintext.

If you would like to test a valid encrypted line please use:

W9aMvbRmmN/52Kmv1rr9i59ecKu2KYIhrL+Mj+dD8VE3BtwUiFIEBqrRc/e3aw8li2GKKu4B3FVyx/dkRnNnmw==
thebest man
  • 109
  • 5
  • first, include your textarea input inside your form. second, do a print_r($_POST); in your php script and report back what you get – DCR Jan 06 '22 at 23:21
  • Ok I updated the code please check, the result of print_r($_POST); isWarning: openssl_decrypt(): IV passed is only 5 bytes long, cipher expects an IV of precisely 16 bytes, padding with \0 in C:\laragon\www\test\testhtml\php.php on line 11 `Array ( [input] => W9aMvbRmmN/52Kmv1rr9i59ecKu2KYIhrL+Mj+dD8VE3BtwUiFIEBqrRc/e3aw8li2GKKu4B3FVyx/dkRnNnmw== [decrypt] => decrypt )` – thebest man Jan 06 '22 at 23:27
  • The ` – Obsidian Age Jan 06 '22 at 23:30
  • @ObsidianAge like this ` `? – thebest man Jan 06 '22 at 23:33
  • `$decrypt = Decrypt($_POST['input']);` Result=>`Array ( [input] => W9aMvbRmmN/52Kmv1rr9i59ecKu2KYIhrL+Mj+dD8VE3BtwUiFIEBqrRc/e3aw8li2GKKu4B3FVyx/dkRnNnmw== [decrypt] => decrypt )` – thebest man Jan 06 '22 at 23:35

1 Answers1

0
if (isset($_POST['submit'])) {
    $decrypt = Decrypt($_POST['decrypt']);
}

and then some where else read the results:

<p><?php echo isset($decrypt)? 'Decryption answer is ' . $decrypt : 'answer goes here' ; ?></p>

also you should have the text area in the form :

<form action="php.php" method="post">
    <textarea id="input" name="decrypt" rows="4" cols="50"></textarea>
    <input type="submit" name="submit" class="button" />
</form>

The name is so important . The post array takes what ever input names inside your form and makes it key in $_POST array and the value of it is what the user types in it.

Yasser CHENIK
  • 370
  • 1
  • 6
  • 17
  • Hi, I got this error Warning: Undefined array key "input" in C:\laragon\www\test\testhtml\php.php on line 20 Array ( [decrypt] => W9aMvbRmmN/52Kmv1rr9i59ecKu2KYIhrL+Mj+dD8VE3BtwUiFIEBqrRc/e3aw8li2GKKu4B3FVyx/dkRnNnmw== [submit] => Submit ) – thebest man Jan 06 '22 at 23:37
  • yes because now your post keys are only `decrypt` and `submit` why ? because they are in name attributes of your form `name="decrypt"` and `name="submit"` . – Yasser CHENIK Jan 06 '22 at 23:39
  • I still can't solve it, I have this error on the html page: `Warning: Undefined variable $decrypt in C:\laragon\www\test\testhtml\test2.php on line 9` which is referring to my html section Line 9 = `

    `
    – thebest man Jan 06 '22 at 23:44
  • Thanks, the last reported issue is gone, but I still get : `Warning: Undefined array key "input" in C:\laragon\www\test\testhtml\php.php on line 20 Array ( [decrypt] => W9aMvbRmmN/52Kmv1rr9i59ecKu2KYIhrL+Mj+dD8VE3BtwUiFIEBqrRc/e3aw8li2GKKu4B3FVyx/dkRnNnmw== [submit] => Submit )` – thebest man Jan 06 '22 at 23:49
  • i confused isset() with !empty() forgive my mistake – Yasser CHENIK Jan 06 '22 at 23:50
  • It's ok, is it possible that you try my code on your laptop please – thebest man Jan 06 '22 at 23:51
  • i think all you have to do is `$decrypt = Decrypt($_POST['decrypt']);` – Yasser CHENIK Jan 06 '22 at 23:54
  • I tried it but didn't work – thebest man Jan 07 '22 at 00:11
  • I tried your updated solution but it would show blank page – thebest man Jan 07 '22 at 00:22
  • here is a [code](https://drive.google.com/file/d/1v_SMl4kSXbq5f6k4BCgYrQF7eS_6Gz6n/view?usp=sharing0) that i made and works 100% . – Yasser CHENIK Jan 07 '22 at 01:14
  • `
    ` this will submit the form to another page php.php but in my code i suggest `
    ` to make the page submit the form to itself
    – Yasser CHENIK Jan 07 '22 at 01:18
  • thank you @Yasser i tried your code again but it's not working can you please retry it? – thebest man Jan 07 '22 at 13:53
  • create a folder `test100` in www and put the index.php file in it . to see your page start your wamp server 1st and then search in the browser for `localhost/test100/` . – Yasser CHENIK Jan 07 '22 at 14:01
  • what i meant is that it doesn't decrypt anything, your code doesn't include the decrypt function, can you test it please – thebest man Jan 07 '22 at 14:34
  • yes just replace my decrypt method with yours . My code is just for sending user input to your server – Yasser CHENIK Jan 07 '22 at 14:50