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

📄 des.java

📁 jpeg2000编解码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
// $Id: DES.java,v 1.1.1.1 2002/08/27 12:32:09 grosbois Exp $//// $Log: DES.java,v $// Revision 1.1.1.1  2002/08/27 12:32:09  grosbois// Add cryptix 3.2//// Revision 1.6  2000/08/17 11:40:51  edwin// java.* -> xjava.*//// Revision 1.5  1997/12/19 06:03:31  hopwood// + Committed changes below.//// Revision 1.4.1  1997/12/19  hopwood// + Added reference and URL for FIPS 46-2.//// Revision 1.4  1997/11/29 04:42:56  hopwood// + Changes to engineUpdate method.//// Revision 1.3  1997/11/22 07:05:39  raif// + Removed most of the test data to cryptix.util.test.Des.mtest//   for use with Maker.// + Cosmetics.//// Revision 1.2  1997/11/20 19:31:40  hopwood// + cryptix.util.* name changes.//// Revision 1.1.1.1.1  1997/11/20  David Hopwood// + Added native_crypt3 method (the C code will need to be written or//   copied from Eric Young's implementation).//// Revision 1.1.1.1  1997/11/03 22:36:56  hopwood// + Imported to CVS (tagged as 'start').//// Revision 0.3.2.0  1997/08/17  David Hopwood// + Removed all deprecated methods and fields. Cryptix 2.2 compatibility//   is now handled by the separate cryptix.security.DES class.// + Tightened some of the access specifiers (e.g. SPI methods were public,//   and are now protected).// + Ensured that this class is final, and added a comment that this is for//   security reasons.//   If it were not final, some VMs have a bug that would allow a subclass//   that implements Cloneable to call Object's or Cipher's clone() method//   using invokenonvirtual, which would duplicate the pointer to the native//   state. Then calling finalize() on the original and using the clone (for//   example) would result in freed memory being written to :-(.//// Revision 0.3.1.1  1997/08/06  David Hopwood// + Changed BLOCK_LENGTH to BLOCK_SIZE in most places, leaving//   BLOCK_LENGTH deprecated.// + Added missing finalize() method.// + Now consistent with Blowfish 0.3.1.3.//// Revision 0.3.1.0  1997/08/02  David Hopwood// + Merged my version, Raif's version, and the version from Systemics'//   server.// + Now consistent with Blowfish 0.3.1.2 (including security fixes to//   engineUpdate).// + Added reference to DES-EDE3 in comments.// + Required native code version is 2.3.//// Revision 0.3.0.2  1997/07/14  R. Naffah// + Java code tests OK.// + Consistent with Blowfish 0.3.0.5.// + Included code (crypt3() method) from cryptix.tools.Crypt//   ported by John F. Dumas (jdumas@zgs.com) to generate Unix-like//   crypt(3) passwords.//// Revision 0.3.0.1  1997/07/05  David Hopwood// + Many changes (JCE, native linking, debugging, ...), to be//   consistent with Blowfish.//// Revision 0.3.0.0  1997/04/05  Systemics// + Added Geoffrey Keating's Java code to DLL stubs, wrote doco, etc.// + Moved copyright notices to end.//// Revision 0.2.5.2  1996/?/0?  Geoffrey Keating// + Ported Eric Young's C version to Java.//// Revision 0.2.5.1  1997/03/15  Jill Baker// + Moved this file here from old namespace.//// Revision 0.2.5.0  1997/02/24  Systemics// + Original version - DLL links only.//// $Endlog$/* * Copyright (c) 1995-1997 Systemics Ltd * on behalf of the Cryptix Development Team.  All rights reserved. */package cryptix.provider.cipher;import cryptix.util.core.Debug;import cryptix.CryptixException;import cryptix.util.core.ArrayUtil;import cryptix.util.core.Hex;import cryptix.provider.key.RawSecretKey;import java.io.PrintWriter;import xjava.security.Cipher;import java.security.Key;import java.security.InvalidKeyException;import xjava.security.SymmetricCipher;/** * DES is a block cipher with an 8 byte block size. The key length * is 8 bytes, but only 56 bits are used as the parity bit in each * byte is ignored. * <P> * This algorithm has been seriously analysed over the last 30 years, * and no significant weaknesses have been reported. Its only known * flaw is that the key length of 56 bits makes it relatively easy to * brute-force it. * <p> * To overcome this near-fatal flaw, it is recommended that DES be * used in Triple DES mode. The JCA algorithm name for the recommended * form of Triple DES is "DES-EDE3/CBC", which is implemented by the * <samp><a href="cryptix.provider.cipher.DES_EDE3.html">DES_EDE3</a></samp> * and <samp><a href="cryptix.provider.mode.CBC.html">CBC</a></samp classes. * <p> * DES was written by IBM and first released in 1976. The algorithm is * freely usable for both single and triple encryption. * <p> * <b>References:</b> * <ol> *    <li> <a href="mailto:schneier@counterpane.com">Bruce Schneier</a>, *         "Chapter 12 Data Encryption Standard," *         <cite>Applied Cryptography, 2nd edition</cite>, *         John Wiley &amp; Sons, 1996. *         <p> *    <li> NIST FIPS PUB 46-2 (supercedes FIPS PUB 46-1), *         "Data Encryption Standard", *         U.S. Department of Commerce, December 1993.<br> *         <a href="http://www.itl.nist.gov/div897/pubs/fip46-2.htm"> *         http://www.itl.nist.gov/div897/pubs/fip46-2.htm</a> * </ol> * <p> * <b>Copyright</b> &copy; 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  Systemics Ltd * @author  Geoffrey Keating (this Java implementation) * @author  Eric Young * @author  David Hopwood * @author  Raif S. Naffah * @author  John F. Dumas (jdumas@zgs.com) * @since   Cryptix 2.2.2 */public final class DES // must be final for security reasonsextends Cipherimplements SymmetricCipher{// Debugging methods and vars.//...........................................................................    private static final boolean DEBUG = Debug.GLOBAL_DEBUG;    private static final boolean DEBUG_SLOW = Debug.GLOBAL_DEBUG_SLOW;    private static final int debuglevel = DEBUG ? Debug.getLevel("DES") : 0;    private static final PrintWriter err = DEBUG ? Debug.getOutput() : null;    private static void debug(String s) { err.println("DES: " + s); }// Native library linking methods and vars.//...........................................................................    private static NativeLink linkStatus = new NativeLink("DES", 2, 3);    /**     * Gets an object representing the native linking status of this class.     */    public static cryptix.util.core.LinkStatus getLinkStatus() { return linkStatus; }    /**     * The native reference to the current native key schedule     * structure. Defaults to 0 but is set by native code after a     * successful call to native_init().     * <p>     * IMPORTANT: Do not change the name of this variable without     * duplicating the same in the native code.     */    private long native_cookie;    /**     * This object must be synchronized on while calling any native instance     * method. It is null if the native code is not being used (e.g. the     * library did not load successfully, or the user disabled its use in     * the properties file).     */    private Object native_lock; // defaults to null    private void link() {        synchronized(linkStatus) {            try {                if (linkStatus.attemptLoad()) {                    linkStatus.checkVersion(getLibMajorVersion(), getLibMinorVersion());                    linkStatus.check(native_clinit());                }                if (linkStatus.useNative()) {                    linkStatus.check(native_init());                    native_lock = new Object();                }            } catch (UnsatisfiedLinkError e) {                linkStatus.fail(e);if (DEBUG && debuglevel > 2) debug(e.getMessage());            }if (DEBUG && debuglevel > 2) debug("Using native library? " + (native_lock != null));        }    }// Native support API//...........................................................................    // The methods that get the library version.    private native static int getLibMajorVersion();    private native static int getLibMinorVersion();    /**     * Static initialization and self-test method for the native code.     *     * @return a string if an error occurred or null otherwise.     */    private native String native_clinit();    /**     * Initializes the native state for this cipher object and allocates     * needed native storage for the internal key schedule. A reference     * to the newly allocated space, if successful, is stored in the     * instance variable <code>native_cookie</code>.     *     * @return a string if an error occurred or null otherwise.     */    private native String native_init();    /**     * This function expands the user key to an internal form.     *     * @param  cookie   a valid reference to the native key structure. This     *                  value is set by the native library upon return from     *                  native_init() (see link() method at the top).     * @param  userKey  a byte array representing the user key     * @return an error String, or null if there was no error     */    private native String native_ks(long cookie, byte[] userKey);    /**     * Encrypts/decrypts a data block.     * <p>     * FUTURE: possibly change this to be able to process more than one block,     * to reduce native method call overhead.     * <p>     * SECURITY: the caller <strong>must</strong> ensure that:     * <ul>     *   <li> <code>in != null</code>     *   <li> <code>out != null</code>     *   <li> <code>inOffset >= 0</code>     *   <li> <code>(long)inOffset + BLOCK_SIZE <= in.length</code>     *   <li> <code>outOffset >= 0</code>     *   <li> <code>(long)outOffset + BLOCK_SIZE <= out.length</code>     * </ul>     *     * @param  cookie       a valid reference to the native key structure. This     *                      value is set by the native library upon return from     *                      native_init() (see link() method at the top).     * @param  in           input array containing data to encrypt or decrypt     *                      depending on the value of the encrypt boolean parameter.     * @param  inOffset     index of where we should start reading from input.     * @param  out          output array containing data decrypted or encrypted     *                      depending on the value of the encrypt boolean parameter.     * @param  outOffset    index of where we should start writing to output.     * @param  encrypt      if true then encrypt, otherwise decrypt.     * @return the number of bytes crypted (always BLOCK_SIZE) or 0 if an error     *                      occurred.     */    private native int native_crypt(long cookie, byte[] in, int inOffset,                                    byte[] out, int outOffset, boolean encrypt);    /**     * Implements the Unix crypt(3) algorithm in native code.     *     * @param  cookie       a valid reference to the native key structure. This     *                      value is set by the native library upon return from     *                      native_init() (see link() method at the top).     * @param  E0           first 32 bits of input.     * @param  E1           second 32 bits of input.     */    private native int[] native_crypt3(long cookie, int E0, int E1);    /**     * Finalizes the native state for this object.     *     * @return a string if an error occurred or null otherwise.     */    private native String native_finalize();// DES constants and variables//...........................................................................    private static final int        ROUNDS = 16,                        // number of encryption/decryption rounds        BLOCK_SIZE = 8,                     // DES block size in bytes        KEY_LENGTH = 8,                     // DES key length in bytes        INTERNAL_KEY_LENGTH = ROUNDS * 2;   // number of elements in key schedule

⌨️ 快捷键说明

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