⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cipher.java

📁 jpeg2000编解码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    /**     * Encrypts or decrypts the specified subarray of data, which is not the     * final data in this stream, and places the result in the specified     * output buffer (starting at offset 0). 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. <code>out.length</code> must be at least     * <code>outBufferSize(inLen)</code>, otherwise an     * ArrayIndexOutOfBoundsException will be thrown (in this case it is     * not specified how much, if any, of the output will have been written).     * <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  inOffset     the offset indicating where the subarray starts in     *                      the <i>in</i> array.     * @param  inLen        the length of the subarray.     * @param  out          the output buffer.     * @return the number of bytes written.     * @exception NullPointerException if in == null || out == null     * @exception IllegalArgumentException if inLen < 0     * @exception ArrayIndexOutOfBoundsException if inOffset < 0 ||     *                      outOffset < 0 || (long) inOffset + inLen > in.length ||     *                      outBufferSize(inLen) > out.length     */    public final int    update (byte[] in, int inOffset, int inLen, byte[] out) {        return updateInternal(in, inOffset, inLen, out, 0, false);    }    /**     * Encrypts or decrypts the specified subarray of data, which is not the     * final data in this stream, and places the result in the specified     * output buffer. 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. <code>out.length</code> must be at least     * <code>(long) outOffset + outBufferSize(inLen)</code>, otherwise an     * ArrayIndexOutOfBoundsException will be thrown (in this case it is     * not specified how much, if any, of the output will have been written).     * <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  inOffset     the offset indicating where the subarray starts in     *                      the <i>in</i> array.     * @param  inLen        the length of the subarray.     * @param  out          the output buffer.     * @param  outOffset    the offset indicating where to start writing the     *                      result into the output buffer.     * @return the number of bytes written.     * @exception NullPointerException if in == null || out == null     * @exception IllegalArgumentException if inLen < 0     * @exception ArrayIndexOutOfBoundsException if inOffset < 0 ||     *                      outOffset < 0 || (long) inOffset + inLen > in.length ||     *                      (long) outOffset + outBufferSize(inLen) > out.length     */    public final int    update (byte[] in, int inOffset, int inLen, byte[] out, int outOffset) {        return updateInternal(in, inOffset, inLen, out, outOffset, false);    }    /**     * Encrypts or decrypts the specified array of data, which will be     * automatically padded/unpadded as necessary.     * <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>     * If the final block is incomplete, the cipher must have a padding scheme     * other than "NONE", and it must be in the ENCRYPT state. If this is not     * the case, an IllegalBlockSizeException will be thrown.     * <p>     * If the cipher is in the DECRYPT state and padding is being used,     * at least one full ciphertext block should be passed to <code>crypt</code>.     * This is necessary because the last block contains information needed to     * determine the length of the original plaintext.     *     * @param  in   the input data.     * @return the encryption or decryption result.     * @exception NullPointerException if in == null     * @exception IllegalBlockSizeException if the final block cannot be     *              padded or unpadded.     */    public final byte[] crypt(byte[] in)    throws IllegalBlockSizeException {        return crypt(in, 0, in.length);    }    /**     * Encrypts or decrypts the specified array of data, which will be     * automatically padded/unpadded as necessary.     * <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>     * If the final block is incomplete, the cipher must have a padding scheme     * other than "NONE", and it must be in the ENCRYPT state. If this is not     * the case, an IllegalBlockSizeException will be thrown.     * <p>     * If the cipher is in the DECRYPT state and padding is being used,     * at least one full ciphertext block should be passed to <code>crypt</code>.     * This is necessary because the last block contains information needed to     * determine the length of the original plaintext.     *     * @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     * @exception IllegalBlockSizeException if the final block cannot be padded     *                 or unpadded.     */    public final byte[] crypt(byte[] in, int offset, int length)    throws IllegalBlockSizeException {        byte[] out = new byte[outBufferSizeInternal(length, true)];        int outlen = updateInternal(in, offset, length, out, 0, true);        if (outlen != out.length) {            byte[] newout = new byte[outlen];            System.arraycopy(out, 0, newout, 0, outlen);            return newout;        } else            return out;    }    /**     * Encrypts or decrypts the specified subarray of data, pads or unpads     * it as necessary, and places the result in the specified output buffer.     * <p>     * Whether the data is encrypted or decrypted depends on the cipher's     * initialization state.     * <p>     * If the final block is incomplete, the cipher must have a padding scheme     * other than "NONE", and it must be in the ENCRYPT state. If this is not     * the case, an IllegalBlockSizeException will be thrown.     * <p>     * If the cipher is in the DECRYPT state and padding is being used,     * at least one full ciphertext block should be passed to <code>crypt</code>.     * This is necessary because the last block contains information needed to     * determine the length of the original plaintext.     *     * @param  in           the input data.     * @param  inOffset     the offset indicating where the subarray starts in     *                      the <i>in</i> array.     * @param  inLen        the length of the subarray.     * @param  out          the output buffer.     * @param  outOffset    the offset indicating where to start writing the     *                      result into the output buffer.     * @return the number of bytes written.     * @exception NullPointerException if in == null || out == null     * @exception IllegalArgumentException if inLen < 0     * @exception ArrayIndexOutOfBoundsException if inOffset < 0 ||     *                      outOffset < 0 || (long) inOffset + inLen > in.length ||     *                      (long) outOffset + outBufferSize(inLen) > out.length     */    public final int    crypt(byte[] in, int inOffset, int inLen, byte[] out, int outOffset)    throws IllegalBlockSizeException {        return updateInternal(in, inOffset, inLen, out, outOffset, true);    }    /**     * Equivalent to <code>crypt(in)</code>.     * <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>.     */    public final byte[] doFinal(byte[] in)    throws IllegalBlockSizeException {        return crypt(in, 0, in.length);    }    /**     * Equivalent to <code>crypt(in, offset, length)</code>.     * <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>.     */    public final byte[] doFinal(byte[] in, int offset, int length)    throws IllegalBlockSizeException {        return crypt(in, offset, length);    }    /**     * Equivalent to <code>crypt(in, inOffset, inLen, out, 0)</code>.     * <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>.     */    public final int    doFinal(byte[] in, int inOffset, int inLen, byte[] out)    throws IllegalBlockSizeException {        return crypt(in, inOffset, inLen, out, 0);    }    /**     * Equivalent to <code>crypt(in, inOffset, inLen, out, outOffset)</code>.     * <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>.     */    public final int    doFinal(byte[] in, int inOffset, int inLen, byte[] out, int outOffset)    throws IllegalBlockSizeException {        return crypt(in, inOffset, inLen, out, outOffset);    }    /**     * Internal method to take into account padding and buffering before     * calling engineOutBufferSize. (engineOutBufferSize should not be     * called other than via this method).     *     * @param inLen     the number of bytes of input     * @param isFinal   whether the last block is to be padded     * @return the length in bytes of the output block     * @exception IllegalArgumentException if inLen < 0     */    private int outBufferSizeInternal(int inLen, boolean isFinal) {        if (inLen < 0) throw new IllegalArgumentException("inLen < 0");        if (!implBuffering) {            inLen += buffered;            int remainder = inLen % inputSize;            inLen -= remainder;            if (isFinal && state == ENCRYPT &&                (padding != null || remainder > 0))                inLen += inputSize;        }        if (inLen < 0) IJCE.reportBug("inLen < 0");        if (tracing) traceMethod("engineOutBufferSize(" + inLen + ", " + isFinal + ")");        int result = engineOutBufferSize(inLen, isFinal);        if (tracing) traceResult(result);        return result;    }    /**     * Internal method to take into account padding and buffering before     * calling engineInBufferSize. (engineInBufferSize should not be     * called other than via this method).     *     * @param outLen    the number of bytes of output     * @param isFinal   whether the last block is to be padded     * @return the minimum number of bytes of input     * @exception IllegalArgumentException if outLen < 0     */    private int inBufferSizeInternal(int outLen, boolean isFinal) {        if (!implBuffering) {            int remainder = outLen % outputSize;            if (remainder > 0) outLen += outputSize - remainder;        }        if (tracing) traceMethod("engineInBufferSize(" + outLen + ", " + isFinal + ")");        int result = engineInBufferSize(outLen, isFinal);        if (tracing) traceResult(result);        if (!implBuffering) {            if (isFinal && state == ENCRYPT && padding != null)                result -= inputSize;            result -= buffered;        }        if (result < 0) result = 0;        return result;    }    /**     * This method handles buffering, padding, and calling the cipher's     * engineUpdate and engineCrypt methods.     */    private int updateInternal(byte[] in, int inOffset, int inLen, byte[] out,                               int outOffset, boolean isFinal) {if (DEBUG && debuglevel >= 5 && tracing) traceMethod("updateInternal(<" + dump(in) + ">, " + inOffset + ", " + inLen + ", <" + dump(out) + ">, " + outOffset + ", " + isFinal + ")");boolean exception = false;int outStart = outOffset;try {        if (state == UNINITIALIZED) throw new IllegalStateException("cipher uninitialized");        if (inLen < 0) throw new IllegalArgumentException("inLen < 0");        if (inOffset < 0 || outOffset < 0 || (long)inOffset+inLen > in.length) {if (DEBUG && debuglevel >= 1) debug("inOffset = " + inOffset + ", inLen = " + inLen + ", outOffset = " + outOffset + ", in.length = " + in.length);            throw new ArrayIndexOutOfBoundsException(                "inOffset < 0  || outOffset < 0 || (long)inOffset+inLen > in.length");        }        // if (in == null) exception has already been thrown.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -