-1

I want to ask if my code secure or not. if this is not secure please tell me the reason. This code is mixed of Rjindael-256, base64_encode and SHA-512.

function xencoder($input,$key) {
    $salt = 'Q(i1V7X,k&_Ydk@8T5punQdL@S+Ih&kY6swz:)wg6n5!yOQ*q5iDtm^b49J#XTHB';
    $key = substr(hash('sha512', base64_encode($key.$salt)), 0, 32);
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $output = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $input, MCRYPT_MODE_ECB, $iv));
    return $output;
}

function xdecoder($input,$key) {
   $salt = 'Q(i1V7X,k&_Ydk@8T5punQdL@S+Ih&kY6swz:)wg6n5!yOQ*q5iDtm^b49J#XTHB';
   $key = substr(hash('sha512', base64_encode($key.$salt)), 0, 32);
   $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
   $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
   $output = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($input), MCRYPT_MODE_ECB, $iv);
   return $output;
}

function xhasher($input,$key) {
    $output = strtoupper(hash('sha512',xencoder($input,$key)));
    $output = substr($output,0,36);
    $output = "^XH".$output."^";
    return $output;
}

For example, i call xhasher to hash the password xhasher("mypassword","AUTH_PASSWORD") the output will be ^XH9586C75B1A098D3ACF97C6581E0CCB0C8CF8^

6
  • 6
    You appear to be reinventing your own password hashing algorithm. Worse, it appears at first glance that your function is reversible, which is a cardinal sin for password hashing. Please just use an existing, heavily-cryptanalyzed function purpose built for this use case such as bcrypt, scrypt, or PBKDF2. Commented May 1, 2015 at 1:19
  • You convert everything to upper, which reduces the entropy of the cyphertext and cut off the outputs to 36 characters. Do you mean to use the same salt for every encryption?
    – schroeder
    Commented May 1, 2015 at 2:39
  • schroeder :I use the same salt, but i have an additional salt. The additional salt is $key variable.
    – dharaninja
    Commented May 1, 2015 at 9:31
  • stephen : Excuse me, can you explain more why you think my function is reversible ? And how to reverse that hashed string ?
    – dharaninja
    Commented May 1, 2015 at 9:33
  • No it's not... Why do you ask that question? Were you planning to use that to protect your data?
    – Gudradain
    Commented May 1, 2015 at 19:10

2 Answers 2

1

You are using ECB mode, that is broken. You could replace it with CBC.

You reduce the key to half strength as you compose a key of only hex characters.

The initialization vector does nothing in your code because of the ECB mode.

If you actually had an initialization vector you would have to send it along with the encrypted message, and use that for decryption.

Your scheme lacks message authentication, that makes it malleable, and thus vulnerable to some subtle attacks.

You hint that xhasher is a password hashing scheme, for this it lacks any key strengthening mechanism. And the way you use it, it lacks a salt. (The $salt in you code is actually a pepper in the password hashing.)

4
  • Thanks for your advice. But, if i use CBC mode, i watch that every encryption generate different cipher every time. How to compare the right result and the wrong result in xhasher ? Xhasher is using sha512, you know that sha512 is very sensitive for each changes in the string.
    – dharaninja
    Commented May 3, 2015 at 5:14
  • @dharaninja The point of the iv is that you get different results. Why do you think that you need to use your encoder in the hash function? Commented May 3, 2015 at 6:35
  • In my opinion, if i use my xhasher after xencoder, the hashed string will be difficult to crack. You know that a lot of people only use salt to strength their hash. For example, sha1($pass,$salt). So, if i encode first, the hash will more stronger than only putting salt.
    – dharaninja
    Commented May 6, 2015 at 8:15
  • @dharaninja That doesn't matter, you are already using a strong cryptographic hash, a cryptanalysis attack is the least of your concerns for this primitive. Commented May 6, 2015 at 10:18
0

It is not true that you use a static salt, salt must be random number with fixed length for each input.

1
  • Thanks for your advice. Now, i use random salt. The random salt is generated and saved to the session. And the PHP code will check if the session is available or not. How about your opinion ? I use the code for password hashing and session hashing for user who already logged in to my app.
    – dharaninja
    Commented May 3, 2015 at 5:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .