📄 sample.txt
字号:
protected int engineBlockSize() { return BLOCK_SIZE; } /** * <b>SPI</b>: Initializes this cipher for encryption, using the * specified key. * * @param key the key to use for encryption. * @exception InvalidKeyException if one of the following occurs: <ul> * <li> key.getEncoded() == null; * <li> The length of the user key array is outside the * permissible limits. * </ul> * @exception CryptixException if a self-test fails. */ protected void engineInitEncrypt(Key key) throws InvalidKeyException, CryptixException { makeKey(key); } /** * <b>SPI</b>: Initializes this cipher for decryption, using the * specified key. * * @param key the key to use for decryption. * @exception InvalidKeyException if one of the following occurs: <ul> * <li> key.getEncoded() == null; * <li> The length of the user key array is outside the * permissible limits. * </ul> * @exception CryptixException if a self-test fails. */ protected void engineInitDecrypt(Key key) throws InvalidKeyException, CryptixException { makeKey(key); } /** * <b>SPI</b>: This is the main engine method for updating data. * <p> * <i>in</i> and <i>out</i> may be the same array, and the input and output * regions may overlap. * * @param in the input data. * @param inOffset the offset into in specifying where the data starts. * @param inLen the length of the subarray. * @param out the output array. * @param outOffset the offset indicating where to start writing into * the out array. * @return the number of bytes written. * @exception CryptixException if the native library is being used, and it * reports an error. */ protected int engineUpdate(byte[] in, int inOffset, int inLen, byte[] out, int outOffset) { if (inLen < 0) throw new IllegalArgumentException("inLen < 0"); int blockCount = inLen / BLOCK_SIZE; inLen = blockCount * BLOCK_SIZE; boolean doEncrypt = (getState() == ENCRYPT); // Avoid overlapping input and output regions. if (in == out && (outOffset >= inOffset && outOffset < (long)inOffset+inLen || inOffset >= outOffset && inOffset < (long)outOffset+inLen)) { byte[] newin = new byte[inLen]; System.arraycopy(in, inOffset, newin, 0, inLen); in = newin; inOffset = 0; } if (native_lock != null) { synchronized(native_lock) { // If in == null || out == 0, evaluating their lengths will throw a // NullPointerException. if (inOffset < 0 || (long)inOffset + inLen > in.length || outOffset < 0 || (long)outOffset + inLen > out.length) throw new ArrayIndexOutOfBoundsException(getAlgorithm() + ": Arguments to native_crypt would cause a buffer overflow"); // In future, we may pass more than one block to native_crypt to reduce // native method overhead. for (int i = 0; i < blockCount; i++) { if (0 == native_crypt(native_cookie, in, inOffset, out, outOffset, doEncrypt)) throw new CryptixException(getAlgorithm() + ": Error in native code"); inOffset += BLOCK_SIZE; outOffset += BLOCK_SIZE; } } } else if (doEncrypt) { // state == ENCRYPT for (int i = 0; i < blockCount; i++) { blockEncrypt(in, inOffset, out, outOffset); inOffset += BLOCK_SIZE; outOffset += BLOCK_SIZE; } } else { // state == DECRYPT for (int i = 0; i < blockCount; i++) { blockDecrypt(in, inOffset, out, outOffset); inOffset += BLOCK_SIZE; outOffset += BLOCK_SIZE; } } return inLen; }// Own methods//........................................................................... /** * The normal entry to the encryption process. It is guaranteed * to be called with enough bytes in the input to carry on an * encryption of one full block. * * @param in an array containing the plaintext block * @param inOffset the starting offset of the plaintext block * @param out an array containing the ciphertext block * @param inOffset the starting offset of the ciphertext block */ private void blockEncrypt(byte[] in, int inOffset, byte[] out, int outOffset) { ... } /** * The normal entry to the decryption process. It is guaranteed * to be called with enough bytes in the input to carry on a * decryption of one full block. * * @param in an array containing the ciphertext block * @param inOffset the starting offset of the ciphertext block * @param out an array containing the plaintext block * @param inOffset the starting offset of the plaintext block */ private void blockDecrypt(byte[] in, int inOffset, byte[] out, int outOffset) { ... } /** * Expands a user-key to a working key schedule. * <p> * [Description of how key schedule is used. E.g.: * The sample algorithm uses a single key schedule for both encryption * and decryption. The process (key byte values and algorithm formulae) * are used in one direction during encryption and simply reversed * during decryption.] * * @param key the user-key object to use. * @exception InvalidKeyException if one of the following occurs: <ul> * <li> key.getEncoded() == null; * <li> The length of the user key array is outside the * permissible limits. * </ul> * @exception CryptixException if a self-test fails. */ private void makeKey(Key key) throws InvalidKeyException, CryptixException { byte[] userkey = key.getEncoded(); if (userkey == null) throw new InvalidKeyException(getAlgorithm() + ": Null user key"); int len = userkey.length; if (len < MIN_USER_KEY_LENGTH || len > MAX_USER_KEY_LENGTH) throw new InvalidKeyException(getAlgorithm() + ": Invalid user key length"); // If native library available then use it. If not or if // native method returned error then revert to 100% Java. if (native_lock != null) { synchronized(native_lock) { try { linkStatus.check(native_ks(native_cookie, userkey)); return; } catch (Error error) { native_finalize(); native_lock = null;if (DEBUG && debuglevel > 0) debug(error + ". Will use 100% Java."); } } } ... }// Test methods//...........................................................................//// Don't expand this code please without thinking about it,// much better to write a separate program.// /** * Entry point for <code>self_test</code>. */ public static void main(String[] args) { try { self_test(); } catch (Exception e) { e.printStackTrace(); } } /** * This is (apparently) the official certification data. * Use decimal as Java grumbles about hex values > 0x7F. */ private static final byte[][][] tests = { { // cert 1 { }, // key { }, // plain { } // cipher }, { // cert 2 { }, // key { }, // plain { } // cipher }, { // cert 3 { }, // key { }, // plain { } // cipher } }; /** * Do some basic tests. * Three of the certification data are included only, no output, * success or exception. * If you want more, write a test program! * * @see cryptix.test.Testsample */ private static void self_test() throws Exception { Cipher cryptor = Cipher.getInstance("sample", "Cryptix"); RawSecretKey userKey; byte[] tmp; for (int i = 0; i < tests.length; i++) { userKey = new RawSecretKey("sample", tests[i][0]); cryptor.initEncrypt(userKey); tmp = cryptor.crypt(tests[i][1]); if (!ArrayUtil.areEqual(tests[i][2], tmp)) throw new CryptixException("encrypt #"+ i +" failed"); cryptor.initDecrypt(userKey); tmp = cryptor.crypt(tests[i][2]); if (!ArrayUtil.areEqual(tests[i][1], tmp)) throw new CryptixException("decrypt #"+ i +" failed"); }if (DEBUG && debuglevel > 0) debug("Self-test OK"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -