📄 cipher.java
字号:
// $Id: Cipher.java,v 1.1.1.1 2002/08/27 11:49:29 grosbois Exp $//// $Log: Cipher.java,v $// Revision 1.1.1.1 2002/08/27 11:49:29 grosbois// Imported source from cryptix 3.2//// Revision 1.9 2000/08/17 11:35:23 edwin// Package move java.* -> xjava.*, which is needed for JDK 1.3 compatibility.// I had to break permission support even further to make this work (I don't// believe it was working in the first place, so it's not really a problem).//// Revision 1.8 1997/12/03 03:36:06 hopwood// + Committed changes below.//// Revision 1.7.1 1997/12/03 hopwood// + Removed temporary dependency on cryptix.util.core.Hex.//// Revision 1.7 1997/12/03 01:13:16 raif// + Fixed a bug in the updateInternal() method when isFinal is true// for a decryption with padding and in the input length is 0. This// is what CipherInputStream uses to force the padding.//// Revision 1.6 1997/12/01 03:37:27 hopwood// + Committed changes below.//// Revision 1.5.1 1997/11/30 hopwood// + Fixed bugs when used with CipherInputStream and a padding cipher.// + More debugging calls added.//// Revision 1.5 1997/11/29 04:45:12 hopwood// + Committed changes below.//// Revision 1.4.1 1997/11/28 hopwood// + Completed padding implementation in updateInternal.// + Fixed some bugs in updateInternal.// + Changed semantics of getInstance(String algorithm, String provider): it// now looks for all components from the given provider, rather than just// the cipher. Added example documentation of how to specify the provider// for each component.// + Made output of toString more concise.// + Added doFinal methods, to ease migration to JCE 1.2.//// Revision 1.4 1997/11/21 04:31:18 hopwood// + Committed changes below.//// Revision 1.3.2 1997/11/18 David Hopwood// + Don't set the block size for the padding scheme until initEncrypt/// initDecrypt is called, because it might not be valid before then.// Note that only the plaintext block size is relevant for padding.// + Debug output now goes to IJCE.getDebugOutput().// + Got rid of implPadding (i.e. we now throw an exception if it is true).// Allowing the cipher implementation to handle padding is too complicated,// and IMHO unnecessary.// + A call to engineSetPaddingScheme was missing from// getInstance(Cipher, Mode, PaddingScheme).// + Rewrote updateInternal again (still not finished).//// Revision 1.3.1 1997/11/18 David Hopwood// + Implement java.security.Parameterized.// + Made getParameter and setParameter non-final.//// Revision 1.3 1997/11/07 14:32:47 raif// *** empty log message ***//// Revision 1.2 1997/11/07 05:53:26 raif// + Fixed padding handling. See also java.security.PaddingScheme.//// Revision 1.1.1.1 1997/11/03 22:36:57 hopwood// + Imported to CVS (tagged as 'start').//// Revision 0.1.0.10 1997/11/03 David Hopwood// + Added inBufferSize, inBufferSizeFinal and related methods, similar to// outBufferSize etc. These are needed by CipherInputStream.// + Fixed bugs in outBufferSizeInternal.// + Removed getFinalCiphertextSize, engineFinalCiphertextSize, and// unpadBufferSize, which were not used.// + Slightly changed the criteria for a "padding block cipher", to use// getPaddingScheme() != null instead of !getPadding().equals("NONE").//// Revision 0.1.0.9 1997/11/01 David Hopwood// + Changed PaddingScheme.setBlockSize to engineSetBlockSize.//// Revision 0.1.0.8 1997/10/26 David Hopwood// + Added isPaddingBlockCipher method.//// Revision 0.1.0.7 1997/08/17 David Hopwood// + Rewrote part of updateInternal method.//// Revision 0.1.0.6 1997/08/12 David Hopwood// + Replaced getImpl with getImplementation.// + Added references to guide/ijce/Algorithms.html in documentation.//// Revision 0.1.0.5 1997/08/11 David Hopwood// + Changed debugging to use properties file.//// Revision 0.1.0.4 1997/08/09 David Hopwood// + Merge of Raif's version and my version.// + Changed state back to private (the same as in JavaSoft's// JCE).// + Changed implMultiblock back to implBuffering.// + Removed constructor that takes a PaddingScheme (this can now// be done by calling engineSetPaddingScheme).// + Renamed the following private fields:// - algorithm => cipherName// - mode => modeName// - padding => paddingName// - paddingObj => padding// + Corrected getInstance(Cipher, Mode, PaddingScheme) to be static.//// Revision 0.1.0.3 1997/07/20 R. Naffah// + Tested OK.// + Fixed where paddingObj.setBlockSize() whould be called.// + Replaced the updateInternal() with new implementation.//// Revision 0.1.0.2 1997/07/20 R. Naffah// + Use setBlockSize() now defined in PaddingScheme when instantiating// a new cipher.//// Revision 0.1.0.1 1997/07/11 R. Naffah// + Made the state variable protected instead of private so cipher// algorithm and Mode implementations can r/w it. Should remain so// until the setParameter() method is working.// + Modified outBufferSizeInternal() to handle case when no paddingObj// is set.//// Revision 0.1.0.0 1997/??/?? David Hopwood// + Start of history (IJCE 1.0.0).//// $Endlog$/* * Copyright (c) 1997 Systemics Ltd * on behalf of the Cryptix Development Team. All rights reserved. */package xjava.security;import java.io.PrintWriter;import java.security.InvalidParameterException;import java.security.Key;import java.security.KeyException;import java.security.NoSuchAlgorithmException;import java.security.NoSuchProviderException;import java.security.Provider;/** * This class is used to provide the functionality of a general purpose * encryption algorithm, such as DES or RSA. Encryption is used to ensure * confidentiality of digital data. * <p> * This class follows the general algorithm architecture found elsewhere * in the security API: the base class provides an algorithm-independent * interface to basic encryption functionality, with provider implementation * subclassing a subset of the behaviours. * <p> * Like other algorithm-based classes in Java Security, the Cipher * class is separated between application and provider interfaces: * <dl> * <dt> <b>Cipher API</b> (Application Programming Interface) * <dd> This is the interface of methods called by applications * needing encryption services. The API consists of all public * methods. * <dt> <b>Cipher SPI</b> (Service Provider Interface) * <dd> This is the interface implemented by providers that supply * specific algorithms. It consists of all methods whose names * are prefixed by <code>engine</code>. Each such method is * usually called by a correspondingly-named public API method. * For example, the <code>engineInitEncrypt</code> method is * called by the <code>initEncrypt</code> method. * </dl> * <p> * Ciphers represented by this class satisfy the following constraints: * <ul> * <li> At any point in time the cipher has a plaintext block size, and * a ciphertext block size, both expressed in bytes. These sizes may * change when initEncrypt or initDecrypt are called, and they may * be invalid before the cipher is initialized, but they do not * change at other times. * <li> A plaintext block encrypts to exactly one ciphertext block, and * a ciphertext block decrypts to exactly one plaintext block. * </ul> * <p> * Byte-oriented stream ciphers (or ciphers in CFB and OFB modes, for * example) have plaintext and ciphertext block sizes of 1 byte. For * public key ciphers, it is common for the block sizes to be dependent * on the length of some parameter of the public key. * <p> * A block cipher implementation may either implement its own buffering * (by passing <code>implBuffering == true</code> to the constructor), or leave * it to the Cipher superclass (<code>implBuffering</code> == false). When * the implementation handles buffering, data passed to <code>update</code> is * passed directly on to <code>engineUpdate</code>, and data passed to * <code>crypt</code> is passed to <code>engineUpdate</code>, followed * immediately by a call to <code>engineCrypt</code>. * <p> * When the Cipher superclass handles buffering, up to one block is buffered, * in order to ensure that the length of data passed to <code>engineUpdate</code> * is always a multiple of the block size. In this case the <code>engineCrypt</code> * method is not used. * <p> * Cipher implementations are not required or expected to be threadsafe. * If methods of a single Cipher object are called simultaneously by more * than one thread, the result will be unpredictable. * <p> * <b>Copyright</b> © 1997 * <a href="http://www.systemics.com/">Systemics Ltd</a> on behalf of the * <a href="http://www.systemics.com/docs/cryptix/">Cryptix Development Team</a>. * <br>All rights reserved. * <p> * <b>$Revision: 1.1.1.1 $</b> * @author David Hopwood * @author Raif S. Naffah * @since IJCE 1.0.0 */public abstract class Cipherextends IJCE_Traceableimplements Parameterized{// Debugging methods and vars.//........................................................................... private static final boolean DEBUG = true; private static int debuglevel = DEBUG ? IJCE.getDebugLevel("Cipher") : 0; private static PrintWriter err = DEBUG ? IJCE.getDebugOutput() : null; private static void debug(String s) { err.println("Cipher: " + s); } private static String dump(byte[] b) { if (b == null) return "null";// if (b.length <= 32) return cryptix.util.core.Hex.toString(b); return b.toString(); }//........................................................................... /** * The state of the cipher object when it is uninitialized, * that is, the state it is in right after it has been created. */ public static final int UNINITIALIZED = 0; /** * The state of the cipher when it is ready to encrypt, that is, * the state it is in right after a call to <code>initEncrypt</code>. * * @see #initEncrypt */ public static final int ENCRYPT = 1; /** * The state of the cipher when it is ready to decrypt, that is, * the state it is in right after a call to <code>initDecrypt</code>. * * @see #initDecrypt */ public static final int DECRYPT = 2; private boolean implBuffering;// private boolean implPadding; private byte[] buffer; private int buffered; private int inputSize; private int outputSize; private String provider; private String cipherName; private String modeName; private String paddingName; private PaddingScheme padding; private int state; // defaults to UNINITIALIZED = 0 /** * The JCE docs say: "Constructor used for dynamic instantiation." * I don't understand why this is needed. --DJH * @deprecated */ protected Cipher() { super("Cipher"); } /** * Constructor for a Cipher. This constructor is only for use * by subclasses, which should pass the correct arguments to convey * their behaviour to the superclass. Applications cannot call this * constructor directly. * <p> * For byte-oriented stream ciphers (where the input block size is 1), * buffering is not needed, and the <i>implBuffering</i> parameter has * no effect. * <p> * <strong><a href="../guide/ijce/JCEDifferences.html">In this version * of IJCE, <i>implPadding</i> must be false. If it is true, an * <samp>IllegalArgumentException</samp> is thrown.</a></strong> * * @param implBuffering if true, this argument indicates that data * will always be passed from update/crypt to * engineUpdate/engineCrypt without modification. * @param implPadding must be false. * @param provider the name of the provider of the underlying * cryptographic engine. */ protected Cipher(boolean implBuffering, boolean implPadding, String provider) { super("Cipher"); if (implPadding)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -