bbonkr / crypto-js

Automatically exported from code.google.com/p/crypto-js
0 stars 0 forks source link

The aes.js use gibberish-aes (with md5) #122

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I would like to use this hash with sha256 or sha512. Can you help me?

Original issue reported on code.google.com by thenextw...@gmail.com on 30 Mar 2014 at 11:52

GoogleCodeExporter commented 9 years ago
*hash = encryption (Sorry, I misspelled.)

Original comment by thenextw...@gmail.com on 30 Mar 2014 at 11:54

GoogleCodeExporter commented 9 years ago
Example with php:

function encrypt($data, $password) {
    // Set a random salt
    $salt = openssl_random_pseudo_bytes(8);

    $salted = '';
    $dx = '';
    // Salt the key(176) and iv(16) = 192
    while (strlen($salted) < 192) {
      $dx = hash(sha512, $dx.$password.$salt, true);
      $salted .= $dx;
    }

    $key = substr($salted, 0, 176);
    $iv  = substr($salted, 176,16);

    $encrypted_data = openssl_encrypt($data, 'aes-256-cbc', $key, true, $iv);
    return base64_encode('Salted__' . $salt . $encrypted_data);
  }

function decrypt($edata, $password) {
    $data = base64_decode($edata);
    $salt = substr($data, 8, 8);
    $ct = substr($data, 16);

    $rounds = 3;
    $data00 = $password.$salt;
    $sha512_hash = array();
    $sha512_hash[0] = hash(sha512, $data00, true);
    $result = $sha512_hash[0];
    for ($i = 1; $i < $rounds; $i++) {
      $sha512_hash[$i] = hash(sha512, $sha512_hash[$i - 1].$data00, true);
        $result .= $sha512_hash[$i];
    }
    $key = substr($result, 0, 176);
    $iv  = substr($result, 176,16);

      return openssl_decrypt($ct, 'aes-256-cbc', $key, true, $iv);
  }

Original comment by thenextw...@gmail.com on 30 Mar 2014 at 8:02