Is there a way in PHP to test if a string has been encrypted using mcrypt_encrypt?
You have not written what you're actually concerned about specifically, but:
Whether or not some data has been encrypted is not dependent on which encryption function has been used but which encryption algorithm. Say, if somebody has encrypted something in PERL or in PHP - you can't tell by having the encrypted string.
So as this applies, you can't tell for mcrypt_encrypt. That function does not leave any sign inside the encrypted data.
However, if you have the key and the original text (plain) as well as the algorithm, you can reverse what mcrypt_encrypt does with mcrypt_decrypt. You can then compare the plains and if they match you can say that the plain was encrypted with the specific key and algorithm.
As we're talking about encryption, this is normally not the case, you don't have the plain.
However, you can create a checksum of the plain and encrypt it as well. Then you can decrypt it later on and compare it with a checksum of the plain you encrypted as well to tell if the data was successfully decrypted. But as this shows, this is actually additional information next to the encrypted data.
If you add more information what you're looking for, it might be possible to give more helpful suggestions.
when encrypting add some static text to your string ; when cheking use mcrypt_encode again with static text this time without original string see if encrypted static text exist in encrypted string . it should work
Presumably you mean mcrypt_encrypt()? There is no mcrypt_encode() function.
No. A properly encrypted string should be indistinguishable from random garbage. The only way to test a crypted string to see if it's crypted is to decrypt it.
Related
I've been playing around with php mcrypt over the weekend with AES used to encrypt text strings with a key. Later I worked up a tiny php tool to encrypt / decrypt your strings with AES/mcrypt now when the key is "wrong" and the text doesn't get decrypted, you end up with what I think is binary from what I've read around (http://i.imgur.com/jF8cZMZ.png), is there anyway in PHP to check if the variable holds binary or a properly decoded string?
My apologies if the title and the intro are a bit misleading.
When you encrypt text and then try to decrypt it, you will get the same text, but when you try to decrypt random data, there is a small chance that the result will be text (decreasing with length of data). You haven't specified what kind of data we are talking about, but determining if the decryption is successful by applying a heuristic is a bad idea. It is slow and may lead to false positives.
You should have a checksum or something like that to determine if the decrypted result is valid. This could be easily done by running sha1 on the plaintext data, prepend the result to the text and encrypt it as a whole. When you decrypt it, you can split (sha1 output has a fixed size, so you know where to split) the resulting string run sha1 on the text part and compare with the hash part. If it matches you have a valid result. You can of course improve the security a little by using SHA-256 or SHA-512.
That's is just one way of doing it, but might not be the best. Better ways would be to use an authenticated mode of operation for AES like GCM or CCM, or use encrypt-then-MAC with a good MAC function like HMAC-SHA512.
With using the approaches above you're free to use any kind of data to encrypt, because you're not limited to determining if it is text or not anymore.
When I pass the clear password to check the login of the user and apply a md5() on the string, the md5 hash is equal to the md5 hash stored in the MySQL database (Login succeed).
But I don't want to transfert the user password in clear inside my POST function, so I decided to use cryptoJS to only send the key and then decrypt the password on the PHP server side.
The problem is, when I'm using the decrypted password, the md5 is different. This is weird because the clear password string is the same than the decrypted password, and the md5 hash is different.
By doing:
var_dump($clearPassword); //Hello.
var_dump($decryptedPassword); //Hello.
But:
var_dump(md5($clearPassword)); //3ea484671d7b00a1df4734ded1aa379c1.
var_dump(md5($decryptedPassword)); //470a1ad08cbdebe075214591ea20fec9.
As you can see, it's exactly the same string but the md5 hash is different, I've noticed that var_dump() give as an output:
string(16) for the $clearPassword;
string(32) for the $decryptPassword;
I tried to change the string encoding but there's no luck. Anyone can explain me why md5() behave like that with those same passwords string? thanks again.
the decrypted password IS NOT the same as the original. Check the length of the two strings, check the encoding, do a byte to byte comparison. "Hello\0" and "Hello \0" seems identical but they are not. Even "Hello\0" and "Hello\0\0\0" are not the same. Maybe the decryption algorithm gives a string length of 32 bytes.
I'm probably going to do like #fpierrat said, just encrypt in the client side and do a straight comparison of md5 hash in the PHP server.
While using Laravel, I recently noted that when it encrypts a value the encrypted string keeps changing.
For example
ecrypt('abc') returns xyzABCDXZY
But if I run the same function a little while later:
ecrypt('abc') returns xyzABDJSAS
Now this has me really really confused. If the encryption result is different for each time the function is called how on earth are they able to get back the original value (abc in this case) upon calling the decrypt function?
I suppose it has something to do with the encryption function but I can't for the life of me figure out what and how. Can someone shed some light on this? Thanks.
in fact it may be able to decrypt it, for example, the encryption algorithm can embed a variable in the encrypted data besides the plain text which was encrypted ( like a seed value ) and so the algorithm would
1) encryption: generate a seed, use that seed to cipher the data, hide the seed inside the ciphered data ,produce the final ciphered data
2) decryption: read the encrypted data, get the hidden seed and apply this to get the plain text
in all cases, the plain text data would be the same.
Reading about generating salt using Cryptographically Secure Pseudo-Random Number Generator (CSPRNG). This salt then will be appended to a string that needs to be hashed.
However, the salt generated by CSPRNG function (for PHP I'm using openssl_random_pseudo_bytes) is actually binary data.
Confused about how I should append this binary data to a string, I saw this PHP example for creating hash. It encodes binary data.
So I just wanted to know if that is what I need to do. I need to encode salt to get a string. Then I can append that salt to a string that needs to be hashed. Or are there other ways of adding salt to a string?
note I'm not hashing a password
If you need to hash a password, please use password_hash() and password_verify(), and probably add password_needs_rehash() - see http://de2.php.net/password_hash.
You might notice that these functions are available since PHP 5.5.0 - if you are using an earlier version of PHP, you can add this compatibility library to make it work with PHP starting at 5.3.7.
It can't get very much easier than that.
It's probably best to first convert your string into binary data using an encoding. UTF-8 encoding is probably best for most use cases. Don't forget to (at least) document which character encoding is used.
Now concatenate the salt and the encoded string. Again, you need to (at least) document the size of the salt. Please make sure you use concatenation of bytes, not strings. Bytes can have any value, including invalid characters, control characters etc.
After the concatenation you can feed the resulting byte array into the hashing function.
If you have trouble with byte concatenation in PHP, you could use hexadecimal values instead. But don't forget to convert them back into bytes before feeding them into the hash method.
I am encrypting values of variables that I am sending through hyper link such that it is not easily editable by user..Suppose I have list of values that are shown in hyper link.
<?=$row['p_name'] ?>
Now I want to navigate to view.php where I want to get back value of $row['p_id'] from the title. Is their any way to do So? What are other functions that encrypt on manage.php and decrypt again view.php page?
You appear to be confusing encryption and integrity assessment.
Integrity assessment:
If your purpose is detect when the user modified the values passed on the URL, a hash code (sometimes known as a Message Digest or a Checksum), such as one produce with SHA256 can suffice.
Simply add the URL an extra parameter value with a hash code. This hash code value can be produced by feeding the hash algorithm with the values which integrity you wish to assert, maybe along an extra secret "key" value (aka "salt"). Upon receiving a request, first verify that the hash produced by the values on the URL matches the hash of the URL; if it doesn't at least one value on the URL was altered.
Encryption:
If your purpose is to hide from the user and others what kind of data/values are passed on the URL, encryption is necessary. Unlike a hash, Encryption algorithms can be reversed and produce back the original input from the encrypted text.
In most cases, encryption can also provide data integrity validation, because it is difficult for someone to alter the encrypted text in a way that it can be decrypted to a structurally valid text. Many encryption algorithms are such that altering even just one or two bytes in the encrypted text, results in producing "gibberish" at decryption time.
Practical suggestions with PHP:
for hashing, use one of: crc32(), md5() and the like. (or also hash() as shown in the question's code snippet).
for encryption, use mcrypt_encrypt() / mcrypt_decrypt() from the MCrypt module
depending on the algorithms used, remember to convert the encrypted output base64 or other format so that it can be part of a URL (many hashing and encryption methods produce binary data that include many characters susceptible of "breaking" urls)
if you don't need more secret encrypt ,try using base64_encode/base64_decode
If you only need the data to be tamper proof you could use hash_hmac