2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
5506
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
2
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.crypto.provider;
|
|
27 |
|
|
28 |
import java.security.InvalidKeyException;
|
|
29 |
|
|
30 |
/**
|
|
31 |
* This class represents ciphers in electronic codebook (ECB) mode.
|
|
32 |
*
|
|
33 |
* <p>This mode is implemented independently of a particular cipher.
|
|
34 |
* Ciphers to which this mode should apply (e.g., DES) must be
|
|
35 |
* <i>plugged-in</i> using the constructor.
|
|
36 |
*
|
|
37 |
* <p>NOTE: This class does not deal with buffering or padding.
|
|
38 |
*
|
|
39 |
* @author Gigi Ankeny
|
|
40 |
*/
|
|
41 |
|
|
42 |
final class ElectronicCodeBook extends FeedbackCipher {
|
|
43 |
|
|
44 |
ElectronicCodeBook(SymmetricCipher embeddedCipher) {
|
|
45 |
super(embeddedCipher);
|
|
46 |
}
|
|
47 |
|
|
48 |
/**
|
|
49 |
* Gets the name of the feedback mechanism
|
|
50 |
*
|
|
51 |
* @return the name of the feedback mechanism
|
|
52 |
*/
|
|
53 |
String getFeedback() {
|
|
54 |
return "ECB";
|
|
55 |
}
|
|
56 |
|
|
57 |
/**
|
|
58 |
* Resets the iv to its original value.
|
|
59 |
* This is used when doFinal is called in the Cipher class, so that the
|
|
60 |
* cipher can be reused (with its original iv).
|
|
61 |
*/
|
|
62 |
void reset() {
|
|
63 |
// empty
|
|
64 |
}
|
|
65 |
|
|
66 |
/**
|
|
67 |
* Save the current content of this cipher.
|
|
68 |
*/
|
|
69 |
void save() {}
|
|
70 |
|
|
71 |
/**
|
|
72 |
* Restores the content of this cipher to the previous saved one.
|
|
73 |
*/
|
|
74 |
void restore() {}
|
|
75 |
|
|
76 |
/**
|
|
77 |
* Initializes the cipher in the specified mode with the given key
|
|
78 |
* and iv.
|
|
79 |
*
|
|
80 |
* @param decrypting flag indicating encryption or decryption
|
|
81 |
* @param algorithm the algorithm name
|
|
82 |
* @param key the key
|
|
83 |
* @param iv the iv
|
|
84 |
*
|
|
85 |
* @exception InvalidKeyException if the given key is inappropriate for
|
|
86 |
* initializing this cipher
|
|
87 |
*/
|
|
88 |
void init(boolean decrypting, String algorithm, byte[] key, byte[] iv)
|
|
89 |
throws InvalidKeyException {
|
|
90 |
if ((key == null) || (iv != null)) {
|
|
91 |
throw new InvalidKeyException("Internal error");
|
|
92 |
}
|
|
93 |
embeddedCipher.init(decrypting, algorithm, key);
|
|
94 |
}
|
|
95 |
|
|
96 |
/**
|
|
97 |
* Performs encryption operation.
|
|
98 |
*
|
|
99 |
* <p>The input plain text <code>plain</code>, starting at
|
|
100 |
* <code>plainOffset</code> and ending at
|
|
101 |
* <code>(plainOffset + len - 1)</code>, is encrypted.
|
|
102 |
* The result is stored in <code>cipher</code>, starting at
|
|
103 |
* <code>cipherOffset</code>.
|
|
104 |
*
|
|
105 |
* <p>It is the application's responsibility to make sure that
|
|
106 |
* <code>plainLen</code> is a multiple of the embedded cipher's block size,
|
|
107 |
* as any excess bytes are ignored.
|
|
108 |
*
|
|
109 |
* <p>It is also the application's responsibility to make sure that
|
|
110 |
* <code>init</code> has been called before this method is called.
|
|
111 |
* (This check is omitted here, to avoid double checking.)
|
|
112 |
*
|
|
113 |
* @param in the buffer with the input data to be encrypted
|
|
114 |
* @param inOffset the offset in <code>plain</code>
|
|
115 |
* @param len the length of the input data
|
|
116 |
* @param out the buffer for the result
|
|
117 |
* @param outOff the offset in <code>cipher</code>
|
|
118 |
*/
|
|
119 |
void encrypt(byte[] in, int inOff, int len, byte[] out, int outOff) {
|
|
120 |
while (len >= blockSize) {
|
|
121 |
embeddedCipher.encryptBlock(in, inOff, out, outOff);
|
|
122 |
len -= blockSize;
|
|
123 |
inOff += blockSize;
|
|
124 |
outOff += blockSize;
|
|
125 |
}
|
|
126 |
}
|
|
127 |
|
|
128 |
/**
|
|
129 |
* Performs decryption operation.
|
|
130 |
*
|
|
131 |
* <p>The input cipher text <code>cipher</code>, starting at
|
|
132 |
* <code>cipherOffset</code> and ending at
|
|
133 |
* <code>(cipherOffset + len - 1)</code>, is decrypted.
|
|
134 |
* The result is stored in <code>plain</code>, starting at
|
|
135 |
* <code>plainOffset</code>.
|
|
136 |
*
|
|
137 |
* <p>It is the application's responsibility to make sure that
|
|
138 |
* <code>cipherLen</code> is a multiple of the embedded cipher's block
|
|
139 |
* size, as any excess bytes are ignored.
|
|
140 |
*
|
|
141 |
* <p>It is also the application's responsibility to make sure that
|
|
142 |
* <code>init</code> has been called before this method is called.
|
|
143 |
* (This check is omitted here, to avoid double checking.)
|
|
144 |
*
|
|
145 |
* @param in the buffer with the input data to be decrypted
|
|
146 |
* @param inOff the offset in <code>cipherOffset</code>
|
|
147 |
* @param len the length of the input data
|
|
148 |
* @param out the buffer for the result
|
|
149 |
* @param outOff the offset in <code>plain</code>
|
|
150 |
*/
|
|
151 |
void decrypt(byte[] in, int inOff, int len, byte[] out, int outOff) {
|
|
152 |
while (len >= blockSize) {
|
|
153 |
embeddedCipher.decryptBlock(in, inOff, out, outOff);
|
|
154 |
len -= blockSize;
|
|
155 |
inOff += blockSize;
|
|
156 |
outOff += blockSize;
|
|
157 |
}
|
|
158 |
}
|
|
159 |
|
|
160 |
}
|