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

📄 mycrypt.java

📁 BBS-CS(天乙社区) v5.2.2源码版
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.laoer.bbscs.util;

/**
 * <p>Title: 天乙社区V5.0</p>
 * <p>Description: BBS-CS天乙社区V5.0</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: laoer.com</p>
 * @author 龚天乙
 * @version 5.0
 */

public class MyCrypt {

  class crypt_data {
    public int keysched[] = new int[32];
    public char sb[][] = new char[4][32768];
    /* end-of-aligment-critical-data */
    public char crypt_3_buf[] = new char[13]; //[14]
    public char current_salt[] = new char[2];
    public int current_saltbits;
    public int direction, initialized;
  };

  private static int pc1[] = {
      57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18,
      10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36,
      63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22,
      14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4
  };

  /*
   * How much to rotate each 28 bit half of the pc1 permutated
   *  56 bit key before using pc2 to give the i' key
   */
  private static int rots[] = {
      1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
  };

  /*
   * Permutation giving the key
   * of the i' DES round
   */
  private static int pc2[] = {
      14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10,
      23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2,
      41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48,
      44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32
  };

  /*
   * The E expansion table which selects
   * bits from the 32 bit intermediate result.
   */
  private static int esel[] = {
      32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9,
      8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17,
      16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25,
      24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1
  };

  /*
   * Permutation done on the
   * result of sbox lookups
   */
  private static int perm32[] = {
      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
  };

  /*
   * The sboxes
   */
  private static int sbox[][][] = {
      {
      {
      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, 15, 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}
  }
  };

  /*
   * This is the initial
   * permutation matrix
   */
  private static int initial_perm[] = {
      58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4,
      62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8,
      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
  };

  /*
   * This is the final
   * permutation matrix
   */
  private static int final_perm[] = {
      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
  };

  private static int BITMASK[] = {
      0x40000000, 0x20000000, 0x10000000, 0x08000000, 0x04000000, 0x02000000,
      0x01000000, 0x00800000, 0x00400000, 0x00200000, 0x00100000, 0x00080000,
      0x00004000, 0x00002000, 0x00001000, 0x00000800, 0x00000400, 0x00000200,
      0x00000100, 0x00000080, 0x00000040, 0x00000020, 0x00000010, 0x00000008
  };

  private static short bytemask[] = {
      0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01
  };

  private static int longmask[] = {
      0x80000000, 0x40000000, 0x20000000, 0x10000000,
      0x08000000, 0x04000000, 0x02000000, 0x01000000,
      0x00800000, 0x00400000, 0x00200000, 0x00100000,
      0x00080000, 0x00040000, 0x00020000, 0x00010000,
      0x00008000, 0x00004000, 0x00002000, 0x00001000,
      0x00000800, 0x00000400, 0x00000200, 0x00000100,
      0x00000080, 0x00000040, 0x00000020, 0x00000010,
      0x00000008, 0x00000004, 0x00000002, 0x00000001
  };

  private static int small_tables_initialized = 0;

  private crypt_data data = new crypt_data();
  private int res[] = new int[4];
  private static int do_pc1[][][] = new int[8][2][128];
  private static int do_pc2[][] = new int[8][128];
  private static int eperm32tab[][][] = new int[4][256][2];
  private static int efp[][][] = new int[16][64][2];

  private void copymem(char from[], char to[], int cnt) {
    int i = 0;
    while (i < from.length && cnt-- > 0) {
      to[i] = from[i];
      i++;
    }

    while (i++ < 8) {
      to[i] = 0;
    }
  }

