KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Acme > Crypto > CbcBlockCipher


1 // CbcBlockCipher - use a block cipher in CBC mode
2
//
3
// Copyright (C) 1996 by Jef Poskanzer <jef@acme.com>. All rights reserved.
4
//
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions
7
// are met:
8
// 1. Redistributions of source code must retain the above copyright
9
// notice, this list of conditions and the following disclaimer.
10
// 2. Redistributions in binary form must reproduce the above copyright
11
// notice, this list of conditions and the following disclaimer in the
12
// documentation and/or other materials provided with the distribution.
13
//
14
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
// SUCH DAMAGE.
25
//
26
// Visit the ACME Labs Java page for up-to-date versions of this and other
27
// fine Java utilities: http://www.acme.com/java/
28

29 package Acme.Crypto;
30
31 import java.io.*;
32
33 /// Use a block cipher in CBC mode.
34
// <P>
35
// A plain old block cipher, key and cleartext-block in, ciphertext-block
36
// out, is said to be in Electronic Code Book (ECB) mode. A given block
37
// of plaintext always encrypts to the same block of ciphertext. This
38
// makes it somewhat vulnerable to known plaintext attacks, block replay
39
// attacks, etc.
40
// <P>
41
// A fairly cheap alternative is to use it in Cipher Block Chaining (CBC)
42
// mode. All this does is XOR each plaintext block with the previous
43
// ciphertext block before encryption. For the first block, where there
44
// is no previous ciphertext block, a caller-specified Initialization
45
// Vector (IV) is used for the XOR. This makes each block's encryption
46
// depend on all the previous blocks
47
// <P>
48
// This class lets you use any given block cipher in CBC mode.
49
// <P>
50
// <A HREF="../../../resources/classes/Acme/Crypto/CbcBlockCipher.java">Fetch the software.</A><BR>
51
// <A HREF="../../../resources/classes/Acme.tar.gz">Fetch the entire Acme package.</A>
52
// <P>
53
// @see Cipher
54
// @see BlockCipher
55
// @see StreamCipher
56
// @see EncryptedOutputStream
57
// @see EncryptedInputStream
58

59 public class CbcBlockCipher extends BlockCipher
60     {
61
62     private BlockCipher blockCipher;
63     private byte[] iv;
64     private byte[] temp;
65
66     /// Constructor.
67
public CbcBlockCipher( BlockCipher blockCipher )
68     {
69     super( blockCipher.keySize(), blockCipher.blockSize() );
70     this.blockCipher = blockCipher;
71     iv = new byte[blockSize()];
72     zeroBlock( iv );
73     temp = new byte[blockSize()];
74     }
75
76
77     // Key routines.
78

79     // Set the key.
80
public void setKey( byte[] key )
81     {
82     blockCipher.setKey( key );
83     }
84     
85
86     // IV routines.
87

88     /// Set the Initialization Vector.
89
public void setIv( byte[] iv )
90     {
91     copyBlock( iv, this.iv );
92     }
93     
94     /// Set and return a random IV.
95
// In CBC mode, the IV does not have to be kept secret.
96
// Typical usage is for the caller to set a random IV and then transmit
97
// it as the first block of the message.
98
public byte[] setRandomIv()
99     {
100     byte[] riv = new byte[blockSize()];
101     randomBlock( riv );
102     setIv( riv );
103     return riv;
104     }
105
106
107     // Block encryption routines.
108

109     /// Encrypt a block of bytes.
110
public void encrypt( byte[] clearText, int clearOff, byte[] cipherText, int cipherOff )
111     {
112     xorBlock( clearText, clearOff, iv, 0, temp, 0, blockSize );
113     blockCipher.encrypt( temp, 0, cipherText, cipherOff );
114     copyBlock( cipherText, cipherOff, iv, 0, blockSize );
115     }
116
117     /// Decrypt a block of bytes.
118
public void decrypt( byte[] cipherText, int cipherOff, byte[] clearText, int clearOff )
119     {
120     blockCipher.decrypt( cipherText, cipherOff, temp, 0 );
121     xorBlock( temp, 0, iv, 0, clearText, clearOff, blockSize );
122     copyBlock( cipherText, cipherOff, iv, 0, blockSize );
123     }
124
125     }
126
Popular Tags