📄 unixcrypt.java
字号:
for (int i=0; i<64; i++) { int k = IP[CIFP[i]-1]; if (k > 0) { k--; k = (k|0x07) - (k&0x07); k++; } perm[k-1] = (byte)(i+1); } init_perm(CF6464, perm, 8); // SPE table for (int i=0; i<48; i++) perm[i] = P32Tr[ExpandTr[i]-1]; for (int t=0; t<8; t++) { for (int j=0; j<64; j++) { int k = (((j >> 0) & 0x01) << 5) | (((j >> 1) & 0x01) << 3) | (((j >> 2) & 0x01) << 2) | (((j >> 3) & 0x01) << 1) | (((j >> 4) & 0x01) << 0) | (((j >> 5) & 0x01) << 4); k = S[t][k]; k = (((k >> 3) & 0x01) << 0) | (((k >> 2) & 0x01) << 1) | (((k >> 1) & 0x01) << 2) | (((k >> 0) & 0x01) << 3); for (int i=0; i<32; i++) temp[i] = 0; for (int i=0; i<4; i++) temp[4*t+i] = (byte)((k >> i) & 0x01); long kk = 0; for (int i=24; --i>=0; ) kk = ((kk<<1) | ((long)temp[perm[i]-1])<<32 | ((long)temp[perm[i+24]-1])); SPE[t][j] = to_six_bit(kk); } } } /** * You can't call the constructer. */ private UnixCrypt() { } /** * Returns the transposed and split code of a 24-bit code * into a 4-byte code, each having 6 bits. */ private static int to_six_bit(int num) { return (((num << 26) & 0xfc000000) | ((num << 12) & 0xfc0000) | ((num >> 2) & 0xfc00) | ((num >> 16) & 0xfc)); } /** * Returns the transposed and split code of two 24-bit code * into two 4-byte code, each having 6 bits. */ private static long to_six_bit(long num) { return (((num << 26) & 0xfc000000fc000000L) | ((num << 12) & 0xfc000000fc0000L) | ((num >> 2) & 0xfc000000fc00L) | ((num >> 16) & 0xfc000000fcL)); } /** * Returns the permutation of the given 64-bit code with * the specified permutataion table. */ private static long perm6464(long c, long[][]p) { long out = 0L; for (int i=8; --i>=0; ) { int t = (int)(0x00ff & c); c >>= 8; long tp = p[i<<1][t&0x0f]; out |= tp; tp = p[(i<<1)+1][t>>4]; out |= tp; } return out; } /** * Returns the permutation of the given 32-bit code with * the specified permutataion table. */ private static long perm3264(int c, long[][]p) { long out = 0L; for (int i=4; --i>=0; ) { int t = (0x00ff & c); c >>= 8; long tp = p[i<<1][t&0x0f]; out |= tp; tp = p[(i<<1)+1][t>>4]; out |= tp; } return out; } /** * Returns the key schedule for the given key. */ private static long[] des_setkey(long keyword) { long K = perm6464(keyword, PC1ROT); long[] KS = new long[16]; KS[0] = K&~0x0303030300000000L; for (int i=1; i<16; i++) { KS[i] = K; K = perm6464(K, PC2ROT[Rotates[i]-1]); KS[i] = K&~0x0303030300000000L; } return KS; } /** * Returns the DES encrypted code of the given word with the specified * environment. */ private static long des_cipher(long in, int salt, int num_iter, long[] KS) { salt = to_six_bit(salt); long L = in; long R = L; L &= 0x5555555555555555L; R = (R & 0xaaaaaaaa00000000L) | ((R >> 1) & 0x0000000055555555L); L = ((((L << 1) | (L << 32)) & 0xffffffff00000000L) | ((R | (R >> 32)) & 0x00000000ffffffffL)); L = perm3264((int)(L>>32), IE3264); R = perm3264((int)(L&0xffffffff), IE3264); while (--num_iter >= 0) { for (int loop_count=0; loop_count<8; loop_count++) { long kp; long B; long k; kp = KS[(loop_count<<1)]; k = ((R>>32) ^ R) & salt & 0xffffffffL; k |= (k<<32); B = (k ^ R ^ kp); L ^= (SPE[0][(int)((B>>58)&0x3f)] ^ SPE[1][(int)((B>>50)&0x3f)] ^ SPE[2][(int)((B>>42)&0x3f)] ^ SPE[3][(int)((B>>34)&0x3f)] ^ SPE[4][(int)((B>>26)&0x3f)] ^ SPE[5][(int)((B>>18)&0x3f)] ^ SPE[6][(int)((B>>10)&0x3f)] ^ SPE[7][(int)((B>>2)&0x3f)]); kp = KS[(loop_count<<1)+1]; k = ((L>>32) ^ L) & salt & 0xffffffffL; k |= (k<<32); B = (k ^ L ^ kp); R ^= (SPE[0][(int)((B>>58)&0x3f)] ^ SPE[1][(int)((B>>50)&0x3f)] ^ SPE[2][(int)((B>>42)&0x3f)] ^ SPE[3][(int)((B>>34)&0x3f)] ^ SPE[4][(int)((B>>26)&0x3f)] ^ SPE[5][(int)((B>>18)&0x3f)] ^ SPE[6][(int)((B>>10)&0x3f)] ^ SPE[7][(int)((B>>2)&0x3f)]); } // swap L and R L ^= R; R ^= L; L ^= R; } L = ((((L>>35) & 0x0f0f0f0fL) | (((L&0xffffffff)<<1) & 0xf0f0f0f0L))<<32 | (((R>>35) & 0x0f0f0f0fL) | (((R&0xffffffff)<<1) & 0xf0f0f0f0L))); L = perm6464(L, CF6464); return L; } /** * Initializes the given permutation table with the mapping table. */ private static void init_perm(long[][] perm, byte[] p,int chars_out) { for (int k=0; k<chars_out*8; k++) { int l = p[k] - 1; if (l < 0) continue; int i = l>>2; l = 1<<(l&0x03); for (int j=0; j<16; j++) { int s = ((k&0x07)+((7-(k>>3))<<3)); if ((j & l) != 0x00) perm[i][j] |= (1L<<s); } } } /** * Encrypts String into crypt (Unix) code. * @param key the key to be encrypted * @param setting the salt to be used * @return the encrypted String */ public static String crypt(String key, String setting) { long constdatablock = 0L; /* encryption constant */ byte[] cryptresult = new byte[13]; /* encrypted result */ long keyword = 0L; /* invalid parameters! */ if(key==null||setting==null) return "*"; // will NOT match under ANY circumstances! int keylen = key.length(); for (int i=0; i<8 ; i++) { keyword = (keyword << 8) | ((i < keylen)? 2*key.charAt(i): 0); } long[] KS = des_setkey(keyword); int salt = 0; for (int i=2; --i>=0;) { char c = (i < setting.length())? setting.charAt(i): '.'; cryptresult[i] = (byte)c; salt = (salt<<6) | (0x00ff&A64TOI[c]); } long rsltblock = des_cipher(constdatablock, salt, 25, KS); cryptresult[12] = ITOA64[(((int)rsltblock)<<2)&0x3f]; rsltblock >>= 4; for (int i=12; --i>=2; ) { cryptresult[i] = ITOA64[((int)rsltblock)&0x3f]; rsltblock >>= 6; } return new String(cryptresult, 0x00, 0, 13); } public static void main(String[] arg) { if (arg.length!=2) { System.err.println("Usage - java org.mortbay.util.UnixCrypt <key> <salt>"); System.exit(1); } System.err.println("Crypt="+crypt(arg[0],arg[1])); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -