Beispiel #1
0
 /**
  * Decrypt data for teampass db.
  *
  * @param string      $string
  * @param string|null $iv
  * @param string|null $key
  *
  * @return bool|string
  */
 public function decrypt($string, $iv = null, $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);
     if (empty($iv)) {
         return '';
     }
     $string = hex2bin(trim($string));
     $iv = hex2bin($iv);
     $crypt->IV($iv);
     $decrypt = $crypt->decrypt($string);
     return str_replace(chr(0), '', $decrypt);
 }
 private static function _getKey()
 {
     if (defined('APPLICATION_ENCRYPTION_KEY') && strlen(APPLICATION_ENCRYPTION_KEY) > 7) {
         $keyPart1 = substr(APPLICATION_ENCRYPTION_KEY, 0, 8);
     } else {
         $keyPart1 = PHP_Crypt::createKey(PHP_Crypt::RAND, 16);
         $keyPart1 = '';
     }
     if (isset($_COOKIE[Encryption::$_keyName])) {
         $keyPart2 = $_COOKIE[Encryption::$_keyName];
     } else {
         // Create a new key and store it in the cookie
         $keyPart2 = PHP_Crypt::createKey(PHP_Crypt::RAND, $keyPart1 ? 8 : 16);
         // TODO: add cookie settings
         echo "setting zpek cookie<br>";
         setcookie(Encryption::$_keyName, $keyPart2);
     }
     return $keyPart1 . $keyPart2;
 }
<?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";
 */
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";
Beispiel #5
0
function GenerateCryptKey($size)
{
    return PHP_Crypt::createKey(PHP_Crypt::RAND, $size);
}
Beispiel #6
0
 /**
  * Decrypt data for teampass db.
  *
  * @param string $encrypted
  * @param string $iv
  * @param string $key
  *
  * @return bool|string
  */
 public function decrypt($encrypted, $iv, $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);
     if (empty($iv)) {
         return false;
     }
     $string = hex2bin($encrypted);
     $iv = hex2bin($iv);
     // load IV
     $crypt->IV($iv);
     // decrypt
     $decrypt = $crypt->decrypt($string);
     // return
     return str_replace(chr(0), "", $decrypt);
 }
<?php

/**
 * An example of how to encrypt a file using AES-128 and CFB mode
 *
 */
error_reporting(E_ALL | E_STRICT);
include dirname(__FILE__) . "/../phpCrypt.php";
use PHP_Crypt\PHP_Crypt;
$key = "^mY@TEst~Key_012";
$crypt = new PHP_Crypt($key, PHP_Crypt::CIPHER_BLOWFISH, PHP_Crypt::MODE_NCFB);
$cipher_block_sz = $crypt->cipherBlockSize();
// in bytes
$encrypt = "";
$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);
Beispiel #8
0
 private function decrypt($encrypted_password, $iv, $salt, $master_password)
 {
     // hash the master password
     $master_password = $this->keygen_s2k($master_password, $salt, 32);
     // decrypt the password with the hashed master password
     $crypt = new PHP_Crypt($master_password, PHP_Crypt::CIPHER_AES_256, PHP_Crypt::MODE_CTR);
     $crypt->IV($iv);
     $decrypted_password = $crypt->decrypt($encrypted_password);
     return $decrypted_password;
 }
$mcrypt_mode = "cbc";
$phpcrypt_cipher = PHP_Crypt::CIPHER_AES_128;
$phpcrypt_mode = PHP_Crypt::MODE_CBC;
// END MODIFYING
/****************************************************************
 * DO NOT EDIT BELOW THIS LINE
 ****************************************************************/
// MCRYPT SETUP
srand((double) microtime() * 1000000);
//for sake of MCRYPT_RAND
$td = mcrypt_module_open($mcrypt_cipher, '', $mcrypt_mode, '');
$key = substr($key, 0, mcrypt_enc_get_key_size($td));
$iv_size = mcrypt_enc_get_iv_size($td);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
// PHPCRYPT SETUP
$phpcrypt = new PHP_Crypt($key, $phpcrypt_cipher, $phpcrypt_mode);
print "MCRYPT: {$mcrypt_cipher} - {$mcrypt_mode}\n";
print "PHPCRYPT: " . $phpcrypt->cipherName() . " - " . $phpcrypt->modeName() . "\n\n";
/**
 * ENCRYPT USING mCrypt
 * DECRYPT USING phpCrypt
 */
// MCRYPT: ENCRYPT
mcrypt_generic_init($td, $key, $iv);
$ts_start = microtime(true);
$encrypt = mcrypt_generic($td, $text);
$m_time = number_format(microtime(true) - $ts_start, 5);
mcrypt_generic_deinit($td);
// PHPCRYPT: DECRYPT
$phpcrypt->IV($iv);
$ts_start = microtime(true);
         /*if (empty($history)) {
               $history = date($_SESSION['settings']['date_format']." ".$_SESSION['settings']['time_format'], $record['date'])." - ".$record['login']." - ".$LANG[$record['action']]." - ".(!empty($record['raison']) ? (count($reason) > 1 ? $LANG[trim($reason[0])].' : '.$reason[1] : ($record['action'] == "at_manual" ? $reason[0] : $LANG[trim($reason[0])])):'');
           } else {
               $history .= "<br />".date($_SESSION['settings']['date_format']." ".$_SESSION['settings']['time_format'], $record['date'])." - ".$record['login']." - ".$LANG[$record['action']]." - ".(!empty($record['raison']) ? (count($reason) > 1 ? $LANG[trim($reason[0])].' => '.$reason[1] : ($record['action'] == "at_manual" ? $reason[0] : $LANG[trim($reason[0])])):'');
           }*/
         if (trim($reason[0]) == "at_pw") {
             if (empty($historyOfPws)) {
                 $historyOfPws = $LANG['previous_pw'] . "\n" . $reason[1];
             } else {
                 $historyOfPws .= "\n" . $reason[1];
             }
         }
     }
 }
 // generate 2d key
 $_SESSION['key_tmp'] = bin2hex(PHP_Crypt::createKey(PHP_Crypt::RAND, 16));
 // Prepare files listing
 $files = $filesEdit = "";
 // launch query
 $rows = DB::query("SELECT id, name, file, extension FROM " . prefix_table("files") . " WHERE id_item=%i", $_POST['id']);
 foreach ($rows as $record) {
     // get icon image depending on file format
     $iconImage = fileFormatImage($record['extension']);
     // prepare text to display
     if (strlen($record['name']) > 60 && strrpos($record['name'], ".") >= 56) {
         $filename = substr($record['name'], 0, 50) . "(truncated)" . substr($record['name'], strrpos($record['name'], "."));
     } else {
         $filename = $record['name'];
     }
     // If file is an image, then prepare lightbox. If not image, then prepare donwload
     if (in_array($record['extension'], $k['image_file_ext'])) {
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' => "");
        }
    }
}
Beispiel #12
0
<?php

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);
}
Beispiel #13
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);
        }
    }
}