140 * a multiple of the block size |
140 * a multiple of the block size |
141 * @return the length of the encrypted data |
141 * @return the length of the encrypted data |
142 */ |
142 */ |
143 int encrypt(byte[] plain, int plainOffset, int plainLen, |
143 int encrypt(byte[] plain, int plainOffset, int plainLen, |
144 byte[] cipher, int cipherOffset) { |
144 byte[] cipher, int cipherOffset) { |
|
145 if (plainLen <= 0) { |
|
146 return plainLen; |
|
147 } |
145 cryptBlockSizeCheck(plainLen); |
148 cryptBlockSizeCheck(plainLen); |
146 cryptNullAndBoundsCheck(plain, plainOffset, plainLen); |
149 cryptNullAndBoundsCheck(plain, plainOffset, plainLen); |
147 cryptNullAndBoundsCheck(cipher, cipherOffset, plainLen); |
150 cryptNullAndBoundsCheck(cipher, cipherOffset, plainLen); |
148 return implEncrypt(plain, plainOffset, plainLen, |
151 return implEncrypt(plain, plainOffset, plainLen, |
149 cipher, cipherOffset); |
152 cipher, cipherOffset); |
188 * a multiple of the block size |
191 * a multiple of the block size |
189 * @return the length of the decrypted data |
192 * @return the length of the decrypted data |
190 */ |
193 */ |
191 int decrypt(byte[] cipher, int cipherOffset, int cipherLen, |
194 int decrypt(byte[] cipher, int cipherOffset, int cipherLen, |
192 byte[] plain, int plainOffset) { |
195 byte[] plain, int plainOffset) { |
|
196 if (cipherLen <= 0) { |
|
197 return cipherLen; |
|
198 } |
193 cryptBlockSizeCheck(cipherLen); |
199 cryptBlockSizeCheck(cipherLen); |
194 cryptNullAndBoundsCheck(cipher, cipherOffset, cipherLen); |
200 cryptNullAndBoundsCheck(cipher, cipherOffset, cipherLen); |
195 cryptNullAndBoundsCheck(plain, plainOffset, cipherLen); |
201 cryptNullAndBoundsCheck(plain, plainOffset, cipherLen); |
196 return implDecrypt(cipher, cipherOffset, cipherLen, plain, plainOffset); |
202 return implDecrypt(cipher, cipherOffset, cipherLen, plain, plainOffset); |
197 } |
203 } |
218 throw new ProviderException("Internal error in input buffering"); |
224 throw new ProviderException("Internal error in input buffering"); |
219 } |
225 } |
220 } |
226 } |
221 |
227 |
222 private static void cryptNullAndBoundsCheck(byte[] array, int offset, int len) { |
228 private static void cryptNullAndBoundsCheck(byte[] array, int offset, int len) { |
223 if (len <= 0) { |
|
224 return; // not an error because cryptImpl/decryptImpl won't execute if len <= 0 |
|
225 } |
|
226 |
|
227 Objects.requireNonNull(array); |
229 Objects.requireNonNull(array); |
228 |
230 |
229 if (offset < 0 || offset >= array.length) { |
231 if (offset < 0 || offset >= array.length) { |
230 throw new ArrayIndexOutOfBoundsException(offset); |
232 throw new ArrayIndexOutOfBoundsException(offset); |
231 } |
233 } |