~ubuntu-branches/ubuntu/saucy/horde3/saucy

« back to all changes in this revision

Viewing changes to lib/Horde/Cipher/BlockMode/ecb.php

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2005-05-04 23:08:08 UTC
  • Revision ID: james.westby@ubuntu.com-20050504230808-p4hf3hk28o3v7wir
Tags: upstream-3.0.4
ImportĀ upstreamĀ versionĀ 3.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * The Horde_Cipher_BlockMode_ecb:: This class implements the
 
4
 * Horde_Cipher_BlockMode using the Electronic Code Book method of
 
5
 * encrypting blocks of data.
 
6
 *
 
7
 * $Horde: framework/Cipher/Cipher/BlockMode/ecb.php,v 1.7.12.1 2005/01/03 12:18:56 jan Exp $
 
8
 *
 
9
 * Copyright 2002-2005 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
 * @version $Revision: 1.7.12.1 $
 
16
 * @since   Horde 2.2
 
17
 * @package Horde_Cipher
 
18
 */
 
19
class Horde_Cipher_BlockMode_ecb extends Horde_Cipher_BlockMode {
 
20
 
 
21
    function encrypt(&$cipher, $plaintext)
 
22
    {
 
23
        $encrypted = '';
 
24
        $blocksize = $cipher->getBlockSize();
 
25
 
 
26
        $jMax = strlen($plaintext);
 
27
        for ($j = 0; $j < $jMax; $j += $blocksize) {
 
28
            $plain = substr($plaintext, $j, $blocksize);
 
29
 
 
30
            if (strlen($plain) < $blocksize) {
 
31
                // pad the block with \0's if it's not long enough
 
32
                $plain = str_pad($plain, 8, "\0");
 
33
            }
 
34
 
 
35
            $encrypted .= $cipher->encryptBlock($plain);
 
36
        }
 
37
 
 
38
        return $encrypted;
 
39
    }
 
40
 
 
41
    function decrypt(&$cipher, $ciphertext)
 
42
    {
 
43
        $decrypted = '';
 
44
        $blocksize = $cipher->getBlockSize();
 
45
 
 
46
        $jMax = strlen($ciphertext);
 
47
        for ($j = 0; $j < $jMax; $j += $blocksize) {
 
48
            $plain = substr($ciphertext, $j, $blocksize);
 
49
            $decrypted .= $cipher->decryptBlock($plain);
 
50
        }
 
51
 
 
52
        // remove trailing \0's used to pad the last block
 
53
        while (substr($decrypted, -1, 1) == "\0") {
 
54
            $decrypted = substr($decrypted, 0, -1);
 
55
        }
 
56
 
 
57
        return $decrypted;
 
58
    }
 
59
 
 
60
}