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

📄 cast5engine.java

📁 《移动Agent技术》一书的所有章节源代码。
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		z47 = IntsTo32bits(z, 0x4);		z8B = IntsTo32bits(z, 0x8);		zCF = IntsTo32bits(z, 0xC);		x03 = z8B ^S5[z[0x5]] ^S6[z[0x7]] ^S7[z[0x4]] ^S8[z[0x6]] ^S7[z[0x0]];		Bits32ToInts(x03, x, 0x0);		x47 = z03 ^S5[x[0x0]] ^S6[x[0x2]] ^S7[x[0x1]] ^S8[x[0x3]] ^S8[z[0x2]];		Bits32ToInts(x47, x, 0x4);		x8B = z47 ^S5[x[0x7]] ^S6[x[0x6]] ^S7[x[0x5]] ^S8[x[0x4]] ^S5[z[0x1]];		Bits32ToInts(x8B, x, 0x8);		xCF = zCF ^S5[x[0xA]] ^S6[x[0x9]] ^S7[x[0xB]] ^S8[x[0x8]] ^S6[z[0x3]];		Bits32ToInts(xCF, x, 0xC);		_Kr[13]=(S5[x[0x8]]^S6[x[0x9]]^S7[x[0x7]]^S8[x[0x6]]^S5[x[0x3]])&0x1f;		_Kr[14]=(S5[x[0xA]]^S6[x[0xB]]^S7[x[0x5]]^S8[x[0x4]]^S6[x[0x7]])&0x1f;		_Kr[15]=(S5[x[0xC]]^S6[x[0xD]]^S7[x[0x3]]^S8[x[0x2]]^S7[x[0x8]])&0x1f;		_Kr[16]=(S5[x[0xE]]^S6[x[0xF]]^S7[x[0x1]]^S8[x[0x0]]^S8[x[0xD]])&0x1f;    }    /**     * Encrypt the given input starting at the given offset and place     * the result in the provided buffer starting at the given offset.	 *	 * @param src		The plaintext buffer	 * @param srcIndex	An offset into src	 * @param dst		The ciphertext buffer	 * @param dstIndex	An offset into dst     */    protected int encryptBlock(        byte[] src,         int srcIndex,        byte[] dst,        int dstIndex)    {        int  result[] = new int[2];        // process the input block         // batch the units up into a 32 bit chunk and go for it        // the array is in bytes, the increment is 8x8 bits = 64        int L0 = BytesTo32bits(src, srcIndex);        int R0 = BytesTo32bits(src, srcIndex + 4);        CAST_Encipher(L0, R0, result);        // now stuff them into the destination block        Bits32ToBytes(result[0], dst, dstIndex);        Bits32ToBytes(result[1], dst, dstIndex + 4);        return BLOCK_SIZE;    }    /**     * Decrypt the given input starting at the given offset and place     * the result in the provided buffer starting at the given offset.	 *	 * @param src		The plaintext buffer	 * @param srcIndex	An offset into src	 * @param dst		The ciphertext buffer	 * @param dstIndex	An offset into dst     */    protected int decryptBlock(        byte[] src,         int srcIndex,        byte[] dst,        int dstIndex)    {        int  result[] = new int[2];        // process the input block        // batch the units up into a 32 bit chunk and go for it        // the array is in bytes, the increment is 8x8 bits = 64        int L16 = BytesTo32bits(src, srcIndex);        int R16 = BytesTo32bits(src, srcIndex+4);        CAST_Decipher(L16, R16, result);        // now stuff them into the destination block        Bits32ToBytes(result[0], dst, dstIndex);        Bits32ToBytes(result[1], dst, dstIndex+4);        return BLOCK_SIZE;    }	/**	 * The first of the three processing functions for the	 * encryption and decryption.	 *	 * @param D			the input to be processed	 * @param Kmi		the mask to be used from Km[n]	 * @param Kri		the rotation value to be used	 *	 */    protected final int F1(int D, int Kmi, int Kri)    {		int I = Kmi + D;		I = I << Kri | I >>> (32-Kri);		return ((S1[(I>>>24)&0xff]^S2[(I>>>16)&0xff])-S3[(I>>> 8)&0xff])+				 S4[(I     )&0xff];    }	/**	 * The second of the three processing functions for the	 * encryption and decryption.	 *	 * @param D			the input to be processed	 * @param Kmi		the mask to be used from Km[n]	 * @param Kri		the rotation value to be used	 *	 */    protected final int F2(int D, int Kmi, int Kri)    {		int I = Kmi ^ D;		I = I << Kri | I >>> (32-Kri);		return ((S1[(I>>>24)&0xff]-S2[(I>>>16)&0xff])+S3[(I>>> 8)&0xff])^				 S4[(I     )&0xff];    }	/**	 * The third of the three processing functions for the	 * encryption and decryption.	 *	 * @param D			the input to be processed	 * @param Kmi		the mask to be used from Km[n]	 * @param Kri		the rotation value to be used	 *	 */    protected final int F3(int D, int Kmi, int Kri)    {		int I = Kmi - D;		I = I << Kri | I >>> (32-Kri);		return ((S1[(I>>>24)&0xff]+S2[(I>>>16)&0xff])^S3[(I>>> 8)&0xff])-				 S4[(I     )&0xff];    }	/**	 * Does the 16 rounds to encrypt the block.	 * 	 * @param L0	the LH-32bits of the plaintext block	 * @param R0	the RH-32bits of the plaintext block	 */    protected final void CAST_Encipher(int L0, int R0, int result[])    {		int Lp = L0;		// the previous value, equiv to L[i-1]		int Rp = R0;		// equivalent to R[i-1]		/* 		 * numbering consistent with paper to make		 * checking and validating easier		 */		int Li = L0, Ri = R0;		for (int i = 1; i<=_rounds ; i++)		{			Lp = Li;			Rp = Ri;			Li = Rp;			switch (i)			{				case  1:				case  4:				case  7:				case 10:				case 13:				case 16:					Ri = Lp ^ F1(Rp, _Km[i], _Kr[i]);					break;				case  2:				case  5:				case  8:				case 11:				case 14:					Ri = Lp ^ F2(Rp, _Km[i], _Kr[i]);					break;				case  3:				case  6:				case  9:				case 12:				case 15:					Ri = Lp ^ F3(Rp, _Km[i], _Kr[i]);					break;			}		}        result[0] = Ri;        result[1] = Li;        return;    }    protected final void CAST_Decipher(int L16, int R16, int result[])    {		int Lp = L16;		// the previous value, equiv to L[i-1]		int Rp = R16;		// equivalent to R[i-1]		/* 		 * numbering consistent with paper to make		 * checking and validating easier		 */		int Li = L16, Ri = R16;		for (int i = _rounds; i > 0; i--)		{			Lp = Li;			Rp = Ri;			Li = Rp;			switch (i)			{				case  1:				case  4:				case  7:				case 10:				case 13:				case 16:					Ri = Lp ^ F1(Rp, _Km[i], _Kr[i]);					break;				case  2:				case  5:				case  8:				case 11:				case 14:					Ri = Lp ^ F2(Rp, _Km[i], _Kr[i]);					break;				case  3:				case  6:				case  9:				case 12:				case 15:					Ri = Lp ^ F3(Rp, _Km[i], _Kr[i]);					break;			}		}        result[0] = Ri;        result[1] = Li;        return;    }	protected final void Bits32ToInts(int in,  int[] b, int offset)	{		b[offset + 3] = (in & 0xff);		b[offset + 2] = ((in >>> 8) & 0xff);		b[offset + 1] = ((in >>> 16) & 0xff);		b[offset]     = ((in >>> 24) & 0xff);	}    protected final int IntsTo32bits(int[] b, int i)    {        int rv = 0;        rv = ((b[i]   & 0xff) << 24) |              ((b[i+1] & 0xff) << 16) |             ((b[i+2] & 0xff) << 8) |             ((b[i+3] & 0xff));        return rv;	}	protected final void Bits32ToBytes(int in,  byte[] b, int offset)	{		b[offset + 3] = (byte)in;		b[offset + 2] = (byte)(in >>> 8);		b[offset + 1] = (byte)(in >>> 16);		b[offset]     = (byte)(in >>> 24);	}	protected final int BytesTo32bits(byte[] b, int i)	{		return ((b[i]   & 0xff) << 24) | 			((b[i+1] & 0xff) << 16) |			((b[i+2] & 0xff) << 8) |			((b[i+3] & 0xff));	}}

⌨️ 快捷键说明

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