  private void init_des() {
    int comes_from_bit;
    int bit, sg;
    int j;
    int mask1, mask2;
    int e_inverse[] = new int[64];
    int i1, j11, k1;

    if (small_tables_initialized == 0) {
      for (bit = 0; bit < 56; bit++) {
        comes_from_bit = pc1[bit] - 1;
        mask1 = bytemask[comes_from_bit % 8 + 1];
        mask2 = longmask[bit % 28 + 4];
        for (j = 0; j < 128; j++) {
          if ( (j & mask1) != 0) {
            do_pc1[comes_from_bit / 8][bit / 28][j] |= mask2;
          }
        }
      }

      for (bit = 0; bit < 48; bit++) {
        comes_from_bit = pc2[bit] - 1;
        mask1 = bytemask[comes_from_bit % 7 + 1];
        mask2 = BITMASK[bit % 24];
        for (j = 0; j < 128; j++) {
          if ( (j & mask1) != 0) {
            do_pc2[comes_from_bit / 7][j] |= mask2;
          }
        }
      }

      for (bit = 0; bit < 48; bit++) {
        int mask1_2, comes_from;
        comes_from = perm32[esel[bit] - 1] - 1;
        mask1_2 = bytemask[ (int) (comes_from % 8)];
        for (j = 256; j-- > 0; ) {
          if ( (j & mask1_2) != 0) {
            eperm32tab[ (int) (comes_from / 8)][j][bit / 24] |= BITMASK[bit %
                24];
          }
        }
      }

      for (i1 = 0; i1 < 4; i1++) {

        for (bit = 48; bit-- > 0; ) {
          e_inverse[esel[bit] - 1] = bit;
          e_inverse[esel[bit] - 1 + 32] = bit + 48;
        }
      }

      for (bit = 0; bit < 64; bit++) {
        int o_bit, o_long, word_value;
        int mask1_3, mask2_3;
        int comes_from_f_bit, comes_from_e_bit;
        int comes_from_word, bit_within_word;

        o_long = bit / 32;
        o_bit = bit % 32;

        comes_from_f_bit = final_perm[bit] - 1;
        comes_from_e_bit = e_inverse[comes_from_f_bit];
        comes_from_word = comes_from_e_bit / 6;
        bit_within_word = comes_from_e_bit % 6;

        mask1_3 = longmask[bit_within_word + 26];
        mask2_3 = longmask[o_bit];

        for (word_value = 64; word_value-- > 0; ) {
          if ( (word_value & mask1_3) != 0) {
            efp[comes_from_word][word_value][o_long] |= mask2_3;
          }
        }
      }
      small_tables_initialized = 1;
    }

    for (sg = 0; sg < 4; sg++) {
      int j1, j2;
      int s1, s2;

      for (j1 = 0; j1 < 64; j1++) {
        s1 = sbox[ (2 * sg)][ ( ( (j1) >>> 4) & 0x2) |
            ( (j1) & 0x1)][ ( (j1) >>> 1) & 0xf];

        for (j2 = 0; j2 < 64; j2++) {
          int to_permute;
          int inx;
          int value1, value2;

          s2 = sbox[ (2 * sg + 1)][ ( ( (j2) >>> 4) & 0x2) |
              ( (j2) & 0x1)][ ( (j2) >>> 1) & 0xf];
          to_permute = ( ( (int) s1 << 4) | (int) s2) << (24 - 8 * (int) sg);

          inx = ( (j1 << 6) | j2) << 1;
          inx = inx * 4;

          value1 = eperm32tab[0][ (int) ( (to_permute >> 24) & 0xff)][0];
          value2 = eperm32tab[0][ (int) ( (to_permute >> 24) & 0xff)][1];
          value1 |= eperm32tab[1][ (int) ( (to_permute >> 16) & 0xff)][0];
          value2 |= eperm32tab[1][ (int) ( (to_permute >> 16) & 0xff)][1];
          value1 |= eperm32tab[2][ (int) ( (to_permute >> 8) & 0xff)][0];
          value2 |= eperm32tab[2][ (int) ( (to_permute >> 8) & 0xff)][1];
          value1 |= eperm32tab[3][ (int) ( (to_permute) & 0xff)][0];
          value2 |= eperm32tab[3][ (int) ( (to_permute) & 0xff)][1];

          data.sb[sg][inx] = (char) ( (value1) & 0xff);
          data.sb[sg][inx + 1] = (char) ( (value1 >> 8) & 0xff);
          data.sb[sg][inx + 2] = (char) ( (value1 >> 16) & 0xff);
          data.sb[sg][inx + 3] = (char) ( (value1 >> 24) & 0xff);
          data.sb[sg][inx + 4] = (char) ( (value2) & 0xff);
          data.sb[sg][inx + 5] = (char) ( (value2 >> 8) & 0xff);
          data.sb[sg][inx + 6] = (char) ( (value2 >> 16) & 0xff);
          data.sb[sg][inx + 7] = (char) ( (value2 >> 24) & 0xff);
        }
      }
    }
    data.initialized++;
  }

  private void shuffle_sb(char buf[], int saltbits) {
    int j;
    int x;
    int i = 0;
    int value, i1, i2;

    for (j = 4096; j-- > 0; ) {
      i1 = buf[i] + (buf[i + 1] << 8) + (buf[i + 2] << 16) + (buf[i + 3] << 24);
      i2 = buf[i + 4] + (buf[i + 5] << 8) + (buf[i + 6] << 16) +
          (buf[i + 7] << 24);

      x = (i1 ^ i2) & saltbits;

      value = buf[i] + (buf[i + 1] << 8) + (buf[i + 2] << 16) +
          (buf[i + 3] << 24);
      value ^= x;
      buf[i] = (char) ( (value) & 0xff);
      buf[i + 1] = (char) ( (value >> 8) & 0xff);
      buf[i + 2] = (char) ( (value >> 16) & 0xff);
      buf[i + 3] = (char) ( (value >> 24) & 0xff);

      i += 4;

      value = buf[i] + (buf[i + 1] << 8) + (buf[i + 2] << 16) +
          (buf[i + 3] << 24);
      value ^= x;
      buf[i] = (char) ( (value) & 0xff);

⌨️ 快捷键说明

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