Encrypt and decrypt an email address

Is it possible to encrypt and decrypt an email address (or any other text) in Kirby? Is this functionality already built in?
If so, how do you do that?

As far as I remember, email address are automatically encrypted whenever Html::email() is used (this is also used by the email Kirbytag. Internally, this uses Str::encode(): Str::encode() | Kirby CMS

What I want to do is

  • encode an emailadress in the template
  • send it to the controller in a hidden input
  • decode it in the controller and then use it as ‚send to‘-address

I assume the encoding would work like that:

<?php $encodedemailaddress = Html::email(‚$emailaddress‘) ?>

then send $encodedemailaddress via the hidden input to the controller

But how can you decode/decrypt it there?

Could you elaborate a bit more what your use case is? Why do you want to put the email into the template just to send it to the controller again?

The website contains an exchange for used goods. The users can advertise their used products there. When a request for a particular product is made using a request form, one e-mail is sent to the owner of the website and another to the advertiser of the product. The latter is of course different for each product and therefore must be sent to the controller each time anew. Which already works, but the email address sent via hidden input was already intercepted once and used for spam purposes (that caused trouble…). Therefore I would like to encrypt it.

TBH, I’d do this differently. Since the product is obviously assigned to some product ID or so and the email is stored alongside this product, it should be possible to retrieve the email address from the product within the controller without having to store it in a hidden field. Then you don’t have the problem with encryption/decryption (which is not secure anyway).

Why is encryption in this case not secure?

and

If you encode the email address with html::email(), how do you decode it again?

Str::encode() does something fairly simple. Let’s take an example:

  <input type="hidden" value="<?= Str::encode('me@test.de') ?>">

If you look at this with source viewer, you get this

  <input type="hidden" value="&#109;&#x65;&#64;&#116;&#101;&#x73;&#116;&#x2e;&#100;&#x65;">

However, if you use your dev tools inspector, you get the clear text result:

<input type="hidden" value="me@test.de">

So it doesn’t really help you much privacy-wise, it only protects a little against really stupid site scrapers.

So it doesn’t really help you much.

Really secure would be only one way encoding or something that you can only decode with a private key.

But this would be overkill for a use case that can be prevented but not storing personal data in the page in the first place. Because as I said, there is really no reason to do that.

What you could do is use something like str_rot13 (PHP: str_rot13 - Manual) to encode and decode, but as I said, this is more obfuscating than anything and I wouldn’t recommend any such approach.

I agree with Sonja. The best solution would be to transmit the form to the page of the product. Or, if that is not possible, transmit the form to a separate page and include the product ID in a hidden field. The form controller can then use the product ID to look up the recipient dynamically.