Verschlüsseln, Bild in Php Mittels AES-Algorithmus

Ich versuche zu verschlüsseln, Bild während Upload mit AES 128-bit-aber der text erhalten, verschlüsselt und entschlüsselt, aber ich bin nicht immer so verschlüsseln Sie Bilder vor dem hochladen. Unten ist der code für AES, die ich benutze:

Code:

<?php
/**
Aes encryption
*/
class AES {

  const M_CBC = 'cbc';
  const M_CFB = 'cfb';
  const M_ECB = 'ecb';
  const M_NOFB = 'nofb';
  const M_OFB = 'ofb';
  const M_STREAM = 'stream';

  protected $key;
  protected $cipher;
  protected $data;
  protected $mode;
  protected $IV;
/**
* 
* @param type $data
* @param type $key
* @param type $blockSize
* @param type $mode
*/
  function __construct($data = null, $key = null, $blockSize = null, $mode = null) {
    $this->setData($data);
    $this->setKey($key);
    $this->setBlockSize($blockSize);
    $this->setMode($mode);
    $this->setIV("");
  }

/**
* 
* @param type $data
*/
  public function setData($data) {
    $this->data = $data;
  }

/**
* 
* @param type $key
*/
  public function setKey($key) {
    $this->key = $key;
  }

/**
* 
* @param type $blockSize
*/
  public function setBlockSize($blockSize) {
    switch ($blockSize) {
      case 128:
      $this->cipher = MCRYPT_RIJNDAEL_128;
      break;

      case 192:
      $this->cipher = MCRYPT_RIJNDAEL_192;
      break;

      case 256:
      $this->cipher = MCRYPT_RIJNDAEL_256;
      break;
    }
  }

/**
* 
* @param type $mode
*/
  public function setMode($mode) {
    switch ($mode) {
      case AES::M_CBC:
      $this->mode = MCRYPT_MODE_CBC;
      break;
      case AES::M_CFB:
      $this->mode = MCRYPT_MODE_CFB;
      break;
      case AES::M_ECB:
      $this->mode = MCRYPT_MODE_ECB;
      break;
      case AES::M_NOFB:
      $this->mode = MCRYPT_MODE_NOFB;
      break;
      case AES::M_OFB:
      $this->mode = MCRYPT_MODE_OFB;
      break;
      case AES::M_STREAM:
      $this->mode = MCRYPT_MODE_STREAM;
      break;
      default:
      $this->mode = MCRYPT_MODE_ECB;
      break;
    }
  }

/**
* 
* 
* @return boolean
*/
  public function validateParams() {
    if ($this->data != null &&
        $this->key != null &&
        $this->cipher != null) {
      return true;
    } else {
      return FALSE;
    }
  }

  public function setIV($IV) {
        $this->IV = $IV;
    }
  protected function getIV() {
      if ($this->IV == "") {
        $this->IV = mcrypt_create_iv(mcrypt_get_iv_size($this->cipher, $this->mode), MCRYPT_RAND);
      }
      return $this->IV;
  }

/**
* @return type
* @throws Exception
*/
  public function encrypt() {

    if ($this->validateParams()) {
      return trim(base64_encode(
        mcrypt_encrypt(
          $this->cipher, $this->key, $this->data, $this->mode, $this->getIV())));
    } else {
      throw new Exception('Invlid params!');
    }
  }
/**
* 
* @return type
* @throws Exception
*/
  public function decrypt() {
    if ($this->validateParams()) {
      return trim(mcrypt_decrypt(
        $this->cipher, $this->key, base64_decode($this->data), $this->mode, $this->getIV()));
    } else {
      throw new Exception('Invlid params!');
    }
  }

}
?>

<?php
include 'enc.php';


if(isset($_POST['submit']))
{
    $blockSize = 128;
    $inputKey = "My text to encrypt";



$allowedExts = array("jpg", "jpeg", "gif", "png", "mp3", "mp4", "wma");
$fileName = $_FILES['file']['name'];
$extension = substr($fileName, strrpos($fileName, '.') + 1); //getting the info about the image to get its extension

if(in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
     echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    $aes = new AES($_FILES["file"]["tmp_name"], $inputKey, $blockSize);
    $enc = $aes->encrypt();
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
        echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
       move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $enc."jpg");
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
}
?>
<form method="post"  enctype="multipart/form-data" >

<label for="file"><span>Filename:</span></label>

<input type="file" name="file" id="file" /> 

<br />
<input type="submit" name="submit" value="Submit" />
</form>
  • vor dem hochladen bedeutet im browser, aber ich sehe nur php-code für die Verschlüsselung. Was wollen Sie erreichen? Ich hoffe wirklich, du bist nicht von uns verlangt, den port von php zu js, weil es geschlossen wird.
  • ich möchte nicht zu speichern, verschlüsseln Bild in den Ordner....
  • Ok, was ist dann der Fehler, die Sie sehen? Sie können verschlüsseln Sie eine Datei vor das hochladen nur mit php. Die Datei ist hochgeladen im Klartext in einen temporären Ordner, und nur dann können Sie die Datei verschlüsseln.
  • Können Sie mir bitte einen Hinweis, wie es zu erreichen, weil ich habe überall gesucht, und das einzige, was ich bekommen ist zum verschlüsseln von text, nicht das Bild. vielen Dank für Ihre Antwort
  • Ich bin über folgenden link aesencryption.net
  • bitte helfen Sie mir für AES-rijndael

InformationsquelleAutor Vikash Kumar | 2014-11-24
Schreibe einen Kommentar