How check string is encrypted or not in php?

I have a form that on it, I've got information from database. Now I want to check just a record on it and it is : Is it possible that I check the string value of that field and if it was encrypted or not, for response I return true or false!! How can I solve this issue??

asked Sep 9, 2014 at 6:22

How check string is encrypted or not in php?

3

Check the encrypted of decrypted of the value is equivalent to the input text

answered Sep 9, 2014 at 6:38

To check if it's encrypted you need the key that has encrypted the value.

SELECT if(AES_DECRYPT(`cloumn`, 'encyrption key...') is null, 'false','true') FROM `table`

If you don't have the key you could maybe try testing a certain language signature (the amount of occurences in the string of certain characters). The type of encryption would be rather difficult to detect.

answered Sep 2, 2019 at 12:19

I would think, the header-bit, would be a give away?
My usecase, is where I fetch data from the database, that is encrypted.
After reading the the actual value, I get the decrypted value in my application, but for the duration of the request, this decrypted value is cached on the object itself.
Hence, next time I request the data, it tries to decrypt, but fails. For now, I have a workaround to resolve this by indeed having a try/catch method.
I am looking for a way to see if the string is an encrypted value, without having to go through the entire decryption.
The same issue occurs if I have an unencrypted field I want to encrypt in a later stage. I want the originals to not break the decryption, while still being accessible.

I understand why you are nervous about it and it's not a big deal. I was just asking if there was a better way, because right now, if it fails decryption, the original DB-value is returned, which is also not entirely the right way to go, because it could cause possible leaking of encryption.

I guess a better workaround would be, to mark the object as being decrypted. But that poses another possible issue around undecrypted values being leaked.

Long story short: Encryption is hard!

In PHP, Encryption and Decryption of a string is possible using one of the Cryptography Extensions called OpenSSL function for encrypt and decrypt.

openssl_encrypt() Function: The openssl_encrypt() function is used to encrypt the data.

Syntax:

string openssl_encrypt( string $data, string $method, string $key,
                        $options = 0, string $iv, string $tag= NULL,
                        string $aad, int $tag_length = 16  )

Parameters:

  • $data: It holds the string or data which need to be encrypted.
  • $method: The cipher method is adopted using openssl_get_cipher_methods() function.
  • $key: It holds the encryption key.
  • $options: It holds the bitwise disjunction of the flags OPENSSL_RAW_DATA and OPENSSL_ZERO_PADDING.
  • $iv: It holds the initialization vector which is not NULL.
  • $tag: It holds the authentication tag which is passed by reference when using AEAD cipher mode (GCM or CCM).
  • $aad: It holds the additional authentication data.
  • $tag_length: It holds the length of the authentication tag. The length of authentication tag lies between 4 to 16 for GCM mode.

Return Value: It returns the encrypted string on success or FALSE on failure.

openssl_decrypt() Function The openssl_decrypt() function is used to decrypt the data.

Syntax:

string openssl_decrypt( string $data, string $method, string $key,
             int $options = 0, string $iv, string $tag, string $aad)

Parameters:

  • $data: It holds the string or data which need to be encrypted.
  • $method: The cipher method is adopted using openssl_get_cipher_methods() function.
  • $key: It holds the encryption key.
  • $options: It holds the bitwise disjunction of the flags OPENSSL_RAW_DATA and OPENSSL_ZERO_PADDING.
  • $iv: It holds the initialization vector which is not NULL.
  • $tag: It holds the authentication tag using AEAD cipher mode (GCM or CCM). When authentication fails openssl_decrypt() returns FALSE.
  • $aad: It holds the additional authentication data.

Return Value: It returns the decrypted string on success or FALSE on failure.

Approach: First declare a string and store it into variable and use openssl_encrypt() function to encrypt the given string and use openssl_decrypt() function to descrypt the given string.

Example 1: This example illustrates the encryption and decryption of string.

$simple_string = "Welcome to GeeksforGeeks\n";

echo "Original String: " . $simple_string;

$ciphering = "AES-128-CTR";

$iv_length = openssl_cipher_iv_length($ciphering);

$options = 0;

$encryption_iv = '1234567891011121';

$encryption_key = "GeeksforGeeks";

$encryption = openssl_encrypt($simple_string, $ciphering,

            $encryption_key, $options, $encryption_iv);

echo "Encrypted String: " . $encryption . "\n";

$decryption_iv = '1234567891011121';

$decryption_key = "GeeksforGeeks";

$decryption=openssl_decrypt ($encryption, $ciphering

        $decryption_key, $options, $decryption_iv);

echo "Decrypted String: " . $decryption;

?>

Output:

Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks

Example 2: Below example illustrate the encryption and decryption of string. Here string to be encrypted and decrypted string will be same but the encrypted string is randomly changed respectively.

$simple_string = "Welcome to GeeksforGeeks";

echo "Original String: " . $simple_string . "\n";

$ciphering = "BF-CBC";

$iv_length = openssl_cipher_iv_length($ciphering);

$options = 0;

$encryption_iv = random_bytes($iv_length);

$encryption_key = openssl_digest(php_uname(), 'MD5', TRUE);

$encryption = openssl_encrypt($simple_string, $ciphering,

        $encryption_key, $options, $encryption_iv);

echo "Encrypted String: " . $encryption . "\n";

$decryption_iv = random_bytes($iv_length);

$decryption_key = openssl_digest(php_uname(), 'MD5', TRUE);

$decryption = openssl_decrypt ($encryption, $ciphering,

            $decryption_key, $options, $encryption_iv);

echo "Decrypted String: " . $decryption;

?>

Output:

Original String: Welcome to GeeksforGeeks
Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA==
Decrypted String: Welcome to GeeksforGeeks

References:

  • https://www.php.net/manual/en/function.openssl-encrypt.php
  • https://www.php.net/manual/en/function.openssl-decrypt.php

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


How do you check if a string is encrypted in PHP?

To check if it's encrypted you need the key that has encrypted the value. If you don't have the key you could maybe try testing a certain language signature (the amount of occurences in the string of certain characters). The type of encryption would be rather difficult to detect.

How do you check data is encrypted or not?

In the Data Protection window, click on the icon of the hard drive (aka System Storage). Under System Storage, if you see the following text: OSDisk (C) and In compliance underneath, then your hard drive is encrypted.

How we can encrypt any string using PHP?

In PHP, Encryption and Decryption of a string is possible using one of the Cryptography Extensions called OpenSSL function for encrypt and decrypt. openssl_encrypt() Function: The openssl_encrypt() function is used to encrypt the data. Parameters: $data: It holds the string or data which need to be encrypted.

What are the encryption function in PHP?

PHP encompasses a hash algorithm to encrypt the password. For the most part it is used in functions for password encrypting are crypt(), password_hash() and md5().