Пример #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));
 }
Пример #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)];
 }
Пример #3
0
<?php

error_reporting(E_ALL | E_STRICT);
include dirname(__FILE__) . "/../phpCrypt.php";
use PHP_Crypt\PHP_Crypt;
$text = "This is my secret message.";
$key = "^mY@TEst~Key_0123456789abcefghij";
/**
 * Cipher: ARC4
 * Mode: Stream
 */
$crypt = new PHP_Crypt($key, PHP_Crypt::CIPHER_ARC4, PHP_Crypt::MODE_STREAM);
//$iv = $crypt->createIV(); // STREAM CIPHERS DO NOT REQUIRE AN IV FOR THE STREAM MODE
$encrypt = $crypt->encrypt($text);
$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";
Пример #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);
        }
    }
}
Пример #5
0
$decrypt = "";
$result = "";
print "Encrypting file.txt using:\n";
print "CIPHER: " . $crypt->cipherName() . "\n";
print "MODE: " . $crypt->modeName() . "\n";
/*
 * DO THE ENCRYPTION
 */
$rhandle = fopen("file.txt", "r");
$whandle = fopen("file.encrypted.txt", "w+b");
print "Creating file.encrypted.txt\n";
// CFB mode requires an IV, create it
$iv = $crypt->createIV();
while (!feof($rhandle)) {
    $bytes = fread($rhandle, $cipher_block_sz);
    $result = $crypt->encrypt($bytes);
    fwrite($whandle, $result);
}
fclose($rhandle);
fclose($whandle);
/*
 * DO THE DECRYPTION
 */
$rhandle = fopen("file.encrypted.txt", "rb");
$whandle = fopen("file.decrypted.txt", "w+");
print "Creating file.decrypted.txt\n";
// we need to set the IV to the same IV used for encryption
$crypt->IV($iv);
while (!feof($rhandle)) {
    $bytes = fread($rhandle, $cipher_block_sz);
    $result = $crypt->decrypt($bytes);
Пример #6
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);
 }
Пример #7
0
// PHPCRYPT: DECRYPT
$phpcrypt->IV($iv);
$ts_start = microtime(true);
$decrypt = $phpcrypt->decrypt($encrypt);
$p_time = number_format(microtime(true) - $ts_start, 5);
// OUTPUT
print "MCRYPT ENCRYPTED (HEX):   " . bin2hex($encrypt) . " (length=" . strlen($encrypt) . ", time={$m_time})\n";
print "PHPCRYPT DECRYPTED:       {$decrypt} (length=" . strlen($decrypt) . ", time={$p_time})\n";
print "PHPCRYPT DECRYPTED (HEX): " . bin2hex($decrypt) . "\n";
print "\n\n";
/**
 * ENCRYPT USING phpCrypt
 * DECRYPT USING mCrypt
 */
// PHPCRYPT: ENCRYPT
$phpcrypt->IV($iv);
$ts_start = microtime(true);
$encrypt = $phpcrypt->encrypt($text);
$p_time = number_format(microtime(true) - $ts_start, 5);
// MCRYPT: DECRYPT
mcrypt_generic_init($td, $key, $iv);
$ts_start = microtime(true);
$decrypt = mdecrypt_generic($td, $encrypt);
$m_time = number_format(microtime(true) - $ts_start, 5);
mcrypt_generic_deinit($td);
// OUTPUT
print "PHPCRYPT ENCRYPTED (HEX): " . bin2hex($encrypt) . " (length=" . strlen($encrypt) . ", time={$p_time})\n";
print "MCRYPT DECRYPTED:         {$decrypt} (length=" . strlen($decrypt) . ", time={$m_time})\n";
print "MCRYPT DECRYPTED (HEX):   " . bin2hex($decrypt) . "\n";
// close mcrypt
mcrypt_module_close($td);
Пример #8
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' => "");
        }
    }
}
Пример #9
0
include_once "/phpcrypt-master/phpCrypt.php";
use PHP_Crypt\PHP_Crypt;
if (isset($_POST['dataToEncrypt'])) {
    $data = $_POST['dataToEncrypt'];
}
if (isset($_POST['method'])) {
    $method = $_POST['method'];
}
//$data = "test";
//$data = “This is my secret message.”;
$key = "eou869%Deou869%D";
$crypt = new PHP_Crypt($key, PHP_Crypt::CIPHER_AES_128, PHP_Crypt::MODE_CBC);
//$iv = $crypt->createIV();
$iv = "'kermit@6000#123";
//echo $iv;
if ($method == "crypt") {
    echo "crypt: ";
    //$encrypt = $crypt->encrypt($data);
    $crypt->IV($iv);
    $result = $crypt->encrypt($data);
}
if ($method == "decrypt") {
    echo "decrypt: ";
    //$decrypt = $crypt->decrypt($encrypt);
    $crypt->IV($iv);
    $result = $crypt->encrypt($data);
    $crypt->IV($iv);
    $result = $crypt->decrypt($data);
}
echo $result;