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
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.
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.
Output:
Original String: Welcome to GeeksforGeeks Encrypted String: hwB1K5NkfcIzkLTWQeQfHLNg5FlyX3PNUA== Decrypted String: Welcome to GeeksforGeeks
References:
- //www.php.net/manual/en/function.openssl-encrypt.php
- //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.