package applet; public class Des { protected static final int ENCRYPT = 0; protected static final int DECRYPT = 1; protected static int[] bytebit = { 128, 64, 32, 16, 8, 4, 2, 1 }; protected 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 }; protected static byte[] totrot = { 1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28 }; protected 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 }; protected static byte[] ip = { 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7, 56, 48, 40, 32, 24, 16, 8, 0, 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6 }; protected static byte[] ebit = { 31, 0, 1, 2, 3, 4, 3, 4, 5, 6, 7, 8, 7, 8, 9, 10, 11, 12, 11, 12, 13, 14, 15, 16, 15, 16, 17, 18, 19, 20, 19, 20, 21, 22, 23, 24, 23, 24, 25, 26, 27, 28, 27, 28, 29, 30, 31 }; protected static byte[][] Sn = { { 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, 4, 1, 14, 8, 13, 6, 2, 11, 5, 12, 9, 7, 3, 10, 5, 0, 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 }, { 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 }, { 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 }, { 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 }, { 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 }, { 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 }, { 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 }, { 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 } }; protected static byte[] p = { 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25 }; protected static byte[] ip_1 = { 40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31, 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29, 36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27, 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25 }; protected byte[] K; protected int[][] Kn; public Des(int[] ai) { this.K = new byte[8]; this.Kn = new int[16][3]; byte[] abyte0 = new byte[56]; byte[][] abyte1 = new byte[16][28]; byte[][] abyte2 = new byte[16][28]; for (int i = 0; i < 8; i++) { int j = ai[i]; int l = 128; for (int k1 = 0; k1 < 7; k1++) { j <<= 1; l ^= j & 0x80; } this.K[i] = (byte)(ai[i] & 0x7F | l); } for (int k = 0; k < 56; k++) { int i1 = pc1[k] >> 3; int l1 = pc1[k] & 0x7; abyte0[k] = (byte)((this.K[i1] & bytebit[l1]) != 0 ? 1 : 0); } for (int j1 = 0; j1 < 16; j1++) { for (int i2 = 0; i2 < 28; i2++) { abyte1[j1][i2] = abyte0[((i2 + totrot[j1]) % 28)]; abyte2[j1][i2] = abyte0[((i2 + totrot[j1]) % 28 + 28)]; } } for (int j2 = 0; j2 < 16; j2++) { for (int k2 = 0; k2 < 3; k2++) { this.Kn[j2][k2] = 0; } for (int l2 = 0; l2 < 48; l2++) if (pc2[l2] < 28) this.Kn[j2][(l2 >> 4)] |= abyte1[j2][pc2[l2]] << 15 - (l2 & 0xF); else this.Kn[j2][(l2 >> 4)] |= abyte2[j2][(pc2[l2] - 28)] << 15 - (l2 & 0xF); } } public int[] decrypt_block(int[] ai, int i) { int j = i > 0 ? i : i % 8 != 0 ? i + (8 - i % 8) : 8; long[] al = new long[2]; int[] ai1 = new int[j]; int k = 0; for (; i > 0; i -= 8) { int l = 0; al[0] = 0L; al[1] = 0L; for (; l < 8; l++) { al[(l >> 2)] |= (l < i ? ai[(l + k)] : 0) << 24 - ((l & 0x3) << 3); } long[] al1 = des_block(al, 1); for (int i1 = 0; i1 < 8; i1++) { ai1[(k + i1)] = (int)(al1[(i1 >> 2)] >> 24 - ((i1 & 0x3) << 3) & 0xFF); } k += 8; } return ai1; } public String decrypt_string(String s) { String s1 = ""; int i = s.length() / 2; int[] ai = new int[i]; for (int j = 0; j < i; j++) { ai[j] = Integer.valueOf(s.substring(j * 2, j * 2 + 2), 16).intValue(); } int[] ai1 = decrypt_block(ai, i); for (int k = 0; k < i; k++) { if (ai1[k] != 0) s1 = s1 + (char)ai1[k]; } return s1; } protected long[] des_block(long[] al, int i) { long[] al1 = new long[2]; long[] al2 = new long[17]; long[] al3 = new long[17]; int[] ai = new int[3]; int[] ai1 = new int[3]; byte[] abyte0 = new byte[8]; long tmp39_38 = 0L; al3[0] = tmp39_38; al2[0] = tmp39_38; for (int j = 0; j < 32; j++) { al2[0] |= ((al[(ip[j] >> 5)] & 1 << 31 - ip[j]) != 0L ? 1 : 0) << 31 - j; al3[0] |= ((al[(ip[(j + 32)] >> 5)] & 1 << 31 - (ip[(j + 32)] & 0x1F)) != 0L ? 1 : 0) << 31 - j; } for (int k = 1; k <= 16; k++) { for (int l = 0; l < 3; l++) { ai[l] = 0; } for (int i1 = 0; i1 < 48; i1++) { ai[(i1 >> 4)] |= ((al3[(k - 1)] & 1 << 31 - ebit[i1]) != 0L ? 1 : 0) << (15 - i1 & 0xF); } if (i == 0) { for (int j1 = 0; j1 < 3; j1++) ai[j1] ^= this.Kn[(k - 1)][j1]; } else { for (int k1 = 0; k1 < 3; k1++) { ai[k1] ^= this.Kn[(16 - k)][k1]; } } abyte0[0] = (byte)(ai1[0] >> 10); abyte0[1] = (byte)(ai1[0] >> 4 & 0x3F); abyte0[2] = (byte)((ai1[0] & 0xF) << 2 | ai1[1] >> 14); abyte0[3] = (byte)(ai1[1] >> 8 & 0x3F); abyte0[4] = (byte)(ai1[1] >> 2 & 0x3F); abyte0[5] = (byte)((ai1[1] & 0x3) << 4 | ai1[2] >> 12); abyte0[6] = (byte)(ai1[2] >> 6 & 0x3F); abyte0[7] = (byte)(ai1[2] & 0x3F); for (int i2 = 0; i2 < 8; i2++) abyte0[i2] = (byte)(abyte0[i2] >> 1 & 0xF | abyte0[i2] & 0x20 | (abyte0[i2] & 0x1) << 4); long l3; long l4 = l3 = 0L; for (int k2 = 0; k2 < 8; k2++) { l4 |= Sn[k2][abyte0[k2]] << 28 - k2 * 4; } for (int i3 = 0; i3 < 32; i3++) { l3 |= ((l4 & 1 << 32 - p[i3]) != 0L ? 1 : 0) << 31 - i3; } al2[k] = al3[(k - 1)]; al3[k] = (al2[(k - 1)] ^ l3); } long l1; long l2 = l1 = 0L; for (int j2 = 0; j2 < 32; j2++) { if (ip_1[j2] <= 32) l2 |= ((al3[16] & 1 << 32 - ip_1[j2]) != 0L ? 1 : 0) << 31 - j2; else l2 |= ((al2[16] & 1 << 32 - (ip_1[j2] - 32)) != 0L ? 1 : 0) << 31 - j2; if (ip_1[(j2 + 32)] <= 32) l1 |= ((al3[16] & 1 << 32 - ip_1[(j2 + 32)]) != 0L ? 1 : 0) << 31 - j2; else { l1 |= ((al2[16] & 1 << 32 - (ip_1[(j2 + 32)] - 32)) != 0L ? 1 : 0) << 31 - j2; } } al1[0] = l2; al1[1] = l1; return al1; } public int[] encrypt_block(int[] ai, int i) { int j = i > 0 ? i : i % 8 != 0 ? i + (8 - i % 8) : 8; long[] al = new long[2]; int[] ai1 = new int[j]; int k = 0; for (; i > 0; i -= 8) { int l = 0; al[0] = 0L; al[1] = 0L; for (; l < 8; l++) { al[(l >> 2)] |= (l < i ? ai[(l + k)] : 0) << 24 - ((l & 0x3) << 3); } long[] al1 = des_block(al, 0); for (int i1 = 0; i1 < 8; i1++) { ai1[(k + i1)] = (int)(al1[(i1 >> 2)] >> 24 - ((i1 & 0x3) << 3) & 0xFF); } k += 8; } return ai1; } public String encrypt_string(String s) { String s1 = ""; int i = s.length(); int j; int[] ai; if (i > 0) { int[] ai = new int[i]; int j = i > 0 ? i : i % 8 != 0 ? i + (8 - i % 8) : 8; for (int k = 0; k < i; k++) ai[k] = s.charAt(k); } else { i = 1; j = 8; ai = new int[1]; ai[0] = 0; } int[] ai1 = encrypt_block(ai, i); for (int l = 0; l < j; l++) { if (ai1[l] < 16) s1 = s1 + "0" + Integer.toHexString(ai1[l]); else s1 = s1 + Integer.toHexString(ai1[l]); } return s1.toUpperCase(); } }