File: Cipher.php

package info (click to toggle)
horde3 3.1.3-4etch7
  • links: PTS
  • area: main
  • in suites: etch
  • size: 22,876 kB
  • ctags: 18,071
  • sloc: php: 75,151; xml: 2,979; sql: 1,069; makefile: 79; sh: 64
file content (118 lines) | stat: -rw-r--r-- 3,108 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/**
 * The Horde_Cipher:: class provides a common abstracted interface to
 * various Ciphers for encryption of arbitrary length pieces of data.
 *
 * Copyright 2002-2006 Mike Cochrane <mike@graftonhall.co.nz>
 *
 * See the enclosed file COPYING for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * $Horde: framework/Cipher/Cipher.php,v 1.16.12.8 2006/01/01 21:28:10 jan Exp $
 *
 * @author  Mike Cochrane <mike@graftonhall.co.nz>
 * @since   Horde 2.2
 * @package Horde_Cipher
 */
class Horde_Cipher {

    /**
     * The block mode for the cipher chaining
     *
     * @var string
     */
    var $_blockMode = 'CBC';

    /**
     * The initialization vector
     *
     * @var string
     */
    var $_iv = null;

    /**
     * Set the block mode for cipher chaining.
     *
     * @param string $blockMode  The new blockmode.
     */
    function setBlockMode($blockMode)
    {
        $this->_blockMode = $blockMode;
    }

    /**
     * Set the IV.
     *
     * @param string $iv  The new IV.
     */
    function setIV($iv)
    {
        $this->_iv = $iv;
    }

    /**
     * Encrypt a string.
     *
     * @param string $plaintext  The data to encrypt.
     *
     * @return string  The encrypted data.
     */
    function encrypt($plaintext)
    {
        require_once dirname(__FILE__) . '/Cipher/BlockMode.php';
        $blockMode = &Horde_Cipher_BlockMode::factory($this->_blockMode);
        if (!is_null($this->_iv)) {
            $blockMode->setIV($this->_iv);
        }

        return $blockMode->encrypt($this, $plaintext);
    }

    /**
     * Decrypt a string.
     *
     * @param string $ciphertext  The data to decrypt.
     *
     * @return string  The decrypted data.
     */
    function decrypt($ciphertext)
    {
        require_once dirname(__FILE__) . '/Cipher/BlockMode.php';
        $blockMode = &Horde_Cipher_BlockMode::factory($this->_blockMode);
        if (!is_null($this->_iv)) {
            $blockMode->setIV($this->_iv);
        }

        return $blockMode->decrypt($this, $ciphertext);
    }

    /**
     * Attempts to return a concrete Horde_Cipher instance.
     *
     * @param string $cipher  The type of concrete Horde_Cipher subclass to
     *                        return.
     * @param array $params   A hash containing any additional parameters a
     *                        subclass might need.
     *
     * @return Horde_Cipher  The newly created concrete Horde_Cipher instance,
     *                       or PEAR_Error on error.
     */
    function &factory($cipher, $params = null)
    {
        $driver = basename($cipher);

        if (@file_exists(dirname(__FILE__) . '/Cipher/' . $cipher . '.php')) {
            require_once dirname(__FILE__) . '/Cipher/' . $cipher . '.php';
        }

        $class = 'Horde_Cipher_' . $cipher;
        if (class_exists($class)) {
            $cipher = &new $class($params);
        } else {
            $cipher = PEAR::raiseError('Class definition of ' . $class . ' not found.');
        }

        return $cipher;
    }

}