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

📄 safer.java

📁 另一个使用java编写的加密通用算法包
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
// $Id: SAFER.java,v 1.7 1998/01/28 00:17:43 hopwood Exp $//// $Log: SAFER.java,v $// Revision 1.7  1998/01/28 00:17:43  hopwood// + Committed changes below.//// Revision 1.6.1  1998/01/28  hopwood// + Minor HTML comment fixes.//// Revision 1.6  1998/01/22 04:42:17  iang// + Added URL to V1.2 test kit, which includes C and tests.//// Revision 1.5  1997/11/29 04:42:56  hopwood// + Changes to engineUpdate method.//// Revision 1.4  1997/11/20 19:31:41  hopwood// + cryptix.util.* name changes.//// Revision 1.3.1  1997/11/15  David Hopwood// + Renamed "Rounds" parameter (in constructor) to "rounds".// + Fixed off-by-one error when checking argument to setRounds.// + Throw InvalidParameterException when the argument to setRounds is//   out of range.// + Constructor now calls setVariant and setRounds, instead of duplicating//   code.// + Added getRounds and getVariant methods.// + Simplified engineGetParameter (by calling getVariant when needed).// + Fixed documentation for the default number of rounds, when there is no//   entry in the properties file. The original comment said MAX_NOF_ROUNDS//   (13); should be SK128_DEFAULT_NOF_ROUNDS (10).//// Revision 1.3  1997/11/10 07:31:32  raif// + Added support for engineSet/GetParameter();// + Changed the signature of setVariant(int) to setVariant(String);//// Revision 1.1.1.1  1997/11/03 22:36:56  hopwood// + Imported to CVS (tagged as 'start').//// Revision 0.3.2.1  1997/10/26  David Hopwood// + Renamed "Variant" parameter to "variant".//// Revision 0.3.2.0  1997/09/06  David Hopwood// + Replaced "mode" with "variant" everywhere, to prevent confusion with//   encryption modes.// + Native linking and debugging brought up to date with Blowfish 0.3.2.0//   (hence the jump in version number).//// Revision 0.1.2.0  1997/07/14  R. Naffah// + Tested OK with and without SAFER.DLL;// + Added support for native library;// + Incorporated D. Hopwood framework for native library linking//   and debugging;// + Minor syntax improvements.//// Revision 0.1.1.1  1997/06/30  R. Naffah// + Minor changes for a slight performance improvement.//// $Endlog$/* * Copyright (c) 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 java.io.PrintWriter;import java.security.Cipher;import java.security.Key;import java.security.KeyException;import java.security.InvalidParameterException;import java.security.InvalidParameterTypeException;import java.security.NoSuchParameterException;import java.security.Security;import java.security.SymmetricCipher;/**  * A subclass of Cipher to implement the SAFER algorithm in Java. * <p> * SAFER (Secure And Fast Encryption Routine) is a block-cipher algorithm * developed by Prof. J.L. Massey at the Swiss Federal Institute of Technology. * SAFER is usable in four versions (referred to in this implementation as * VARIANTS): SAFER K-64, SAFER K-128, SAFER SK-64 and SAFER SK-128. The numerals * 64 and 128 stand for the length of the user-selected key, 'K' stands for the * original key schedule and 'SK' stands for the strengthened key schedule. * <p> * <b>References:</b> * <ol> *   <li> Massey, J.L., *        "SAFER K-64: A Byte-Oriented Block Ciphering Algorithm", pp. 1-17 in *        Fast Software Encryption (Ed. R. Anderson), *        Proceedings of the Cambridge Security Workshop, Cambridge, U.K., *        December 9-11, 1993,<br> *        <cite>Lecture Notes in Computer Science No. 809</cite>. *        Heidelberg and New York: Springer, 1994. *        <p> *   <li> Massey, J.L., *        "SAFER K-64: One Year Later", *        preliminary manuscript of a paper presented at the K. U. Leuven *        Workshop on Cryptographic Algorithms, December 14-16, 1994.<br> *        To be published in the Proceedings of this workshop by Springer. *        <p> *   <li> Massey, J.L., *        "Announcement of a Strengthened Key Schedule for the Cipher SAFER", *        Sept. 9, 1995, (see file 'SAFER_SK.TXT' included in the toolkit). *        <p> *   <li> Richard De Moliner &lt;demoliner@isi.ee.ethz.ch&gt; *        <a href="ftp://ftp.isi.ee.ethz.ch/pub/simpl/safer.V1.2.tar.Z"> *        SAFER toolkit V1.2</a> *        includes C implementation, additional notes, test data, test program. * </ol> * <p> * Ported to Java from public domain 'C' code latest revised on September * 9, 1995 by: * <blockquote> *     Richard De Moliner (demoliner@isi.ee.ethz.ch)<br> *     Signal and Information Processing Laboratory<br> *     Swiss Federal Institute of Technology<br> *     CH-8092 Z&uuml;rich, Switzerland. * </blockquote> * <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.7 $</b> * @author  Raif S. Naffah * @author  David Hopwood * @since   Cryptix 2.2.2 */public final class SAFER // 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("SAFER") : 0;    private static final PrintWriter err = DEBUG ? Debug.getOutput() : null;    private static void debug(String s) { err.println("SAFER: " + s); }// Native library linking methods and vars.//...........................................................................    private static NativeLink linkStatus = new NativeLink("SAFER", 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 session key(s)     * 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 functions 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.     * <em>What is the restriction on rounds?</em>     *     * @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  userKey1 a byte array representing the top 64 bits of the user     *                  key.     * @param  userKey2 a byte array representing the bottom 64 bits of the     *                  user key.     * @param  rounds   the number of rounds used.     * @param  strong   whether the strengthened key schedule is to be used.     * @return an error String, or null if there was no error     */    private native String native_ks(long cookie, byte[] userKey1, byte[] userKey2,                                    int rounds, boolean strong);    /**     * 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);    /**     * Finalizes the native state for this object.     *     * @return a string if an error occurred or null otherwise.     */    private native String native_finalize();// SAFER variables and constants//...........................................................................    public static final int        SK128_VARIANT = 0,            // use as default        SK64_VARIANT = 1,        K128_VARIANT = 2,        K64_VARIANT = 3;    private static final int        K64_DEFAULT_NOF_ROUNDS = 6,

⌨️ 快捷键说明

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