Information Security Stack Exchange is a question and answer site for information security professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm planning to send across TWO pieces of information over the internet from machine A

  1. A site identifier which will be like:

    $SiteName = "EXAMPLE" (in plaintext)

  2. Encoded text which will be like:

    $Secret = encrypt($SiteName.'-'.time(), $key)

When I get these two pieces of information on machine B, I lookup the $key based on SiteName. If it decrypts and gives me "EXAMPLE", and within say 24 hours, i consider it a valid request.

Is this a safe mechanism?

The encryption methodology is :

$encrypted_string = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $input, MCRYPT_MODE_CBC);
$transmitted = bin2hex($encrypted_string);
share|improve this question
Safe against what? We need more specifics as to what you are trying to protect against. For example it seems to me that I can easily replay the request, and make you behave as though the request is valid. If that's a problem, then no it's not safe. – frankodwyer Apr 25 '11 at 7:18
Also, what's the request? Does it accompany this message, or is this supposed to be the whole request? If the former then there are other problems. – frankodwyer Apr 25 '11 at 7:23
@frankodwyer - yes, you're right, an attacker can send any request with this encrypted message and my system will accept it. see… please – siliconpi May 2 '11 at 6:17
up vote 5 down vote accepted

You do not describe the attack model (i.e. what kind of things an attacker may want to achieve) so I have to do some guesswork here...

A first thing to note is that you are encrypting the "SiteName", which you also send in cleartext, so one has to assume that any attacker observing a message in transit knows the "SiteName". You also encrypt the current time, which is public data (the attacker has a clock, too). Encryption is meant to transform some data in a way which keeps things confidential, but here the input to the encryption function is only public data. So you are using an encryption system for something other than what it was designed for, and that's a worry.

As you describe it, what you seem to be after is a way to authenticate a timed message: you want machine B to make sure that whatever software runs on machine A deliberately produced a given blob, which includes the production time and date. This calls for a Message Authentication Code, not encryption. The usual MAC is HMAC which uses an underlying hash function (when in doubt, choose SHA-256); with PHP, see function hash_hmac(). It is possible to make a MAC out of an encryption system, but it is somewhat tricky and there is no a priori reason to believe that mcrypt_encrypt() does something which is a good MAC; not that I know how to break it, but it is better to use a tool which has been specifically designed and studied to be a good MAC, and that's HMAC. So you want the message from A to contain the SiteName, the message production time, and a MAC computed over SiteName and the message production time, with $key as key.

Of course, nothing prevents an attacker from replaying an existing message, which may or may not be a problem with you, depending on what machine B does when it receives a "valid request". As a side-note, relying on current time has some practical issues, because there are many users who are quite happy with a system clock off by a few years (I work in the field of digital signatures and X.509 certificates, which have validity dates, and an unset system clock is the number 1 reason of failure).

share|improve this answer
thank you - it actually clarifies things a lot for me... i'm using this as a mechanism to validate that the message is coming from a valid system. The message is generated by system A, shown to a user of system A. He submits a form which passes this value to me, B, I decrypt the message using the key associated with system A. If i get a valid time (<24 hour time difference), i proceed with the request... – siliconpi May 2 '11 at 5:57
please see… – siliconpi May 2 '11 at 6:18

Considering replay attacks (as frankodwyer said): If you check on the timestamp and allow no delay above, say, 2 seconds an adversary will not be able to replay any time later.

Also, I am wondering why you have to transmit your binary data in plain-text. But I would recommend using Base64, which will safe you a few bytes in bandwidth ;)

Please be more elaborate on the scenario and I will try to be more precise :)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.