📄 cipheradapter.java
字号:
/* CipherAdapter.java -- Copyright (C) 2002, 2003, 2006 Free Software Foundation, Inc.This file is a part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or (atyour option) any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; if not, write to the Free SoftwareFoundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301USALinking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package gnu.javax.crypto.jce.cipher;import gnu.javax.crypto.cipher.IBlockCipher;import gnu.javax.crypto.cipher.CipherFactory;import gnu.javax.crypto.jce.spec.BlockCipherParameterSpec;import gnu.javax.crypto.mode.IMode;import gnu.javax.crypto.mode.ModeFactory;import gnu.javax.crypto.pad.IPad;import gnu.javax.crypto.pad.PadFactory;import gnu.javax.crypto.pad.WrongPaddingException;import java.security.AlgorithmParameters;import java.security.InvalidAlgorithmParameterException;import java.security.InvalidKeyException;import java.security.Key;import java.security.NoSuchAlgorithmException;import java.security.SecureRandom;import java.security.spec.AlgorithmParameterSpec;import java.security.spec.InvalidParameterSpecException;import java.util.HashMap;import java.util.Map;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.CipherSpi;import javax.crypto.IllegalBlockSizeException;import javax.crypto.NoSuchPaddingException;import javax.crypto.ShortBufferException;import javax.crypto.spec.IvParameterSpec;/** * <p>The implementation of a generic {@link Cipher} <i>Adapter</i> class to * wrap GNU Crypto cipher instances.</p> * * <p>This class defines the <i>Service Provider Interface</i> (<b>SPI</b>) for * the {@link Cipher} class, which provides the functionality of symmetric-key * block ciphers, such as the AES.<p> * * <p>This base class defines all of the abstract methods in {@link CipherSpi}, * but does not define the (non-abstract) key wrapping functions that extended * the base cipher SPI, and these methods thus immediately throw an * {@link UnsupportedOperationException}. If a cipher implementation provides * this functionality, or if it in fact accepts parameters other than the key * and the initialization vector, the subclass should override those methods. * Otherwise a subclass need only call the {@link #CipherAdapter(String)} * constructor with the name of the cipher.</p> * * @version $Revision: 1.1 $ */class CipherAdapter extends CipherSpi{ // Constants and variables. // ------------------------------------------------------------------------- /** Our cipher instance. */ protected IBlockCipher cipher; /** Our mode instance. */ protected IMode mode; /** Our padding instance. */ protected IPad pad; /** The current key size. */ protected int keyLen; /** Our attributes map. */ protected Map attributes; /** An incomplete block. */ protected byte[] partBlock; /** The number of bytes in {@link #partBlock}. */ protected int partLen; /** The length of blocks we are processing. */ protected int blockLen; // Constructor(s) // ------------------------------------------------------------------------- /** * <p>Protected constructor to be called by subclasses. The cipher name * argument should be the appropriate one listed in {@link gnu.crypto.Registry}. * The basic cipher instance is created, along with an instance of the * {@link gnu.crypto.mode.ECB} mode and no padding.</p> * * @param cipherName The cipher to instantiate. * @param blockLen The block length to use. */ protected CipherAdapter(String cipherName, int blockLen) { cipher = CipherFactory.getInstance(cipherName); attributes = new HashMap(); this.blockLen = blockLen; mode = ModeFactory.getInstance("ECB", cipher, blockLen); attributes.put(IBlockCipher.CIPHER_BLOCK_SIZE, new Integer(blockLen)); } /** * <p>Creates a new cipher adapter with the default block size.</p> * * @param cipherName The cipher to instantiate. */ protected CipherAdapter(String cipherName) { cipher = CipherFactory.getInstance(cipherName); blockLen = cipher.defaultBlockSize(); attributes = new HashMap(); mode = ModeFactory.getInstance("ECB", cipher, blockLen); attributes.put(IBlockCipher.CIPHER_BLOCK_SIZE, new Integer(blockLen)); } // Instance methods implementing javax.crypto.CipherSpi. // ------------------------------------------------------------------------- protected void engineSetMode(String modeName) throws NoSuchAlgorithmException { if (modeName.length() >= 3 && modeName.substring(0, 3).equalsIgnoreCase("CFB")) { if (modeName.length() > 3) { try { int bs = Integer.parseInt(modeName.substring(3)); attributes.put(IMode.MODE_BLOCK_SIZE, new Integer(bs / 8)); } catch (NumberFormatException nfe) { throw new NoSuchAlgorithmException(modeName); } modeName = "CFB"; } } else { attributes.remove(IMode.MODE_BLOCK_SIZE); } mode = ModeFactory.getInstance(modeName, cipher, blockLen); if (mode == null) { throw new NoSuchAlgorithmException(modeName); } } protected void engineSetPadding(String padName) throws NoSuchPaddingException { if (padName.equalsIgnoreCase("NoPadding")) { pad = null; return; } pad = PadFactory.getInstance(padName); if (pad == null) { throw new NoSuchPaddingException(padName); } } protected int engineGetBlockSize() { if (cipher != null) { return blockLen; } return 0; } protected int engineGetOutputSize(int inputLen) { final int blockSize = mode.currentBlockSize(); return ((inputLen + partLen) / blockSize) * blockSize; } protected byte[] engineGetIV() { byte[] iv = (byte[]) attributes.get(IMode.IV); if (iv == null) { return null; } return (byte[]) iv.clone(); } protected AlgorithmParameters engineGetParameters() { BlockCipherParameterSpec spec = new BlockCipherParameterSpec( (byte[]) attributes.get(IMode.IV), cipher.currentBlockSize(), keyLen); AlgorithmParameters params; try { params = AlgorithmParameters.getInstance("BlockCipherParameters"); params.init(spec); } catch (NoSuchAlgorithmException nsae) { return null; } catch (InvalidParameterSpecException ipse) { return null; } return params; } protected void engineInit(int opmode, Key key, SecureRandom random) throws InvalidKeyException { switch (opmode) { case Cipher.ENCRYPT_MODE: attributes.put(IMode.STATE, new Integer(IMode.ENCRYPTION));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -