📄 cipher.java
字号:
public final int getBlockSize() { if (cipherSpi != null) { return cipherSpi.engineGetBlockSize(); } return 1; } /** * Return the currently-operating {@link ExemptionMechanism}. * * @return null, currently. */ public final ExemptionMechanism getExemptionMechanism() { return null; } /** * Return the <i>initialization vector</i> that this instance was * initialized with. * * @return The IV. */ public final byte[] getIV() { if (cipherSpi != null) { return cipherSpi.engineGetIV(); } return null; } /** * Return the {@link java.security.AlgorithmParameters} that this * instance was initialized with. * * @return The parameters. */ public final AlgorithmParameters getParameters() { if (cipherSpi != null) { return cipherSpi.engineGetParameters(); } return null; } /** * Return this cipher's provider. * * @return The provider. */ public final Provider getProvider() { return provider; } /** * Finishes a multi-part transformation, and returns the final * transformed bytes. * * @return The final transformed bytes. * @throws java.lang.IllegalStateException If this instance has not * been initialized, or if a <tt>doFinal</tt> call has already * been made. * @throws javax.crypto.IllegalBlockSizeException If this instance has * no padding and the input is not a multiple of this cipher's * block size. * @throws javax.crypto.BadPaddingException If this instance is * decrypting and the padding bytes do not match this * instance's padding scheme. */ public final byte[] doFinal() throws IllegalStateException, IllegalBlockSizeException, BadPaddingException { return doFinal(new byte[0], 0, 0); } /** * Finishes a multi-part transformation or does an entire * transformation on the input, and returns the transformed bytes. * * @param input The final input bytes. * @return The final transformed bytes. * @throws java.lang.IllegalStateException If this instance has not * been initialized, or if a <tt>doFinal</tt> call has already * been made. * @throws javax.crypto.IllegalBlockSizeException If this instance has * no padding and the input is not a multiple of this cipher's * block size. * @throws javax.crypto.BadPaddingException If this instance is * decrypting and the padding bytes do not match this * instance's padding scheme. */ public final byte[] doFinal(byte[] input) throws IllegalStateException, IllegalBlockSizeException, BadPaddingException { return doFinal(input, 0, input.length); } /** * Finishes a multi-part transformation or does an entire * transformation on the input, and returns the transformed bytes. * * @param input The final input bytes. * @param inputOffset The index in the input bytes to start. * @param inputLength The number of bytes to read from the input. * @return The final transformed bytes. * @throws java.lang.IllegalStateException If this instance has not * been initialized, or if a <tt>doFinal</tt> call has already * been made. * @throws javax.crypto.IllegalBlockSizeException If this instance has * no padding and the input is not a multiple of this cipher's * block size. * @throws javax.crypto.BadPaddingException If this instance is * decrypting and the padding bytes do not match this * instance's padding scheme. */ public final byte[] doFinal(byte[] input, int inputOffset, int inputLength) throws IllegalStateException, IllegalBlockSizeException, BadPaddingException { if (cipherSpi == null) { byte[] b = new byte[inputLength]; System.arraycopy(input, inputOffset, b, 0, inputLength); return b; } if (state != ENCRYPT_MODE && state != DECRYPT_MODE) { throw new IllegalStateException("neither encrypting nor decrypting"); } state = INITIAL_STATE; return cipherSpi.engineDoFinal(input, inputOffset, inputLength); } /** * Finishes a multi-part transformation and stores the transformed * bytes into the given array. * * @param output The destination for the transformed bytes. * @param outputOffset The offset in <tt>output</tt> to start storing * bytes. * @return The number of bytes placed into the output array. * @throws java.lang.IllegalStateException If this instance has not * been initialized, or if a <tt>doFinal</tt> call has already * been made. * @throws javax.crypto.IllegalBlockSizeException If this instance has * no padding and the input is not a multiple of this cipher's * block size. * @throws javax.crypto.BadPaddingException If this instance is * decrypting and the padding bytes do not match this * instance's padding scheme. * @throws javax.crypto.ShortBufferException If the output array is * not large enough to hold the transformed bytes. */ public final int doFinal(byte[] output, int outputOffset) throws IllegalStateException, IllegalBlockSizeException, BadPaddingException, ShortBufferException { if (cipherSpi == null) { return 0; } if (state != ENCRYPT_MODE && state != DECRYPT_MODE) { throw new IllegalStateException("neither encrypting nor decrypting"); } state = INITIAL_STATE; return cipherSpi.engineDoFinal(new byte[0], 0, 0, output, outputOffset); } /** * Finishes a multi-part transformation or transforms a portion of a * byte array, and stores the result in the given byte array. * * @param input The input bytes. * @param inputOffset The index in <tt>input</tt> to start. * @param inputLength The number of bytes to transform. * @param output The output buffer. * @param outputOffset The index in <tt>output</tt> to start. * @return The number of bytes placed into the output array. * @throws java.lang.IllegalStateException If this instance has not * been initialized, or if a <tt>doFinal</tt> call has already * been made. * @throws javax.crypto.IllegalBlockSizeException If this instance has * no padding and the input is not a multiple of this cipher's * block size. * @throws javax.crypto.BadPaddingException If this instance is * decrypting and the padding bytes do not match this * instance's padding scheme. * @throws javax.crypto.ShortBufferException If the output array is * not large enough to hold the transformed bytes. */ public final int doFinal(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset) throws IllegalStateException, IllegalBlockSizeException, BadPaddingException, ShortBufferException { if (cipherSpi == null) { if (inputLength > output.length - outputOffset) { throw new ShortBufferException(); } System.arraycopy(input, inputOffset, output, outputOffset, inputLength); return inputLength; } if (state != ENCRYPT_MODE && state != DECRYPT_MODE) { throw new IllegalStateException("neither encrypting nor decrypting"); } state = INITIAL_STATE; return cipherSpi.engineDoFinal(input, inputOffset, inputLength, output, outputOffset); } public final int doFinal(byte[] input, int inputOffset, int inputLength, byte[] output) throws IllegalStateException, IllegalBlockSizeException, BadPaddingException, ShortBufferException { return doFinal(input, inputOffset, inputLength, output, 0); } /** * Returns the size an output buffer needs to be if this cipher is * updated with a number of bytes. * * @param inputLength The input length. * @return The output length given this input length. * @throws java.lang.IllegalStateException If this instance has not * been initialized, or if a <tt>doFinal</tt> call has already * been made. */ public final int getOutputSize(int inputLength) throws IllegalStateException { if (cipherSpi == null) { return inputLength; } if (state != ENCRYPT_MODE && state != DECRYPT_MODE) { throw new IllegalStateException("neither encrypting nor decrypting"); } return cipherSpi.engineGetOutputSize(inputLength); } /** * <p>Initialize this cipher with the public key from the given * certificate.</p> * * <p>The cipher will be initialized for encryption, decryption, key * wrapping, or key unwrapping, depending upon whether the * <code>opmode</code> argument is {@link #ENCRYPT_MODE}, {@link * #DECRYPT_MODE}, {@link #WRAP_MODE}, or {@link #UNWRAP_MODE}, * respectively.</p> * * <p>As per the Java 1.4 specification, if <code>cert</code> is an * instance of an {@link java.security.cert.X509Certificate} and its * <i>key usage</i> extension field is incompatible with * <code>opmode</code> then an {@link * java.security.InvalidKeyException} is thrown.</p> * * <p>If this cipher requires any random bytes (for example for an * initilization vector) than the {@link java.security.SecureRandom} * with the highest priority is used as the source of these bytes.</p> * * <p>A call to any of the <code>init</code> methods overrides the * state of the instance, and is equivalent to creating a new instance * and calling its <code>init</code> method.</p> * * @param opmode The operation mode to use. * @param certificate The certificate. * @throws java.security.InvalidKeyException If the underlying cipher * instance rejects the certificate's public key, or if the * public key cannot be used as described above. */ public final void init(int opmode, Certificate certificate) throws InvalidKeyException { init(opmode, certificate, new SecureRandom()); } /** * <p>Initialize this cipher with the supplied key.</p> * * <p>The cipher will be initialized for encryption, decryption, key * wrapping, or key unwrapping, depending upon whether the * <code>opmode</code> argument is {@link #ENCRYPT_MODE}, {@link * #DECRYPT_MODE}, {@link #WRAP_MODE}, or {@link #UNWRAP_MODE}, * respectively.</p> * * <p>If this cipher requires any random bytes (for example for an * initilization vector) than the {@link java.security.SecureRandom} * with the highest priority is used as the source of these bytes.</p> * * <p>A call to any of the <code>init</code> methods overrides the * state of the instance, and is equivalent to creating a new instance * and calling its <code>init</code> method.</p> * * @param opmode The operation mode to use. * @param key The key. * @throws java.security.InvalidKeyException If the underlying cipher * instance rejects the given key. */ public final void init(int opmode, Key key) throws InvalidKeyException { state = opmode; if (cipherSpi != null) { cipherSpi.engineInit(opmode, key, new SecureRandom()); } } /** * <p>Initialize this cipher with the public key from the given * certificate and the specified source of randomness.</p> * * <p>The cipher will be initialized for encryption, decryption, key * wrapping, or key unwrapping, depending upon whether the * <code>opmode</code> argument is {@link #ENCRYPT_MODE}, {@link * #DECRYPT_MODE}, {@link #WRAP_MODE}, or {@link #UNWRAP_MODE}, * respectively.</p> * * <p>As per the Java 1.4 specification, if <code>cert</code> is an * instance of an {@link java.security.cert.X509Certificate} and its * <i>key usage</i> extension field is incompatible with * <code>opmode</code> then an {@link * java.security.InvalidKeyException} is thrown.</p> * * <p>If this cipher requires any random bytes (for example for an * initilization vector) than the {@link java.security.SecureRandom} * with the highest priority is used as the source of these bytes.</p> * * <p>A call to any of the <code>init</code> methods overrides the * state of the instance, and is equivalent to creating a new instance * and calling its <code>init</code> method.</p> * * @param opmode The operation mode to use. * @param certificate The certificate. * @param random The source of randomness. * @throws java.security.InvalidKeyException If the underlying cipher * instance rejects the certificate's public key, or if the * public key cannot be used as described above. */ public final void init(int opmode, Certificate certificate, SecureRandom random) throws InvalidKeyException { if (certificate instanceof X509Certificate) { boolean[] keyInfo = ((X509Certificate) certificate).getKeyUsage(); if (keyInfo != null) { switch (opmode) { case DECRYPT_MODE: if (!keyInfo[3]) { throw new InvalidKeyException( "the certificate's key cannot be used for transforming data"); } if (keyInfo[7]) { throw new InvalidKeyException(
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -