public classEncryptor {public static final String HUNDSUN_VERSION="@system 管理平台 @version 2.0.1 @lastModiDate @describe ";protected static final int BLOCK_SIZE = 8;private int[] workingKey = null;private byte[] userkey = null;/*** standard constructor.*/
public Encryptor(byte[] userkey) {this.userkey =userkey;
}/*** initialise a DES cipher.
*
*@paramforEncryption whether or not we are for encryption.
*@paramparams the parameters required to set up the cipher.
*@exceptionIllegalArgumentException if the params argument is
* inappropriate.*/
public void init(boolean encrypting, byte[] key) {if (key != null) {
workingKey=generateWorkingKey(encrypting, key);return;
}else{throw newIllegalArgumentException("invalid parameter passed to DES init - ");
}
}public intgetBlockSize() {returnBLOCK_SIZE;
}/***
it used to check the parameters and then call desfun()
*/public intprocessBlock(byte[] in,intinOff,byte[] out,intoutOff) {if (workingKey == null) {throw new IllegalStateException("DES engine not initialised");
}if ( (inOff + BLOCK_SIZE) >in.length) {throw new IllegalStateException("input buffer too short");
}if ( (outOff + BLOCK_SIZE) >out.length) {throw new IllegalStateException("output buffer too short");
}
desFunc(workingKey, in, inOff, out, outOff);returnBLOCK_SIZE;
}/*** what follows is mainly taken from "Applied Cryptography", by
* Bruce Schneier, however it also bears great resemblance to Richard
* Outerbridge's D3DES...*/
static short[] Df_Key ={0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67};static short[] bytebit ={0200, 0100, 040, 020, 010, 04, 02, 01};static int[] bigbyte ={0x800000, 0x400000, 0x200000, 0x100000,0x80000, 0x40000, 0x20000, 0x10000,0x8000, 0x4000, 0x2000, 0x1000,0x800, 0x400, 0x200, 0x100,0x80, 0x40, 0x20, 0x10,0x8, 0x4, 0x2, 0x1};/** Use the key schedule specified in the Standard (ANSI X3.92-1981).*/
static byte[] pc1 ={56, 48, 40, 32, 24, 16, 8, 0, 57, 49, 41, 33, 25, 17,9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35,62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21,13, 5, 60, 52, 44, 36, 28, 20, 12, 4, 27, 19, 11, 3};static byte[] totrot ={1, 2, 4, 6, 8, 10, 12, 14,15, 17, 19, 21, 23, 25, 27, 28};static byte[] pc2 ={13, 16, 10, 23, 0, 4, 2, 27, 14, 5, 20, 9,22, 18, 11, 3, 25, 7, 15, 6, 26, 19, 12, 1,40, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47,43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31};static int[] SP1 ={0x01010400, 0x00000000, 0x00010000, 0x01010404,0x01010004, 0x00010404, 0x00000004, 0x00010000,0x00000400, 0x01010400, 0x01010404, 0x00000400,0x01000404, 0x01010004, 0x01000000, 0x00000004,0x00000404, 0x01000400, 0x01000400, 0x00010400,0x00010400, 0x01010000, 0x01010000, 0x01000404,0x00010004, 0x01000004, 0x01000004, 0x00010004,0x00000000, 0x00000404, 0x00010404, 0x01000000,0x00010000, 0x01010404, 0x00000004, 0x01010000,0x01010400, 0x01000000, 0x01000000, 0x00000400,0x01010004, 0x00010000, 0x00010400, 0x01000004,0x00000400, 0x00000004, 0x01000404, 0x00010404,0x01010404, 0x00010004, 0x01010000, 0x01000404,0x01000004, 0x00000404, 0x00010404, 0x01010400,0x00000404, 0x0100040