📄 cipheradapter.java
字号:
break; case Cipher.DECRYPT_MODE: attributes.put(IMode.STATE, new Integer(IMode.DECRYPTION)); break; } if (!key.getFormat().equalsIgnoreCase("RAW")) { throw new InvalidKeyException("bad key format " + key.getFormat()); } byte[] kb = key.getEncoded(); if (keyLen == 0) { keyLen = kb.length; } else if (keyLen < kb.length) { byte[] kbb = kb; kb = new byte[keyLen]; System.arraycopy(kbb, 0, kb, 0, keyLen); } attributes.put(IBlockCipher.KEY_MATERIAL, kb); reset(); } protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException { if (params == null) { byte[] iv = new byte[blockLen]; random.nextBytes(iv); attributes.put(IMode.IV, iv); blockLen = cipher.defaultBlockSize(); attributes.put(IBlockCipher.CIPHER_BLOCK_SIZE, new Integer(blockLen)); keyLen = 0; } else if (params instanceof BlockCipherParameterSpec) { attributes.put( IBlockCipher.CIPHER_BLOCK_SIZE, new Integer( ((BlockCipherParameterSpec) params).getBlockSize())); attributes.put(IMode.IV, ((BlockCipherParameterSpec) params).getIV()); keyLen = ((BlockCipherParameterSpec) params).getKeySize(); blockLen = ((BlockCipherParameterSpec) params).getBlockSize(); } else if (params instanceof IvParameterSpec) { attributes.put(IMode.IV, ((IvParameterSpec) params).getIV()); blockLen = cipher.defaultBlockSize(); attributes.put(IBlockCipher.CIPHER_BLOCK_SIZE, new Integer(blockLen)); keyLen = 0; } engineInit(opmode, key, random); } protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException { AlgorithmParameterSpec spec = null; try { if (params != null) { spec = params.getParameterSpec(BlockCipherParameterSpec.class); } } catch (InvalidParameterSpecException ignored) { } engineInit(opmode, key, spec, random); } protected byte[] engineUpdate(byte[] input, int off, int len) { final int blockSize = mode.currentBlockSize(); final int count = (partLen + len) / blockSize; final byte[] out = new byte[count * blockSize]; try { engineUpdate(input, off, len, out, 0); } catch (ShortBufferException x) { // should not happen x.printStackTrace(System.err); } return out; } // protected int // engineUpdate(byte[] in, int inOff, int inLen, byte[] out, int outOff) // throws ShortBufferException // { // int blockSize = mode.currentBlockSize(); // int count = (partLen + inLen) / blockSize; // if (count * blockSize > out.length - outOff) { // throw new ShortBufferException(); // } // byte[] buf; // if (partLen > 0 && count > 0) { // buf = new byte[partLen + inLen]; // System.arraycopy(partBlock, 0, buf, 0, partLen); // if (in != null && inLen > 0) { // System.arraycopy(in, inOff, buf, partLen, inLen); // } // partLen = 0; // inOff = 0; // } else { // buf = in; // } // for (int i = 0; i < count; i++) { // mode.update(buf, i * blockSize + inOff, out, i * blockSize + outOff); // } // if (inOff + inLen > count * blockSize) { // partLen = (inOff + inLen) - (count * blockSize); // System.arraycopy(in, count * blockSize, partBlock, 0, partLen); // } // return count * blockSize; // } protected int engineUpdate(byte[] in, int inOff, int inLen, byte[] out, int outOff) throws ShortBufferException { if (inLen == 0) { // nothing to process return 0; } final int blockSize = mode.currentBlockSize(); final int blockCount = (partLen + inLen) / blockSize; final int result = blockCount * blockSize; if (result > out.length - outOff) { throw new ShortBufferException(); } if (blockCount == 0) { // not enough bytes for even 1 block System.arraycopy(in, inOff, partBlock, partLen, inLen); partLen += inLen; return 0; } final byte[] buf; // we have enough bytes for at least 1 block if (partLen == 0) { // if no cached bytes use input buf = in; } else { // prefix input with cached bytes buf = new byte[partLen + inLen]; System.arraycopy(partBlock, 0, buf, 0, partLen); if (in != null && inLen > 0) { System.arraycopy(in, inOff, buf, partLen, inLen); } inOff = 0; } for (int i = 0; i < blockCount; i++) { // update blockCount * blockSize mode.update(buf, inOff, out, outOff); inOff += blockSize; outOff += blockSize; } partLen += inLen - result; if (partLen > 0) { // cache remaining bytes from buf System.arraycopy(buf, inOff, partBlock, 0, partLen); } return result; } protected byte[] engineDoFinal(byte[] input, int off, int len) throws IllegalBlockSizeException, BadPaddingException { final byte[] result; final byte[] buf = engineUpdate(input, off, len); if (pad != null) { switch (((Integer) attributes.get(IMode.STATE)).intValue()) { case IMode.ENCRYPTION: byte[] padding = pad.pad(partBlock, 0, partLen); byte[] buf2 = engineUpdate(padding, 0, padding.length); result = new byte[buf.length + buf2.length]; System.arraycopy(buf, 0, result, 0, buf.length); System.arraycopy(buf2, 0, result, buf.length, buf2.length); break; case IMode.DECRYPTION: int padLen; try { padLen = pad.unpad(buf, 0, buf.length); } catch (WrongPaddingException wpe) { throw new BadPaddingException(wpe.getMessage()); } result = new byte[buf.length - padLen]; System.arraycopy(buf, 0, result, 0, result.length); break; default: throw new IllegalStateException(); } } else { if (partLen > 0) { throw new IllegalBlockSizeException(partLen + " trailing bytes"); } result = buf; } try { reset(); } catch (InvalidKeyException ike) { // Should not happen; if we initialized it with the current // parameters before, we should be able to do it again. throw new Error(ike); } return result; } protected int engineDoFinal(byte[] in, int inOff, int inLen, byte[] out, int outOff) throws BadPaddingException, IllegalBlockSizeException, ShortBufferException { byte[] buf = engineDoFinal(in, inOff, inLen); if (out.length + outOff < buf.length) { throw new ShortBufferException(); } System.arraycopy(buf, 0, out, outOff, buf.length); return buf.length; } private void reset() throws InvalidKeyException { mode.reset(); mode.init(attributes); if (pad != null) { pad.reset(); pad.init(blockLen); } partBlock = new byte[blockLen]; partLen = 0; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -