Exemplo n.º 1
0
 /**
  * Encrypt data for teampass db.
  *
  * @param string $decrypted
  * @param string $key
  *
  * @return bool|array
  */
 public function encrypt($decrypted, $key = null)
 {
     $salt = null === $key ? $this->defaultSalt : $key;
     //manage key origin
     if (empty($key)) {
         $key = $salt;
     }
     if ($key != $salt) {
         // check key (AES-128 requires a 16 bytes length key)
         if (strlen($key) < 16) {
             for ($x = strlen($key) + 1; $x <= 16; $x++) {
                 $key .= chr(0);
             }
         } else {
             if (strlen($key) > 16) {
                 $key = substr($key, 16);
             }
         }
     }
     // load crypt
     $crypt = new PHP_Crypt($key, PHP_Crypt::CIPHER_AES_128, PHP_Crypt::MODE_CBC);
     $string = trim($decrypted);
     if (empty($string)) {
         return false;
     }
     //generate IV and encrypt
     $iv = $crypt->createIV();
     $encrypted = $crypt->encrypt($string);
     // return
     return array("string" => bin2hex($encrypted), "iv" => bin2hex($iv));
 }
Exemplo n.º 2
0
 /**
  * Encrypt data for teampass db.
  *
  * @param string $string
  * @param string $key
  *
  * @return bool|string
  */
 public function encrypt($string, $key = null)
 {
     if (empty($key)) {
         $key = $this->defaultSalt;
     }
     if ($key != $this->defaultSalt) {
         if (strlen($key) < 16) {
             for ($x = strlen($key) + 1; $x <= 16; ++$x) {
                 $key .= chr(0);
             }
         } elseif (strlen($key) > 16) {
             $key = substr($key, 16);
         }
     }
     $crypt = new PHP_Crypt($key, PHP_Crypt::CIPHER_AES_128, PHP_Crypt::MODE_CBC);
     $iv = $crypt->createIV();
     $encrypt = $crypt->encrypt($string);
     return ['string' => bin2hex($encrypt), 'iv' => bin2hex($iv)];
 }
Exemplo n.º 3
0
 */
error_reporting(E_ALL | E_STRICT);
include dirname(__FILE__) . "/../phpCrypt.php";
use PHP_Crypt\PHP_Crypt;
/*
 * The example below uses Linux/Unix /dev/urandom to create a random
 * string of bytes. Because AES-128 uses a 128 bit (16 byte) key, we
 * request 16 bytes in the second parameter. Please read above for the
 * different constants used to create a key.
 *
 * WE CAN USE THE FOLLOWING METHODS OF CREATING AN KEY:
 * $key = PHP_Crypt::createKey(PHP_Crypt::RAND); // The default, uses PHP's mt_rand()
 * $key = PHP_Crypt::createKey(PHP_Crypt::RAND_DEV_RAND); // unix only, uses /dev/random
 * $key = PHP_Crypt::createKey(PHP_Crypt::RAND_DEV_URAND);// unix only, uses /dev/urandom
 * $key = PHP_Crypt::createKey(PHP_Crypt::RAND_WIN_COM);  // Windows only, uses the com_dotnet extension
 */
