📄 cipher.java
字号:
* <p> * See <a href="../guide/ijce/Algorithms.html#PaddingScheme"> * <cite>International JCE Standard Algorithm Names</cite></a> for a list * of PaddingScheme algorithm names. * * @return the algorithm's standard padding scheme name (such as * "PKCS#7" or "NONE") */ public final String getPadding() { return (paddingName == null) ? "NONE" : paddingName; } /** * Returns the name of the provider of this cipher. * * @return the provider name (such as "SUN" or "Cryptix") */ public final String getProvider() { return provider; } /** * Returns true if this cipher is a padding block cipher. * <p> * A cipher is a padding block cipher iff <code>getPlaintextBlockSize() > 1 * && getPaddingScheme() != null</code>. * If getPlaintextBlockSize throws an exception, so will this method. * <p> * This method is needed because * <samp><a href="java.security.CipherInputStream.html">CipherInputStream</a></samp> and * <samp><a href="java.security.CipherOutputStream.html">CipherOutputStream</a></samp> * use a different buffering algorithm for padding block ciphers. * <p> * <strong><a href="../guide/ijce/JCEDifferences.html">This method * is not supported in JavaSoft's version of JCE.</a></strong> */ public final boolean isPaddingBlockCipher() { return getPlaintextBlockSize() > 1 && getPaddingScheme() != null; } /** * Returns the size of the buffer necessary to hold the output * resulting from a call to <code>update</code> (i.e. not including * padding). This call takes into account any incomplete block * currently being buffered. * * @param inLen the number of bytes to process. * @exception IllegalArgumentException if inLen < 0 */ public final int outBufferSize(int inLen) { return outBufferSizeInternal(inLen, false); } /** * Returns the size of the buffer necessary to hold the output * resulting from a call to <code>crypt</code> (including padding * for the final block of the stream, if applicable). This call takes * into account any incomplete block currently being buffered. * <p> * <strong><a href="../guide/ijce/JCEDifferences.html">This method * is not supported in JavaSoft's version of JCE.</a></strong> * * @param inLen the number of bytes to process. * @exception IllegalArgumentException if inLen < 0 */ public final int outBufferSizeFinal(int inLen) { return outBufferSizeInternal(inLen, true); } /** * Returns the minimum number of bytes of input, that will cause an * output of <i>outLen</i> bytes from a call to <code>update</code> (i.e. * not including padding). This call takes into account any incomplete * block currently being buffered. * <p> * This is used by <samp>CipherInputStream</samp>, for example, to * calculate how much data must be read from its underlying stream before * encryption or decryption. * <p> * <strong><a href="../guide/ijce/JCEDifferences.html">This method * is not supported in JavaSoft's version of JCE.</a></strong> * * @param outLen the number of bytes of output required. * @exception IllegalArgumentException if outLen < 0 */ public final int inBufferSize(int outLen) { return inBufferSizeInternal(outLen, false); } /** * Returns the minimum number of bytes of input, that will cause an * output of <i>outLen</i> bytes from a call to <code>crypt</code> * (including padding for the final block of the stream, if applicable). * This call takes into account any incomplete block currently being * buffered. * <p> * <strong><a href="../guide/ijce/JCEDifferences.html">This method * is not supported in JavaSoft's version of JCE.</a></strong> * * @param outLen the number of bytes of output required. * @exception IllegalArgumentException if outLen < 0 */ public final int inBufferSizeFinal(int outLen) { return inBufferSizeInternal(outLen, true); } /** * Returns the length of a block for this cipher. If plaintext and * ciphertext blocks are different lengths, this method throws an * IllegalBlockSizeException. * * @return the length in bytes of a block. * @exception IllegalBlockSizeException if getPlaintextBlockSize() != * getCiphertextBlockSize() */ public final int blockSize() { int blocksize = enginePlaintextBlockSize(); if (blocksize != engineCiphertextBlockSize()) throw new IllegalBlockSizeException( "blockSize() called when plaintext and ciphertext block sizes differ"); return blocksize; } /** * Returns the length of an input block, in bytes. When the cipher is * in encryption mode, this is the length of a plaintext block. When in * decryption mode, it is the length of a ciphertext block. * <p> * <strong><a href="../guide/ijce/JCEDifferences.html">This method * is not supported in JavaSoft's version of JCE.</a></strong> * * @return the length in bytes of an input block for this cipher. * @exception Error if the cipher is uninitialized. */ public final int getInputBlockSize() { switch (getState()) { case ENCRYPT: return enginePlaintextBlockSize(); case DECRYPT: return engineCiphertextBlockSize(); default: IJCE.reportBug("invalid Cipher state: " + getState()); case UNINITIALIZED: throw new Error("cipher uninitialized"); } } /** * Returns the length of an output block, in bytes. When the cipher is * in encryption mode, this is the length of a ciphertext block. When in * decryption mode, it is the length of a plaintext block. * <p> * <strong><a href="../guide/ijce/JCEDifferences.html">This method * is not supported in JavaSoft's version of JCE.</a></strong> * * @return the length in bytes of an output block for this cipher. * @exception Error if the cipher is uninitialized. */ public final int getOutputBlockSize() { switch (getState()) { case ENCRYPT: return engineCiphertextBlockSize(); case DECRYPT: return enginePlaintextBlockSize(); default: IJCE.reportBug("invalid Cipher state: " + getState()); case UNINITIALIZED: throw new Error("cipher uninitialized"); } } /** * Returns the length of a plaintext block, in bytes. * For byte-oriented stream ciphers, this method returns 1. * <p> * <strong><a href="../guide/ijce/JCEDifferences.html">This method * is not supported in JavaSoft's version of JCE.</a></strong> * * @return the length in bytes of a plaintext block for this cipher. */ public final int getPlaintextBlockSize() { return enginePlaintextBlockSize(); } /** * Returns the length of a ciphertext block, in bytes. * For byte-oriented stream ciphers, this method returns 1. * <p> * <strong><a href="../guide/ijce/JCEDifferences.html">This method * is not supported in JavaSoft's version of JCE.</a></strong> * * @return the length in bytes of a ciphertext block for this cipher. */ public final int getCiphertextBlockSize() { return engineCiphertextBlockSize(); } /** * Initializes this cipher for encryption, using the specified * key. A successful call to this method puts the cipher in the * ENCRYPT state. This method may be called on a cipher in any * state. Any state information (key, feedback buffer, ...) is * lost and reset. * * @param key the key to use for encryption. * @exception NullPointerException if key == null * @exception KeyException if the key is invalid. */ public final void initEncrypt(Key key) throws KeyException { if (key == null) throw new NullPointerException("key == null"); if (tracing) traceVoidMethod("engineInitEncrypt(<" + key + ">)"); engineInitEncrypt(key); state = ENCRYPT; inputSize = enginePlaintextBlockSize(); outputSize = engineCiphertextBlockSize(); if (inputSize < 1 || outputSize < 1) { state = UNINITIALIZED; throw new Error("input or output block size < 1"); } buffer = (!implBuffering && inputSize > 1) ? new byte[inputSize] : null; buffered = 0; if (padding != null) padding.engineSetBlockSize(inputSize); } /** * Initializes this cipher for decryption, using the specified * key. A successful call to this method puts the cipher in the * DECRYPT state. This method may be called on a cipher in any * state. Any state information (key, feedback buffer, ...) is * lost and reset. * * @param key the key to use for decryption. * @exception NullPointerException if key == null * @exception KeyException if the key is invalid. */ public final void initDecrypt(Key key) throws KeyException { if (key == null) throw new NullPointerException("key == null"); if (tracing) traceVoidMethod("engineInitDecrypt(<" + key + ">)"); engineInitDecrypt(key); state = DECRYPT; inputSize = engineCiphertextBlockSize(); outputSize = enginePlaintextBlockSize(); if (inputSize < 1 || outputSize < 1) { state = UNINITIALIZED; throw new Error("input or output block size < 1"); } buffer = (!implBuffering && inputSize > 1) ? new byte[inputSize] : null; buffered = 0; if (padding != null) padding.engineSetBlockSize(outputSize); } /** * Encrypts or decrypts the specified array of data, which is not the * final data in this stream. For block ciphers, if the last block so * far is incomplete, it will be buffered and processed in subsequent * calls to <code>update</code> or <code>crypt</code>. * <p> * Whether the data is encrypted or decrypted depends on the cipher's * initialization state. This method will automatically allocate * an output buffer of the right size. * <p> * <strong><a href="../guide/ijce/JCEDifferences.html">This method * is not supported in JavaSoft's version of JCE.</a></strong> However, * an equivalent method is declared in the JCE 1.2 preview documentation * for <samp>javax.crypto.Cipher</samp>. * * @param in the input data. * @return the encryption or decryption result. * @exception NullPointerException if in == null */ public final byte[] update(byte[] in) { return update(in, 0, in.length); } /** * Encrypts or decrypts the specified subarray of data, which is not the * final data in this stream. For block ciphers, if the last block so * far is incomplete, it will be buffered and processed in subsequent * calls to <code>update</code> or <code>crypt</code>. * <p> * Whether the data is encrypted or decrypted depends on the cipher's * initialization state. This method will automatically allocate * an output buffer of the right size. * <p> * <strong><a href="../guide/ijce/JCEDifferences.html">This method * is not supported in JavaSoft's version of JCE.</a></strong> However, * an equivalent method is declared in the JCE 1.2 preview documentation * for <samp>javax.crypto.Cipher</samp>. * * @param in the input data. * @param offset the offset indicating where the subarray starts in the * <i>in</i> array. * @param length the length of the subarray. * @return the encryption or decryption result. * @exception NullPointerException if in == null * @exception IllegalArgumentException if length < 0 * @exception ArrayIndexOutOfBoundsException if offset < 0 || * (long) offset + length > in.length */ public final byte[] update(byte[] in, int offset, int length) { byte[] out = new byte[outBufferSizeInternal(length, false)]; int outlen = updateInternal(in, offset, length, out, 0, false); if (outlen != out.length) { byte[] newout = new byte[outlen]; System.arraycopy(out, 0, newout, 0, outlen); return newout; } else return out; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -