1 package javax.crypto; 2 3 import java.security.Key; 4 import java.security.SecureRandom; 5 import java.security.InvalidKeyException; 6 import java.security.AlgorithmParameters; 7 import java.security.NoSuchAlgorithmException; 8 import java.security.InvalidAlgorithmParameterException; 9 import java.security.spec.AlgorithmParameterSpec; 10 11 /** 12 * The NullCipher class is a class that provides an 13 * "identity cipher" -- one that does not tranform the plaintext. As 14 * a consequence, the ciphertext is identical to the plaintext. All 15 * initialization methods do nothing, while the blocksize is set to 1 16 * byte. 17 * 18 * @since JCE1.2 19 */ 20 public class NullCipher 21 extends Cipher 22 { 23 static private class NullCipherSpi 24 extends CipherSpi 25 { 26 /** 27 * Sets the mode of this cipher - no op. 28 */ engineSetMode( String mode)29 protected void engineSetMode( 30 String mode) 31 throws NoSuchAlgorithmException 32 { 33 } 34 35 /** 36 * Sets the padding mechanism of this cipher - no op. 37 */ engineSetPadding( String padding)38 protected void engineSetPadding( 39 String padding) 40 throws NoSuchPaddingException 41 { 42 } 43 44 /** 45 * Returns the block size (in bytes) - 1 46 */ engineGetBlockSize()47 protected int engineGetBlockSize() 48 { 49 return 1; 50 } 51 52 /** 53 * Returns the length in bytes that an output buffer would 54 * need to be in order to hold the result of the next <code>update</code> 55 * or <code>doFinal</code> operation, given the input length 56 * <code>inputLen</code> (in bytes). 57 * 58 * @param inputLen the input length (in bytes) 59 * @return the required output buffer size (in bytes) 60 */ engineGetOutputSize( int inputLen)61 protected int engineGetOutputSize( 62 int inputLen) 63 { 64 return inputLen; 65 } 66 67 /** 68 * Returns the initialization vector (IV) in a new buffer. 69 * 70 * @return null 71 */ engineGetIV()72 protected byte[] engineGetIV() 73 { 74 return null; 75 } 76 77 /** 78 * Returns the parameters used with this cipher - null 79 */ engineGetParameters()80 protected AlgorithmParameters engineGetParameters() 81 { 82 return null; 83 } 84 85 /** 86 * Initializes this cipher with a key and a source 87 * of randomness - no op. 88 */ engineInit( int opmode, Key key, SecureRandom random)89 protected void engineInit( 90 int opmode, 91 Key key, 92 SecureRandom random) 93 throws InvalidKeyException 94 { 95 } 96 97 /** 98 * Initializes this cipher with a key, a set of 99 * algorithm parameters, and a source of randomness - no op. 100 */ engineInit( int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random)101 protected void engineInit( 102 int opmode, 103 Key key, 104 AlgorithmParameterSpec params, 105 SecureRandom random) 106 throws InvalidKeyException, InvalidAlgorithmParameterException 107 { 108 } 109 110 /** 111 * Initializes this cipher with a key, a set of 112 * algorithm parameters, and a source of randomness - no op. 113 */ engineInit( int opmode, Key key, AlgorithmParameters params, SecureRandom random)114 protected void engineInit( 115 int opmode, 116 Key key, 117 AlgorithmParameters params, 118 SecureRandom random) 119 throws InvalidKeyException, InvalidAlgorithmParameterException 120 { 121 } 122 123 /** 124 * Continues a multiple-part encryption or decryption operation 125 * (depending on how this cipher was initialized), processing another data 126 * part - in this case just return a copy of the input. 127 */ engineUpdate( byte[] input, int inputOffset, int inputLen)128 protected byte[] engineUpdate( 129 byte[] input, 130 int inputOffset, 131 int inputLen) 132 { 133 if (input == null) 134 { 135 return null; 136 } 137 138 byte[] tmp = new byte[inputLen]; 139 140 System.arraycopy(input, inputOffset, tmp, 0, inputLen); 141 142 return tmp; 143 } 144 145 /** 146 * Continues a multiple-part encryption or decryption operation 147 * (depending on how this cipher was initialized), processing another data 148 * part - in this case just copy the input to the output. 149 */ engineUpdate( byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset)150 protected int engineUpdate( 151 byte[] input, 152 int inputOffset, 153 int inputLen, 154 byte[] output, 155 int outputOffset) 156 throws ShortBufferException 157 { 158 if (input == null) 159 { 160 return 0; 161 } 162 163 if ((output.length - outputOffset) < inputLen) 164 { 165 throw new ShortBufferException("output buffer to short for NullCipher"); 166 } 167 168 System.arraycopy(input, inputOffset, output, outputOffset, inputLen); 169 170 return inputLen; 171 } 172 173 /** 174 * Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation. 175 * The data is encrypted or decrypted, depending on how this cipher was initialized 176 * - in this case just return a copy of the input. 177 */ engineDoFinal( byte[] input, int inputOffset, int inputLen)178 protected byte[] engineDoFinal( 179 byte[] input, 180 int inputOffset, 181 int inputLen) 182 throws IllegalBlockSizeException, BadPaddingException 183 { 184 if (input == null) 185 { 186 return new byte[0]; 187 } 188 189 byte[] tmp = new byte[inputLen]; 190 191 System.arraycopy(input, inputOffset, tmp, 0, inputLen); 192 193 return tmp; 194 } 195 196 /** 197 * Encrypts or decrypts data in a single-part operation, 198 * or finishes a multiple-part operation. 199 * The data is encrypted or decrypted, depending on how this cipher was 200 * initialized. 201 */ engineDoFinal( byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset)202 protected int engineDoFinal( 203 byte[] input, 204 int inputOffset, 205 int inputLen, 206 byte[] output, 207 int outputOffset) 208 throws ShortBufferException, IllegalBlockSizeException, BadPaddingException 209 { 210 if (input == null) 211 { 212 return 0; 213 } 214 215 if ((output.length - outputOffset) < inputLen) 216 { 217 throw new ShortBufferException("output buffer too short for NullCipher"); 218 } 219 220 System.arraycopy(input, inputOffset, output, outputOffset, inputLen); 221 222 return inputLen; 223 } 224 225 /** 226 * Returns the key size of the given key object - 0 227 */ engineGetKeySize( Key key)228 protected int engineGetKeySize( 229 Key key) 230 throws InvalidKeyException 231 { 232 return 0; 233 } 234 } 235 NullCipher()236 public NullCipher() 237 { 238 super(new NullCipherSpi(), null, "NULL"); 239 } 240 } 241