1: <?php
2: /**
3: * Pry Framework
4: *
5: * LICENSE
6: *
7: * This source file is subject to the new BSD license that is bundled
8: * with this package in the file LICENSE.txt.
9: *
10: * @version $Revision: 276 $
11: */
12:
13: /**
14: * Cryptage / Décryptage de fichier
15: * @category Pry
16: * @package File
17: * @version 0.9
18: * @author Olivier ROGER <oroger.fr>
19: */
20:
21: class File_Crypt
22: {
23: /**
24: * Clé de chiffrement
25: * @var string $key
26: */
27: private $key = null;
28:
29: /**
30: * Type de chiffrement
31: * @see Doc Mcrypt pour la liste
32: * @var string $cipher
33: */
34: private $cipher;
35:
36: /**
37: * Mode de chiffrement
38: * @see Doc mcrypt pour liste
39: * @var string $mode
40: */
41: private $mode;
42:
43: /**
44: * Vecteur d'initialisation
45: * @var <type> $iv
46: */
47: private $iv;
48:
49: /**
50: * Constructeur
51: * @param string $key Clé de chiffrage
52: * @param string $cipher Type de chiffrment (défaut 3DES)
53: * @param string $mode Mode de chiffrement (défaut nofb)
54: */
55: public function __construct($key,$cipher = MCRYPT_3DES,$mode = MCRYPT_MODE_NOFB)
56: {
57: if(!extension_loaded('mcrypt'))
58: throw new Exception ('Extension mcrypt nécessaire');
59:
60: $this->cipher = $cipher;
61: $this->mode = $mode;
62: $key_size = mcrypt_module_get_algo_key_size($this->cipher);
63: $this->key = substr($this->key, 0, $key_size);
64: $iv_size = mcrypt_get_iv_size($this->cipher, $this->mode);
65: $this->iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
66: }
67:
68: /**
69: * Crypte un fichier
70: * @param File_FileManager $filein Fichier à crypter
71: * @param File_FileManager $fileout Fichier crypté
72: * @param boolean $deletein Supprime ou non le fichier non crypté à la fin du processus.
73: */
74: public function crypt(File_FileManager $filein,File_FileManager $fileout,$deletein = true)
75: {
76: $data2Crypt = $this->readFile($filein);
77: if($deletein)
78: $filein->delete();
79: unset($filein);
80:
81: $dataCrypted = mcrypt_encrypt($this->cipher, $this->key, $data2Crypt, $this->mode, $this->iv);
82: $this->writeFile($fileout,$dataCrypted);
83: }
84:
85: /**
86: * Décrypte un fichier
87: * @param File_FileManager $filein Fichier à décrypter
88: * @param File_FileManager $fileout Fichier en clair
89: * @param boolean $deletein Supprime ou non le fichier crypté à la fin du processus
90: */
91: public function decrypt(File_FileManager $filein,File_FileManager $fileout, $deletein = false)
92: {
93: $cryptedData = $this->readFile($filein);
94: if($deletein)
95: $filein->delete();
96: unset($filein);
97:
98: $uncryptedData = mcrypt_decrypt($this->cipher, $this->key, $cryptedData, $this->mode, $this->iv);
99: $this->writeFile($fileout, $uncryptedData);
100: }
101:
102: /**
103: * Lit le contenu du fichier
104: * @param File_FileManager $file
105: * @return string
106: */
107: private function readFile(File_FileManager $file)
108: {
109: $file->open('rb');
110: return $file->read();
111: }
112:
113: /**
114: * Ecrit le fichier
115: * @param File_FileManager $file
116: * @param string $data
117: */
118: private function writeFile(File_FileManager $file,$data)
119: {
120: $file->open('wb+');
121: $file->write($data);
122: $file->close();
123: }
124:
125: }