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);
 }
<?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";
Beispiel #3
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);
        }
    }
}
Beispiel #4
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);
 }
/*
 * 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);
    fwrite($whandle, $result);
}
fclose($rhandle);
fclose($whandle);
print "Finished.\n";
Beispiel #6
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;
 }
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);
$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);
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 #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;