$key = PHP_Crypt::createKey(PHP_Crypt::RAND_DEV_URAND, 16);
$text = "This is my secret message.";
// now create the phpCrypt object and set the cipher to AES-128, with CTR mode
$crypt = new PHP_Crypt($key, PHP_Crypt::CIPHER_AES_128, PHP_Crypt::MODE_CTR);
$iv = $crypt->createIV();
$encrypt = $crypt->encrypt($text);
$crypt->IV($iv);
$decrypt = $crypt->decrypt($encrypt);
print "CIPHER: " . $crypt->cipherName() . "\n";
print "MODE: " . $crypt->modeName() . "\n";
print "PLAIN TEXT: {$text}\n";
print "PLAIN TEXT HEX: " . bin2hex($text) . "\n";
print "ENCRYPTED HEX: " . bin2hex($encrypt) . "\n";
print "DECRYPTED: {$decrypt}\n";
print "DECRYPTED HEX: " . bin2hex($decrypt) . "\n";
Exemplo n.º 4
0
function cryption($string, $key, $iv, $type)
{
    // manage key origin
    if (empty($key)) {
        $key = SALT;
    }
    if ($key != SALT) {
        // check key (AES-128 requires a 16 bytes length key)
        if (strlen($key) < 16) {
            for ($x = strlen($key) + 1; $x <= 16; $x++) {
                $key .= chr(0);
            }
        } else {
            if (strlen($key) > 16) {
                $key = substr($key, 16);
            }
        }
    }
    // load crypt
    $crypt = new PHP_Crypt($key, PHP_Crypt::CIPHER_AES_128, PHP_Crypt::MODE_CBC);
    if ($type == "encrypt") {
        // generate IV and encrypt
        $iv = $crypt->createIV();
        $encrypt = $crypt->encrypt($string);
        // return
        return array("string" => bin2hex($encrypt), "iv" => bin2hex($iv));
    } else {
        if ($type == "decrypt") {
            if (empty($iv)) {
                return "";
            }
            $string = hex2bin(trim($string));
            $iv = hex2bin($iv);
            // load IV
            $crypt->IV($iv);
            // decrypt
            $decrypt = $crypt->decrypt($string);
            // return
            return str_replace(chr(0), "", $decrypt);
        }
    }
}
Exemplo n.º 5
0
 private function encrypt($password, $master_password = false)
 {
     // decrypt the master password
     if ($master_password === false) {
         $master_password = $this->account->decrypt_password();
     }
     // generate a random salt
     require_once "PasswordHash.php";
     $hasher = new PasswordHash(8, false);
     $salt = $hasher->get_random_bytes(100);
     // hash the master password
     $master_password = $this->keygen_s2k($master_password, $salt, 32);
     // encrypt the password with the hashed master password
     $crypt = new PHP_Crypt($master_password, PHP_Crypt::CIPHER_AES_256, PHP_Crypt::MODE_CTR);
     $iv = $crypt->createIV();
     $encrypted_password = $crypt->encrypt($password);
     // return all important variables
     return array("iv" => $iv, "encrypted_password" => $encrypted_password, "salt" => $salt);
 }
Exemplo n.º 6
0
function cryption_phpCrypt($string, $key, $iv, $type)
{
    // manage key origin
    if (empty($key)) {
        $key = SALT;
    }
    if ($key != SALT) {
        // check key (AES-128 requires a 16 bytes length key)
        if (strlen($key) < 16) {
            for ($x = strlen($key) + 1; $x <= 16; $x++) {
                $key .= chr(0);
            }
        } else {
            if (strlen($key) > 16) {
                $key = substr($key, 16);
            }
        }
    }
    // load crypt
    $crypt = new PHP_Crypt($key, PHP_Crypt::CIPHER_AES_128, PHP_Crypt::MODE_CBC);
    if ($type == "encrypt") {
        // generate IV and encrypt
        $iv = $crypt->createIV();
        $encrypt = $crypt->encrypt($string);
        // return
        return array("string" => bin2hex($encrypt), "iv" => bin2hex($iv), "error" => empty($encrypt) ? "ERR_ENCRYPTION_NOT_CORRECT" : "");
    } else {
        if ($type == "decrypt") {
            // case if IV is empty
            if (empty($iv)) {
                return array('string' => "", 'error' => "ERR_ENCRYPTION_NOT_CORRECT");
            }
            // convert
            try {
                $string = testHex2Bin(trim($string));
                $iv = testHex2Bin($iv);
            } catch (Exception $e) {
                // error - $e->getMessage();
                return array('string' => "", 'error' => "ERR_ENCRYPTION_NOT_CORRECT");
            }
            // load IV
            $crypt->IV($iv);
            // decrypt
            $decrypt = $crypt->decrypt($string);
            // return
            //return str_replace(chr(0), "", $decrypt);
            return array('string' => str_replace(chr(0), "", $decrypt), 'error' => "");
        }
    }
}