📄 des.java
字号:
* @param input_start Offset into input to start encryption. * @param length Number of bytes to encrypt. * @param output Output byte [] array. * @param output_start Offset into output to place result. * @param ivec Initialization vector. A byte [] array of length 8. Updated on exit. * @param num Reference to an int used to keep track of 'how far' we are though ivec. Updated on exit. */ void ofb64_encrypt(byte [] input, int input_start, int length, byte [] output, int output_start, byte [] ivec, int [] num);}/** * Des class. Does all modes of Des encryption. * Written by Jeremy Allison (jra@cygnus.com) based on * C code from Eric Young (eay@mincom.oz.au). */class Des implements DesCrypt { /** * Constant. Pass this to functions that have a boolean * encrypt parameter to tell them to encrypt the data. */ public static final boolean ENCRYPT = true; /** * Constant. Pass this to functions that have a boolean * encrypt parameter to tell them to decrypt the data. */ public static final boolean DECRYPT = false; private DesKey ks_; private static final int des_SPtrans_[][] = { { /* nibble 0 */ 0x00820200, 0x00020000, 0x80800000, 0x80820200, 0x00800000, 0x80020200, 0x80020000, 0x80800000, 0x80020200, 0x00820200, 0x00820000, 0x80000200, 0x80800200, 0x00800000, 0x00000000, 0x80020000, 0x00020000, 0x80000000, 0x00800200, 0x00020200, 0x80820200, 0x00820000, 0x80000200, 0x00800200, 0x80000000, 0x00000200, 0x00020200, 0x80820000, 0x00000200, 0x80800200, 0x80820000, 0x00000000, 0x00000000, 0x80820200, 0x00800200, 0x80020000, 0x00820200, 0x00020000, 0x80000200, 0x00800200, 0x80820000, 0x00000200, 0x00020200, 0x80800000, 0x80020200, 0x80000000, 0x80800000, 0x00820000, 0x80820200, 0x00020200, 0x00820000, 0x80800200, 0x00800000, 0x80000200, 0x80020000, 0x00000000, 0x00020000, 0x00800000, 0x80800200, 0x00820200, 0x80000000, 0x80820000, 0x00000200, 0x80020200 },{ /* nibble 1 */ 0x10042004, 0x00000000, 0x00042000, 0x10040000, 0x10000004, 0x00002004, 0x10002000, 0x00042000, 0x00002000, 0x10040004, 0x00000004, 0x10002000, 0x00040004, 0x10042000, 0x10040000, 0x00000004, 0x00040000, 0x10002004, 0x10040004, 0x00002000, 0x00042004, 0x10000000, 0x00000000, 0x00040004, 0x10002004, 0x00042004, 0x10042000, 0x10000004, 0x10000000, 0x00040000, 0x00002004, 0x10042004, 0x00040004, 0x10042000, 0x10002000, 0x00042004, 0x10042004, 0x00040004, 0x10000004, 0x00000000, 0x10000000, 0x00002004, 0x00040000, 0x10040004, 0x00002000, 0x10000000, 0x00042004, 0x10002004, 0x10042000, 0x00002000, 0x00000000, 0x10000004, 0x00000004, 0x10042004, 0x00042000, 0x10040000, 0x10040004, 0x00040000, 0x00002004, 0x10002000, 0x10002004, 0x00000004, 0x10040000, 0x00042000 },{ /* nibble 2 */ 0x41000000, 0x01010040, 0x00000040, 0x41000040, 0x40010000, 0x01000000, 0x41000040, 0x00010040, 0x01000040, 0x00010000, 0x01010000, 0x40000000, 0x41010040, 0x40000040, 0x40000000, 0x41010000, 0x00000000, 0x40010000, 0x01010040, 0x00000040, 0x40000040, 0x41010040, 0x00010000, 0x41000000, 0x41010000, 0x01000040, 0x40010040, 0x01010000, 0x00010040, 0x00000000, 0x01000000, 0x40010040, 0x01010040, 0x00000040, 0x40000000, 0x00010000, 0x40000040, 0x40010000, 0x01010000, 0x41000040, 0x00000000, 0x01010040, 0x00010040, 0x41010000, 0x40010000, 0x01000000, 0x41010040, 0x40000000, 0x40010040, 0x41000000, 0x01000000, 0x41010040, 0x00010000, 0x01000040, 0x41000040, 0x00010040, 0x01000040, 0x00000000, 0x41010000, 0x40000040, 0x41000000, 0x40010040, 0x00000040, 0x01010000 },{ /* nibble 3 */ 0x00100402, 0x04000400, 0x00000002, 0x04100402, 0x00000000, 0x04100000, 0x04000402, 0x00100002, 0x04100400, 0x04000002, 0x04000000, 0x00000402, 0x04000002, 0x00100402, 0x00100000, 0x04000000, 0x04100002, 0x00100400, 0x00000400, 0x00000002, 0x00100400, 0x04000402, 0x04100000, 0x00000400, 0x00000402, 0x00000000, 0x00100002, 0x04100400, 0x04000400, 0x04100002, 0x04100402, 0x00100000, 0x04100002, 0x00000402, 0x00100000, 0x04000002, 0x00100400, 0x04000400, 0x00000002, 0x04100000, 0x04000402, 0x00000000, 0x00000400, 0x00100002, 0x00000000, 0x04100002, 0x04100400, 0x00000400, 0x04000000, 0x04100402, 0x00100402, 0x00100000, 0x04100402, 0x00000002, 0x04000400, 0x00100402, 0x00100002, 0x00100400, 0x04100000, 0x04000402, 0x00000402, 0x04000000, 0x04000002, 0x04100400 },{ /* nibble 4 */ 0x02000000, 0x00004000, 0x00000100, 0x02004108, 0x02004008, 0x02000100, 0x00004108, 0x02004000, 0x00004000, 0x00000008, 0x02000008, 0x00004100, 0x02000108, 0x02004008, 0x02004100, 0x00000000, 0x00004100, 0x02000000, 0x00004008, 0x00000108, 0x02000100, 0x00004108, 0x00000000, 0x02000008, 0x00000008, 0x02000108, 0x02004108, 0x00004008, 0x02004000, 0x00000100, 0x00000108, 0x02004100, 0x02004100, 0x02000108, 0x00004008, 0x02004000, 0x00004000, 0x00000008, 0x02000008, 0x02000100, 0x02000000, 0x00004100, 0x02004108, 0x00000000, 0x00004108, 0x02000000, 0x00000100, 0x00004008, 0x02000108, 0x00000100, 0x00000000, 0x02004108, 0x02004008, 0x02004100, 0x00000108, 0x00004000, 0x00004100, 0x02004008, 0x02000100, 0x00000108, 0x00000008, 0x00004108, 0x02004000, 0x02000008 },{ /* nibble 5 */ 0x20000010, 0x00080010, 0x00000000, 0x20080800, 0x00080010, 0x00000800, 0x20000810, 0x00080000, 0x00000810, 0x20080810, 0x00080800, 0x20000000, 0x20000800, 0x20000010, 0x20080000, 0x00080810, 0x00080000, 0x20000810, 0x20080010, 0x00000000, 0x00000800, 0x00000010, 0x20080800, 0x20080010, 0x20080810, 0x20080000, 0x20000000, 0x00000810, 0x00000010, 0x00080800, 0x00080810, 0x20000800, 0x00000810, 0x20000000, 0x20000800, 0x00080810, 0x20080800, 0x00080010, 0x00000000, 0x20000800, 0x20000000, 0x00000800, 0x20080010, 0x00080000, 0x00080010, 0x20080810, 0x00080800, 0x00000010, 0x20080810, 0x00080800, 0x00080000, 0x20000810, 0x20000010, 0x20080000, 0x00080810, 0x00000000, 0x00000800, 0x20000010, 0x20000810, 0x20080800, 0x20080000, 0x00000810, 0x00000010, 0x20080010 },{ /* nibble 6 */ 0x00001000, 0x00000080, 0x00400080, 0x00400001, 0x00401081, 0x00001001, 0x00001080, 0x00000000, 0x00400000, 0x00400081, 0x00000081, 0x00401000, 0x00000001, 0x00401080, 0x00401000, 0x00000081, 0x00400081, 0x00001000, 0x00001001, 0x00401081, 0x00000000, 0x00400080, 0x00400001, 0x00001080, 0x00401001, 0x00001081, 0x00401080, 0x00000001, 0x00001081, 0x00401001, 0x00000080, 0x00400000, 0x00001081, 0x00401000, 0x00401001, 0x00000081, 0x00001000, 0x00000080, 0x00400000, 0x00401001, 0x00400081, 0x00001081, 0x00001080, 0x00000000, 0x00000080, 0x00400001, 0x00000001, 0x00400080, 0x00000000, 0x00400081, 0x00400080, 0x00001080, 0x00000081, 0x00001000, 0x00401081, 0x00400000, 0x00401080, 0x00000001, 0x00001001, 0x00401081, 0x00400001, 0x00401080, 0x00401000, 0x00001001 },{ /* nibble 7 */ 0x08200020, 0x08208000, 0x00008020, 0x00000000, 0x08008000, 0x00200020, 0x08200000, 0x08208020, 0x00000020, 0x08000000, 0x00208000, 0x00008020, 0x00208020, 0x08008020, 0x08000020, 0x08200000, 0x00008000, 0x00208020, 0x00200020, 0x08008000, 0x08208020, 0x08000020, 0x00000000, 0x00208000, 0x08000000, 0x00200000, 0x08008020, 0x08200020, 0x00200000, 0x00008000, 0x08208000, 0x00000020, 0x00200000, 0x00008000, 0x08000020, 0x08208020, 0x00008020, 0x08000000, 0x00000000, 0x00208000, 0x08200020, 0x08008020, 0x08008000, 0x00200020, 0x08208000, 0x00000020, 0x00200020, 0x08008000, 0x08208020, 0x00200000, 0x08200000, 0x08000020, 0x00208000, 0x00008020, 0x08008020, 0x08200000, 0x00000020, 0x08208000, 0x00208020, 0x00000000, 0x08000000, 0x08200020, 0x00008000, 0x00208020 } }; /** * Internal routine only exported for use by TripleDes code. */ /* * Initial permutation. * #define IP(l,r) \ { \ register DES_LONG tt; \ PERM_OP(r,l,tt, 4,0x0f0f0f0fL); \ PERM_OP(l,r,tt,16,0x0000ffffL); \ PERM_OP(r,l,tt, 2,0x33333333L); \ PERM_OP(l,r,tt, 8,0x00ff00ffL); \ PERM_OP(r,l,tt, 1,0x55555555L); \ } */ public void IP(int [] ref_to_l, int [] ref_to_r) { int tt = 0; Int32Manipulator.PERM_OP(ref_to_r, ref_to_l, tt, 4, 0x0f0f0f0f); Int32Manipulator.PERM_OP(ref_to_l, ref_to_r, tt, 16, 0x0000ffff); Int32Manipulator.PERM_OP(ref_to_r, ref_to_l, tt, 2, 0x33333333); Int32Manipulator.PERM_OP(ref_to_l, ref_to_r, tt, 8, 0x00ff00ff); Int32Manipulator.PERM_OP(ref_to_r, ref_to_l, tt, 1, 0x55555555); } /** * Internal routine only exported for use by TripleDes code. */ /* * Final permutation. * #define FP(l,r) \ { \ register DES_LONG tt; \ PERM_OP(l,r,tt, 1,0x55555555L); \ PERM_OP(r,l,tt, 8,0x00ff00ffL); \ PERM_OP(l,r,tt, 2,0x33333333L); \ PERM_OP(r,l,tt,16,0x0000ffffL); \ PERM_OP(l,r,tt, 4,0x0f0f0f0fL); \ } */ public void FP(int [] ref_to_l, int [] ref_to_r) { int tt = 0; Int32Manipulator.PERM_OP(ref_to_l, ref_to_r, tt, 1, 0x55555555); Int32Manipulator.PERM_OP(ref_to_r, ref_to_l, tt, 8, 0x00ff00ff); Int32Manipulator.PERM_OP(ref_to_l, ref_to_r, tt, 2, 0x33333333); Int32Manipulator.PERM_OP(ref_to_r, ref_to_l, tt,16, 0x0000ffff); Int32Manipulator.PERM_OP(ref_to_l, ref_to_r, tt, 4, 0x0f0f0f0f); } /* #define D_ENCRYPT(Q,R,S) {\ u=(R^s[S ]); \ t=R^s[S+1]; \ t=((t>>4L)+(t<<28L)); \ Q^= des_SPtrans[1][(t )&0x3f]| \ des_SPtrans[3][(t>> 8L)&0x3f]| \ des_SPtrans[5][(t>>16L)&0x3f]| \ des_SPtrans[7][(t>>24L)&0x3f]| \ des_SPtrans[0][(u )&0x3f]| \ des_SPtrans[2][(u>> 8L)&0x3f]| \ des_SPtrans[4][(u>>16L)&0x3f]| \ des_SPtrans[6][(u>>24L)&0x3f]; } */ private void D_ENCRYPT(int [] ref_to_Q, int R, int S, int [] ref_to_u) { byte [] s = ks_.get_keysced(); S = S*4; /* Remember, S is a int offset, int C */ int s_at_S_offset = Int32Manipulator.bytes_to_int(s, S); int s_at_S_plus_one_offset = Int32Manipulator.bytes_to_int(s, S+4); ref_to_u[0] = R^s_at_S_offset; int tmp = R^s_at_S_plus_one_offset; tmp = (tmp>>>4) + (tmp<<28); ref_to_Q[0] ^= des_SPtrans_[1][(tmp )&0x3f]| des_SPtrans_[3][(tmp>>>8 )&0x3f]| des_SPtrans_[5][(tmp>>>16 )&0x3f]| des_SPtrans_[7][(tmp>>>24 )&0x3f]| des_SPtrans_[0][(ref_to_u[0] )&0x3f]| des_SPtrans_[2][(ref_to_u[0]>>> 8)&0x3f]| des_SPtrans_[4][(ref_to_u[0]>>>16)&0x3f]| des_SPtrans_[6][(ref_to_u[0]>>>24)&0x3f]; } /** * Constructor for a Des Object. Takes an already * existing DesKey which will be used for all future * encryption/decryption with this Des object. * The passed in key is not checked for parity of weakness. * * @param key Existing DesKey. */ public Des(DesKey key) { ks_ = key; } /* * This is the internal encrypt routine, called by all the * other des encrypt/decrypt functions. It is not callable * outside the Des class. */ private void des_encrypt(int [] data, boolean encrypt) { int tmp = 0; int [] ref_to_u = new int [1]; int [] ref_to_r = new int [1]; int [] ref_to_l = new int [1]; ref_to_u[0] = data[0]; ref_to_r[0] = data[1]; IP(ref_to_u, ref_to_r); ref_to_l[0]=(ref_to_r[0]<<1)|(ref_to_r[0]>>>31); ref_to_r[0]=(ref_to_u[0]<<1)|(ref_to_u[0]>>>31); if (encrypt == Des.ENCRYPT) { for (int i = 0; i < 32; i+=4) { D_ENCRYPT(ref_to_l,ref_to_r[0],i+0, ref_to_u); /* 1 */ D_ENCRYPT(ref_to_r,ref_to_l[0],i+2, ref_to_u); /* 2 */ } } else { /* Decrypt */ for (int i = 30; i > 0; i-=4) { D_ENCRYPT(ref_to_l,ref_to_r[0],i-0, ref_to_u); /* 16 */ D_ENCRYPT(ref_to_r,ref_to_l[0],i-2, ref_to_u); /* 15 */ } } ref_to_l[0] = (ref_to_l[0]>>>1)|(ref_to_l[0]<<31); ref_to_r[0] = (ref_to_r[0]>>>1)|(ref_to_r[0]<<31); FP(ref_to_r, ref_to_l); data[0]=ref_to_l[0]; data[1]=ref_to_r[0]; } /** * This is the a publicly callable variant of the * internal encrypt routine, called by all the * TripleDes encrypt/decrypt functions. The only difference * between this and the des_encrypt function is that this * doesn't do the initial and final permutations. This * optimises the speed of TripleDes as * IP() des_encrypt2() des_encrypt2() des_encrypt2() FP() * is the same as * des_encrypt() des_encrypt() des_encrypt() * except faster :-). * * No parameters listed as this is only used by the TripleDes * class. */ public void des_encrypt2(int [] data, boolean encrypt) { int tmp = 0; int [] ref_to_u = new int [1]; int [] ref_to_r = new int [1]; int [] ref_to_l = new int [1]; ref_to_u[0] = data[0]; ref_to_r[0] = data[1]; ref_to_l[0]=(ref_to_r[0]<<1)|(ref_to_r[0]>>>31); ref_to_r[0]=(ref_to_u[0]<<1)|(ref_to_u[0]>>>31); if (encrypt == Des.ENCRYPT) { for (int i = 0; i < 32; i+=4) { D_ENCRYPT(ref_to_l,ref_to_r[0],i+0, ref_to_u); /* 1 */ D_ENCRYPT(ref_to_r,ref_to_l[0],i+2, ref_to_u); /* 2 */ } } else { /* Decrypt */ for (int i = 30; i > 0; i-=4) { D_ENCRYPT(ref_to_l,ref_to_r[0],i-0, ref_to_u); /* 16 */ D_ENCRYPT(ref_to_r,ref_to_l[0],i-2, ref_to_u); /* 15 */ } } ref_to_l[0] = (ref_to_l[0]>>>1)|(ref_to_l[0]<<31); ref_to_r[0] = (ref_to_r[0]>>>1)|(ref_to_r[0]<<31); data[0]=ref_to_l[0]; data[1]=ref_to_r[0]; } /** * Do the ecb (Encrypt/Decrypt 8 bytes electronic code book) * mode. Encrypts 8 bytes starting at offset input_start in
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -