[ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 /** 3 * The Horde_Cipher_BlockMode_cfb64:: This class implements the 4 * Horde_Cipher_BlockMode using a 64 bit cipher feedback. 5 * 6 * This can be used to encrypt any length string and the encrypted 7 * version will be the same length. 8 * 9 * $Horde: framework/Cipher/Cipher/BlockMode/cfb64.php,v 1.7.12.5 2006/01/01 21:28:11 jan Exp $ 10 * 11 * Copyright 2002-2006 Mike Cochrane <mike@graftonhall.co.nz> 12 * 13 * See the enclosed file COPYING for license information (LGPL). If you 14 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 15 * 16 * @author Mike Cochrane <mike@graftonhall.co.nz> 17 * @since Horde 2.2 18 * @package Horde_Cipher 19 */ 20 class Horde_Cipher_BlockMode_cfb64 extends Horde_Cipher_BlockMode { 21 22 /** 23 * Encrypt a string. 24 * 25 * @param Horde_Cipher $cipher Cipher algorithm to use for 26 * encryption. 27 * @param string $plaintext The data to encrypt. 28 * 29 * @return The encrypted data. 30 */ 31 function encrypt(&$cipher, $plaintext) 32 { 33 $encrypted = ''; 34 35 $n = 0; 36 $jMax = strlen($plaintext); 37 for ($j = 0; $j < $jMax; $j++) { 38 if ($n == 0) { 39 $this->_iv = $cipher->encryptBlock($this->_iv); 40 } 41 42 $c = $plaintext[$j] ^ $this->_iv[$n]; 43 $this->_iv = substr($this->_iv, 0, $n) . $c . substr($this->_iv, $n + 1); 44 $encrypted .= $c; 45 46 $n = (++$n) & 0x07; 47 } 48 49 return $encrypted; 50 } 51 52 /** 53 * Decrypt a string. 54 * 55 * @param Horde_Cipher $cipher Cipher algorithm to use for 56 * decryption. 57 * @param string $ciphertext The data to decrypt. 58 * 59 * @return The decrypted data. 60 */ 61 function decrypt(&$cipher, $ciphertext) 62 { 63 $decrypted = ''; 64 65 $n = 0; 66 $jMax = strlen($ciphertext); 67 for ($j = 0; $j < $jMax; $j++) { 68 if ($n == 0) { 69 $this->_iv = $cipher->encryptBlock($this->_iv); 70 } 71 72 $c = $ciphertext[$j] ^ $this->_iv[$n]; 73 $this->_iv = substr($this->_iv, 0, $n) . substr($ciphertext, $j, 1) . substr($this->_iv, $n + 1); 74 $decrypted .= $c; 75 76 $n = (++$n) & 0x07; 77 } 78 79 // Remove trailing \0's used to pad the last block. 80 while (substr($decrypted, -1, 1) == "\0") { 81 $decrypted = substr($decrypted, 0, -1); 82 } 83 84 return $decrypted; 85 } 86 87 }
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 |