[ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 /** 3 * The Horde_Cipher_BlockMode_cbc:: This class implements the 4 * Horde_Cipher_BlockMode using the Cipher Block Chaining method of 5 * encrypting blocks of data. 6 * 7 * $Horde: framework/Cipher/Cipher/BlockMode/cbc.php,v 1.7.12.5 2006/01/01 21:28:11 jan Exp $ 8 * 9 * Copyright 2002-2006 Mike Cochrane <mike@graftonhall.co.nz> 10 * 11 * See the enclosed file COPYING for license information (LGPL). If you 12 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 13 * 14 * @author Mike Cochrane <mike@graftonhall.co.nz> 15 * @since Horde 2.2 16 * @package Horde_Cipher 17 */ 18 class Horde_Cipher_BlockMode_cbc extends Horde_Cipher_BlockMode { 19 20 /** 21 * Encrypt a string. 22 * 23 * @param Horde_Cipher $cipher Cipher algorithm to use for 24 * encryption. 25 * @param string $plaintext The data to encrypt. 26 * 27 * @return The encrypted data. 28 */ 29 function encrypt(&$cipher, $plaintext) 30 { 31 $encrypted = ''; 32 33 $blocksize = $cipher->getBlockSize(); 34 $previousCipher = $this->_iv; 35 36 $jMax = strlen($plaintext); 37 for ($j = 0; $j < $jMax; $j += $blocksize) { 38 $plain = substr($plaintext, $j, $blocksize); 39 40 if (strlen($plain) < $blocksize) { 41 // pad the block with \0's if it's not long enough 42 $plain = str_pad($plain, 8, "\0"); 43 } 44 45 $plain = $plain ^ $previousCipher; 46 $previousCipher = $cipher->encryptBlock($plain); 47 $encrypted .= $previousCipher; 48 } 49 50 return $encrypted; 51 } 52 53 /** 54 * Decrypt a string. 55 * 56 * @param Horde_Cipher $cipher Cipher algorithm to use for 57 * decryption. 58 * @param string $ciphertext The data to decrypt. 59 * 60 * @return The decrypted data. 61 */ 62 function decrypt(&$cipher, $ciphertext) 63 { 64 $decrypted = ''; 65 66 $blocksize = $cipher->getBlockSize(); 67 $previousCipher = $this->_iv; 68 69 $jMax = strlen($ciphertext); 70 for ($j = 0; $j < $jMax; $j += $blocksize) { 71 $plain = substr($ciphertext, $j, $blocksize); 72 $decrypted .= $cipher->decryptBlock($plain) ^ $previousCipher; 73 $previousCipher = $plain; 74 } 75 76 // Remove trailing \0's used to pad the last block. 77 while (substr($decrypted, -1, 1) == "\0") { 78 $decrypted = substr($decrypted, 0, -1); 79 } 80 81 return $decrypted; 82 } 83 84 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 18:01:28 2007 | par Balluche grâce à PHPXref 0.7 |