📄 cipher.java
字号:
"the certificate's key can only be used for encryption"); } break; case ENCRYPT_MODE: if (!keyInfo[3]) { throw new InvalidKeyException( "the certificate's key cannot be used for transforming data"); } if (keyInfo[8]) { throw new InvalidKeyException( "the certificate's key can only be used for decryption"); } break; case UNWRAP_MODE: if (!keyInfo[2] || keyInfo[7]) { throw new InvalidKeyException( "the certificate's key cannot be used for key unwrapping"); } break; case WRAP_MODE: if (!keyInfo[2] || keyInfo[8]) { throw new InvalidKeyException( "the certificate's key cannot be used for key wrapping"); } break; } } } init(opmode, certificate.getPublicKey(), random); } /** * <p>Initialize this cipher with the supplied key and 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>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. * @param random The source of randomness to use. * @throws java.security.InvalidKeyException If the underlying cipher * instance rejects the given key. */ public final void init(int opmode, Key key, SecureRandom random) throws InvalidKeyException { state = opmode; if (cipherSpi != null) { cipherSpi.engineInit(opmode, key, random); } } /** * <p>Initialize this cipher with the supplied key and parameters.</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) then 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. * @param params The algorithm parameters to initialize this instance * with. * @throws java.security.InvalidKeyException If the underlying cipher * instance rejects the given key. * @throws java.security.InvalidAlgorithmParameterException If the * supplied parameters are inappropriate for this cipher. */ public final void init(int opmode, Key key, AlgorithmParameters params) throws InvalidKeyException, InvalidAlgorithmParameterException { init(opmode, key, params, new SecureRandom()); } /** * <p>Initialize this cipher with the supplied key and parameters.</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) then 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. * @param params The algorithm parameters to initialize this instance * with. * @throws java.security.InvalidKeyException If the underlying cipher * instance rejects the given key. * @throws java.security.InvalidAlgorithmParameterException If the * supplied parameters are inappropriate for this cipher. */ public final void init(int opmode, Key key, AlgorithmParameterSpec params) throws InvalidKeyException, InvalidAlgorithmParameterException { init(opmode, key, params, new SecureRandom()); } /** * <p>Initialize this cipher with the supplied key, parameters, and * 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>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. * @param params The algorithm parameters to initialize this instance * with. * @param random The source of randomness to use. * @throws java.security.InvalidKeyException If the underlying cipher * instance rejects the given key. * @throws java.security.InvalidAlgorithmParameterException If the * supplied parameters are inappropriate for this cipher. */ public final void init(int opmode, Key key, AlgorithmParameters params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException { state = opmode; if (cipherSpi != null) { cipherSpi.engineInit(opmode, key, params, random); } } /** * <p>Initialize this cipher with the supplied key, parameters, and * 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>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. * @param params The algorithm parameters to initialize this instance * with. * @param random The source of randomness to use. * @throws java.security.InvalidKeyException If the underlying cipher * instance rejects the given key. * @throws java.security.InvalidAlgorithmParameterException If the * supplied parameters are inappropriate for this cipher. */ public final void init(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException { state = opmode; if (cipherSpi != null) { cipherSpi.engineInit(opmode, key, params, random); } } /** * Unwrap a previously-wrapped key. * * @param wrappedKey The wrapped key. * @param wrappedKeyAlgorithm The algorithm with which the key was * wrapped. * @param wrappedKeyType The type of key (public, private, or * secret) that this wrapped key respresents. * @return The unwrapped key. * @throws java.lang.IllegalStateException If this instance has not be * initialized for unwrapping. * @throws java.security.InvalidKeyException If <code>wrappedKey</code> * is not a wrapped key, if the algorithm cannot unwrap this * key, or if the unwrapped key's type differs from the * specified type. * @throws java.security.NoSuchAlgorithmException If * <code>wrappedKeyAlgorithm</code> is not a valid algorithm * name. */ public final Key unwrap(byte[] wrappedKey, String wrappedKeyAlgorithm, int wrappedKeyType) throws IllegalStateException, InvalidKeyException, NoSuchAlgorithmException { if (cipherSpi == null) { return null; } if (state != UNWRAP_MODE) { throw new IllegalStateException("instance is not for unwrapping"); } return cipherSpi.engineUnwrap(wrappedKey, wrappedKeyAlgorithm, wrappedKeyType); } /** * Continue a multi-part transformation on an entire byte array, * returning the transformed bytes. * * @param input The input bytes. * @return The transformed bytes. * @throws java.lang.IllegalStateException If this cipher was not * initialized for encryption or decryption. */ public final byte[] update(byte[] input) throws IllegalStateException { return update(input, 0, input.length); } /** * Continue a multi-part transformation on part of a byte array, * returning the transformed bytes. * * @param input The input bytes. * @param inputOffset The index in the input to start. * @param inputLength The number of bytes to transform. * @return The transformed bytes. * @throws java.lang.IllegalStateException If this cipher was not * initialized for encryption or decryption. */ public final byte[] update(byte[] input, int inputOffset, int inputLength) throws IllegalStateException { 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( "cipher is not for encrypting or decrypting"); } return cipherSpi.engineUpdate(input, inputOffset, inputLength); } /** * Continue a multi-part transformation on part of a byte array, * placing the transformed bytes into the given array. * * @param input The input bytes. * @param inputOffset The index in the input to start. * @param inputLength The number of bytes to transform. * @param output The output byte array. * @return The number of transformed bytes. * @throws java.lang.IllegalStateException If this cipher was not * initialized for encryption or decryption. * @throws javax.security.ShortBufferException If there is not enough * room in the output array to hold the transformed bytes. */ public final int update(byte[] input, int inputOffset, int inputLength, byte[] output) throws IllegalStateException, ShortBufferException { return update(input, inputOffset, inputLength, output, 0); } /** * Continue a multi-part transformation on part of a byte array, * placing the transformed bytes into the given array. * * @param input The input bytes. * @param inputOffset The index in the input to start. * @param inputLength The number of bytes to transform. * @param output The output byte array. * @param outputOffset The index in the output array to start. * @return The number of transformed bytes. * @throws java.lang.IllegalStateException If this cipher was not * initialized for encryption or decryption. * @throws javax.security.ShortBufferException If there is not enough * room in the output array to hold the transformed bytes. */ public final int update(byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset) throws IllegalStateException, 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( "cipher is not for encrypting or decrypting"); } return cipherSpi.engineUpdate(input, inputOffset, inputLength, output, outputOffset); } /** * Wrap a key. * * @param key The key to wrap. * @return The wrapped key. * @throws java.lang.IllegalStateException If this instance was not * initialized for key wrapping. * @throws javax.crypto.IllegalBlockSizeException If this instance has * no padding and the key is not a multiple of the block size. * @throws java.security.InvalidKeyException If this instance cannot * wrap this key. */ public final byte[] wrap(Key key) throws IllegalStateException, IllegalBlockSizeException, InvalidKeyException { if (cipherSpi == null) { return null; } if (state != WRAP_MODE) { throw new IllegalStateException("instance is not for key wrapping"); } return cipherSpi.engineWrap(key); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -