zoukankan      html  css  js  c++  java
  • java 实现加密算法(在网上看的,保存)

    1. import java.util.ArrayList;
    2.  
      import java.util.List;
    3.  
       
    4.  
      /**
    5.  
      * DES加密/解密
    6.  
      *
    7.  
      * @Copyright Copyright (c) 2015
    8.  
      * @author liuyazhuang
    9.  
      * @see DESCore
    10.  
      */
    11.  
      public class Des {
    12.  
      public Des() {
    13.  
      }
    14.  
      public static void main(String[] args) {
    15.  
      Des desObj = new Des();
    16.  
      String key1 = "1";
    17.  
      String key2 = "2";
    18.  
      String key3 = "3";
    19.  
      String data = "admin";
    20.  
      String str = desObj.strEnc(data, key1, key2, key3);
    21.  
      System.out.println(str);
    22.  
      String dec = desObj.strDec(str, key1, key2, key3);
    23.  
      System.out.println(dec);
    24.  
      }
    25.  
       
    26.  
      /**
    27.  
      * DES加密/解密
    28.  
      *
    29.  
      * @Copyright Copyright (c) 2015
    30.  
      * @author liuyazhuang
    31.  
      * @see DESCore
    32.  
      */
    33.  
       
    34.  
      /*
    35.  
      * encrypt the string to string made up of hex return the encrypted string
    36.  
      */
    37.  
      public String strEnc(String data, String firstKey, String secondKey,
    38.  
      String thirdKey) {
    39.  
       
    40.  
      int leng = data.length();
    41.  
      String encData = "";
    42.  
      List firstKeyBt = null, secondKeyBt = null, thirdKeyBt = null;
    43.  
      int firstLength = 0, secondLength = 0, thirdLength = 0;
    44.  
      if (firstKey != null && firstKey != "") {
    45.  
      firstKeyBt = getKeyBytes(firstKey);
    46.  
      firstLength = firstKeyBt.size();
    47.  
      }
    48.  
      if (secondKey != null && secondKey != "") {
    49.  
      secondKeyBt = getKeyBytes(secondKey);
    50.  
      secondLength = secondKeyBt.size();
    51.  
      }
    52.  
      if (thirdKey != null && thirdKey != "") {
    53.  
      thirdKeyBt = getKeyBytes(thirdKey);
    54.  
      thirdLength = thirdKeyBt.size();
    55.  
      }
    56.  
       
    57.  
      if (leng > 0) {
    58.  
      if (leng < 4) {
    59.  
      int[] bt = strToBt(data);
    60.  
      int[] encByte = null;
    61.  
      if (firstKey != null && firstKey != "" && secondKey != null
    62.  
      && secondKey != "" && thirdKey != null
    63.  
      && thirdKey != "") {
    64.  
      int[] tempBt;
    65.  
      int x, y, z;
    66.  
      tempBt = bt;
    67.  
      for (x = 0; x < firstLength; x++) {
    68.  
      tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
    69.  
      }
    70.  
      for (y = 0; y < secondLength; y++) {
    71.  
      tempBt = enc(tempBt, (int[]) secondKeyBt.get(y));
    72.  
      }
    73.  
      for (z = 0; z < thirdLength; z++) {
    74.  
      tempBt = enc(tempBt, (int[]) thirdKeyBt.get(z));
    75.  
      }
    76.  
      encByte = tempBt;
    77.  
      } else {
    78.  
      if (firstKey != null && firstKey != "" && secondKey != null
    79.  
      && secondKey != "") {
    80.  
      int[] tempBt;
    81.  
      int x, y;
    82.  
      tempBt = bt;
    83.  
      for (x = 0; x < firstLength; x++) {
    84.  
      tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
    85.  
      }
    86.  
      for (y = 0; y < secondLength; y++) {
    87.  
      tempBt = enc(tempBt, (int[]) secondKeyBt.get(y));
    88.  
      }
    89.  
      encByte = tempBt;
    90.  
      } else {
    91.  
      if (firstKey != null && firstKey != "") {
    92.  
      int[] tempBt;
    93.  
      int x = 0;
    94.  
      tempBt = bt;
    95.  
      for (x = 0; x < firstLength; x++) {
    96.  
      tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
    97.  
      }
    98.  
      encByte = tempBt;
    99.  
      }
    100.  
      }
    101.  
      }
    102.  
      encData = bt64ToHex(encByte);
    103.  
      } else {
    104.  
      int iterator = (leng / 4);
    105.  
      int remainder = leng % 4;
    106.  
      int i = 0;
    107.  
      for (i = 0; i < iterator; i++) {
    108.  
      String tempData = data.substring(i * 4 + 0, i * 4 + 4);
    109.  
      int[] tempByte = strToBt(tempData);
    110.  
      int[] encByte = null;
    111.  
      if (firstKey != null && firstKey != "" && secondKey != null
    112.  
      && secondKey != "" && thirdKey != null
    113.  
      && thirdKey != "") {
    114.  
      int[] tempBt;
    115.  
      int x, y, z;
    116.  
      tempBt = tempByte;
    117.  
      for (x = 0; x < firstLength; x++) {
    118.  
      tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
    119.  
      }
    120.  
      for (y = 0; y < secondLength; y++) {
    121.  
      tempBt = enc(tempBt, (int[]) secondKeyBt.get(y));
    122.  
      }
    123.  
      for (z = 0; z < thirdLength; z++) {
    124.  
      tempBt = enc(tempBt, (int[]) thirdKeyBt.get(z));
    125.  
      }
    126.  
      encByte = tempBt;
    127.  
      } else {
    128.  
      if (firstKey != null && firstKey != ""
    129.  
      && secondKey != null && secondKey != "") {
    130.  
      int[] tempBt;
    131.  
      int x, y;
    132.  
      tempBt = tempByte;
    133.  
      for (x = 0; x < firstLength; x++) {
    134.  
      tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
    135.  
      }
    136.  
      for (y = 0; y < secondLength; y++) {
    137.  
      tempBt = enc(tempBt, (int[]) secondKeyBt.get(y));
    138.  
      }
    139.  
      encByte = tempBt;
    140.  
      } else {
    141.  
      if (firstKey != null && firstKey != "") {
    142.  
      int[] tempBt;
    143.  
      int x;
    144.  
      tempBt = tempByte;
    145.  
      for (x = 0; x < firstLength; x++) {
    146.  
      tempBt = enc(tempBt, (int[]) firstKeyBt
    147.  
      .get(x));
    148.  
      }
    149.  
      encByte = tempBt;
    150.  
      }
    151.  
      }
    152.  
      }
    153.  
      encData += bt64ToHex(encByte);
    154.  
      }
    155.  
      if (remainder > 0) {
    156.  
      String remainderData = data.substring(iterator * 4 + 0,
    157.  
      leng);
    158.  
      int[] tempByte = strToBt(remainderData);
    159.  
      int[] encByte = null;
    160.  
      if (firstKey != null && firstKey != "" && secondKey != null
    161.  
      && secondKey != "" && thirdKey != null
    162.  
      && thirdKey != "") {
    163.  
      int[] tempBt;
    164.  
      int x, y, z;
    165.  
      tempBt = tempByte;
    166.  
      for (x = 0; x < firstLength; x++) {
    167.  
      tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
    168.  
      }
    169.  
      for (y = 0; y < secondLength; y++) {
    170.  
      tempBt = enc(tempBt, (int[]) secondKeyBt.get(y));
    171.  
      }
    172.  
      for (z = 0; z < thirdLength; z++) {
    173.  
      tempBt = enc(tempBt, (int[]) thirdKeyBt.get(z));
    174.  
      }
    175.  
      encByte = tempBt;
    176.  
      } else {
    177.  
      if (firstKey != null && firstKey != ""
    178.  
      && secondKey != null && secondKey != "") {
    179.  
      int[] tempBt;
    180.  
      int x, y;
    181.  
      tempBt = tempByte;
    182.  
      for (x = 0; x < firstLength; x++) {
    183.  
      tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
    184.  
      }
    185.  
      for (y = 0; y < secondLength; y++) {
    186.  
      tempBt = enc(tempBt, (int[]) secondKeyBt.get(y));
    187.  
      }
    188.  
      encByte = tempBt;
    189.  
      } else {
    190.  
      if (firstKey != null && firstKey != "") {
    191.  
      int[] tempBt;
    192.  
      int x;
    193.  
      tempBt = tempByte;
    194.  
      for (x = 0; x < firstLength; x++) {
    195.  
      tempBt = enc(tempBt, (int[]) firstKeyBt
    196.  
      .get(x));
    197.  
      }
    198.  
      encByte = tempBt;
    199.  
      }
    200.  
      }
    201.  
      }
    202.  
      encData += bt64ToHex(encByte);
    203.  
      }
    204.  
      }
    205.  
      }
    206.  
      return encData;
    207.  
      }
    208.  
       
    209.  
      /*
    210.  
      * decrypt the encrypted string to the original string
    211.  
      * return the original string
    212.  
      */
    213.  
      public String strDec(String data, String firstKey, String secondKey,
    214.  
      String thirdKey) {
    215.  
      int leng = data.length();
    216.  
      String decStr = "";
    217.  
      List firstKeyBt = null, secondKeyBt = null, thirdKeyBt = null;
    218.  
      int firstLength = 0, secondLength = 0, thirdLength = 0;
    219.  
      if (firstKey != null && firstKey != "") {
    220.  
      firstKeyBt = getKeyBytes(firstKey);
    221.  
      firstLength = firstKeyBt.size();
    222.  
      }
    223.  
      if (secondKey != null && secondKey != "") {
    224.  
      secondKeyBt = getKeyBytes(secondKey);
    225.  
      secondLength = secondKeyBt.size();
    226.  
      }
    227.  
      if (thirdKey != null && thirdKey != "") {
    228.  
      thirdKeyBt = getKeyBytes(thirdKey);
    229.  
      thirdLength = thirdKeyBt.size();
    230.  
      }
    231.  
       
    232.  
      int iterator = leng / 16;
    233.  
      int i = 0;
    234.  
      for (i = 0; i < iterator; i++) {
    235.  
      String tempData = data.substring(i * 16 + 0, i * 16 + 16);
    236.  
      String strByte = hexToBt64(tempData);
    237.  
      int[] intByte = new int[64];
    238.  
      int j = 0;
    239.  
      for (j = 0; j < 64; j++) {
    240.  
      intByte[j] = Integer.parseInt(strByte.substring(j, j + 1));
    241.  
      }
    242.  
      int[] decByte = null;
    243.  
      if (firstKey != null && firstKey != "" && secondKey != null
    244.  
      && secondKey != "" && thirdKey != null && thirdKey != "") {
    245.  
      int[] tempBt;
    246.  
      int x, y, z;
    247.  
      tempBt = intByte;
    248.  
      for (x = thirdLength - 1; x >= 0; x--) {
    249.  
      tempBt = dec(tempBt, (int[]) thirdKeyBt.get(x));
    250.  
      }
    251.  
      for (y = secondLength - 1; y >= 0; y--) {
    252.  
      tempBt = dec(tempBt, (int[]) secondKeyBt.get(y));
    253.  
      }
    254.  
      for (z = firstLength - 1; z >= 0; z--) {
    255.  
      tempBt = dec(tempBt, (int[]) firstKeyBt.get(z));
    256.  
      }
    257.  
      decByte = tempBt;
    258.  
      } else {
    259.  
      if (firstKey != null && firstKey != "" && secondKey != null
    260.  
      && secondKey != "") {
    261.  
      int[] tempBt;
    262.  
      int x, y, z;
    263.  
      tempBt = intByte;
    264.  
      for (x = secondLength - 1; x >= 0; x--) {
    265.  
      tempBt = dec(tempBt, (int[]) secondKeyBt.get(x));
    266.  
      }
    267.  
      for (y = firstLength - 1; y >= 0; y--) {
    268.  
      tempBt = dec(tempBt, (int[]) firstKeyBt.get(y));
    269.  
      }
    270.  
      decByte = tempBt;
    271.  
      } else {
    272.  
      if (firstKey != null && firstKey != "") {
    273.  
      int[] tempBt;
    274.  
      int x, y, z;
    275.  
      tempBt = intByte;
    276.  
      for (x = firstLength - 1; x >= 0; x--) {
    277.  
      tempBt = dec(tempBt, (int[]) firstKeyBt.get(x));
    278.  
      }
    279.  
      decByte = tempBt;
    280.  
      }
    281.  
      }
    282.  
      }
    283.  
      decStr += byteToString(decByte);
    284.  
      }
    285.  
      return decStr;
    286.  
      }
    287.  
       
    288.  
      /*
    289.  
      * chang the string into the bit array
    290.  
      *
    291.  
      * return bit array(it's length % 64 = 0)
    292.  
      */
    293.  
      public List getKeyBytes(String key) {
    294.  
      List keyBytes = new ArrayList();
    295.  
      int leng = key.length();
    296.  
      int iterator = (leng / 4);
    297.  
      int remainder = leng % 4;
    298.  
      int i = 0;
    299.  
      for (i = 0; i < iterator; i++) {
    300.  
      keyBytes.add(i, strToBt(key.substring(i * 4 + 0, i * 4 + 4)));
    301.  
      }
    302.  
      if (remainder > 0) {
    303.  
      // keyBytes[i] = strToBt(key.substring(i*4+0,leng));
    304.  
      keyBytes.add(i, strToBt(key.substring(i * 4 + 0, leng)));
    305.  
      }
    306.  
      return keyBytes;
    307.  
      }
    308.  
       
    309.  
      /*
    310.  
      * chang the string(it's length <= 4) into the bit array
    311.  
      *
    312.  
      * return bit array(it's length = 64)
    313.  
      */
    314.  
      public int[] strToBt(String str) {
    315.  
      int leng = str.length();
    316.  
      int[] bt = new int[64];
    317.  
      if (leng < 4) {
    318.  
      int i = 0, j = 0, p = 0, q = 0;
    319.  
      for (i = 0; i < leng; i++) {
    320.  
      int k = str.charAt(i);
    321.  
      for (j = 0; j < 16; j++) {
    322.  
      int pow = 1, m = 0;
    323.  
      for (m = 15; m > j; m--) {
    324.  
      pow *= 2;
    325.  
      }
    326.  
      // bt.set(16*i+j,""+(k/pow)%2));
    327.  
      bt[16 * i + j] = (k / pow) % 2;
    328.  
      }
    329.  
      }
    330.  
      for (p = leng; p < 4; p++) {
    331.  
      int k = 0;
    332.  
      for (q = 0; q < 16; q++) {
    333.  
      int pow = 1, m = 0;
    334.  
      for (m = 15; m > q; m--) {
    335.  
      pow *= 2;
    336.  
      }
    337.  
      // bt[16*p+q]=parseInt(k/pow)%2;
    338.  
      // bt.add(16*p+q,""+((k/pow)%2));
    339.  
      bt[16 * p + q] = (k / pow) % 2;
    340.  
      }
    341.  
      }
    342.  
      } else {
    343.  
      for (int i = 0; i < 4; i++) {
    344.  
      int k = str.charAt(i);
    345.  
      for (int j = 0; j < 16; j++) {
    346.  
      int pow = 1;
    347.  
      for (int m = 15; m > j; m--) {
    348.  
      pow *= 2;
    349.  
      }
    350.  
      // bt[16*i+j]=parseInt(k/pow)%2;
    351.  
      // bt.add(16*i+j,""+((k/pow)%2));
    352.  
      bt[16 * i + j] = (k / pow) % 2;
    353.  
      }
    354.  
      }
    355.  
      }
    356.  
      return bt;
    357.  
      }
    358.  
       
    359.  
      /*
    360.  
      * chang the bit(it's length = 4) into the hex
    361.  
      *
    362.  
      * return hex
    363.  
      */
    364.  
      public String bt4ToHex(String binary) {
    365.  
      String hex = "";
    366.  
      if (binary.equalsIgnoreCase("0000")) {
    367.  
      hex = "0";
    368.  
      } else if (binary.equalsIgnoreCase("0001")) {
    369.  
      hex = "1";
    370.  
      } else if (binary.equalsIgnoreCase("0010")) {
    371.  
      hex = "2";
    372.  
      } else if (binary.equalsIgnoreCase("0011")) {
    373.  
      hex = "3";
    374.  
      } else if (binary.equalsIgnoreCase("0100")) {
    375.  
      hex = "4";
    376.  
      } else if (binary.equalsIgnoreCase("0101")) {
    377.  
      hex = "5";
    378.  
      } else if (binary.equalsIgnoreCase("0110")) {
    379.  
      hex = "6";
    380.  
      } else if (binary.equalsIgnoreCase("0111")) {
    381.  
      hex = "7";
    382.  
      } else if (binary.equalsIgnoreCase("1000")) {
    383.  
      hex = "8";
    384.  
      } else if (binary.equalsIgnoreCase("1001")) {
    385.  
      hex = "9";
    386.  
      } else if (binary.equalsIgnoreCase("1010")) {
    387.  
      hex = "A";
    388.  
      } else if (binary.equalsIgnoreCase("1011")) {
    389.  
      hex = "B";
    390.  
      } else if (binary.equalsIgnoreCase("1100")) {
    391.  
      hex = "C";
    392.  
      } else if (binary.equalsIgnoreCase("1101")) {
    393.  
      hex = "D";
    394.  
      } else if (binary.equalsIgnoreCase("1110")) {
    395.  
      hex = "E";
    396.  
      } else if (binary.equalsIgnoreCase("1111")) {
    397.  
      hex = "F";
    398.  
      }
    399.  
       
    400.  
      return hex;
    401.  
      }
    402.  
       
    403.  
      /*
    404.  
      * chang the hex into the bit(it's length = 4)
    405.  
      *
    406.  
      * return the bit(it's length = 4)
    407.  
      */
    408.  
      public String hexToBt4(String hex) {
    409.  
      String binary = "";
    410.  
      if (hex.equalsIgnoreCase("0")) {
    411.  
      binary = "0000";
    412.  
      } else if (hex.equalsIgnoreCase("1")) {
    413.  
      binary = "0001";
    414.  
      }
    415.  
      if (hex.equalsIgnoreCase("2")) {
    416.  
      binary = "0010";
    417.  
      }
    418.  
      if (hex.equalsIgnoreCase("3")) {
    419.  
      binary = "0011";
    420.  
      }
    421.  
      if (hex.equalsIgnoreCase("4")) {
    422.  
      binary = "0100";
    423.  
      }
    424.  
      if (hex.equalsIgnoreCase("5")) {
    425.  
      binary = "0101";
    426.  
      }
    427.  
      if (hex.equalsIgnoreCase("6")) {
    428.  
      binary = "0110";
    429.  
      }
    430.  
      if (hex.equalsIgnoreCase("7")) {
    431.  
      binary = "0111";
    432.  
      }
    433.  
      if (hex.equalsIgnoreCase("8")) {
    434.  
      binary = "1000";
    435.  
      }
    436.  
      if (hex.equalsIgnoreCase("9")) {
    437.  
      binary = "1001";
    438.  
      }
    439.  
      if (hex.equalsIgnoreCase("A")) {
    440.  
      binary = "1010";
    441.  
      }
    442.  
      if (hex.equalsIgnoreCase("B")) {
    443.  
      binary = "1011";
    444.  
      }
    445.  
      if (hex.equalsIgnoreCase("C")) {
    446.  
      binary = "1100";
    447.  
      }
    448.  
      if (hex.equalsIgnoreCase("D")) {
    449.  
      binary = "1101";
    450.  
      }
    451.  
      if (hex.equalsIgnoreCase("E")) {
    452.  
      binary = "1110";
    453.  
      }
    454.  
      if (hex.equalsIgnoreCase("F")) {
    455.  
      binary = "1111";
    456.  
      }
    457.  
      return binary;
    458.  
      }
    459.  
       
    460.  
      /*
    461.  
      * chang the bit(it's length = 64) into the string
    462.  
      *
    463.  
      * return string
    464.  
      */
    465.  
      public String byteToString(int[] byteData) {
    466.  
      String str = "";
    467.  
      for (int i = 0; i < 4; i++) {
    468.  
      int count = 0;
    469.  
      for (int j = 0; j < 16; j++) {
    470.  
      int pow = 1;
    471.  
      for (int m = 15; m > j; m--) {
    472.  
      pow *= 2;
    473.  
      }
    474.  
      count += byteData[16 * i + j] * pow;
    475.  
      }
    476.  
      if (count != 0) {
    477.  
      str += "" + (char) (count);
    478.  
      }
    479.  
      }
    480.  
      return str;
    481.  
      }
    482.  
       
    483.  
      public String bt64ToHex(int[] byteData) {
    484.  
      String hex = "";
    485.  
      for (int i = 0; i < 16; i++) {
    486.  
      String bt = "";
    487.  
      for (int j = 0; j < 4; j++) {
    488.  
      bt += byteData[i * 4 + j];
    489.  
      }
    490.  
      hex += bt4ToHex(bt);
    491.  
      }
    492.  
      return hex;
    493.  
      }
    494.  
       
    495.  
      public String hexToBt64(String hex) {
    496.  
      String binary = "";
    497.  
      for (int i = 0; i < 16; i++) {
    498.  
      binary += hexToBt4(hex.substring(i, i + 1));
    499.  
      }
    500.  
      return binary;
    501.  
      }
    502.  
       
    503.  
      /*
    504.  
      * the 64 bit des core arithmetic
    505.  
      */
    506.  
       
    507.  
      public int[] enc(int[] dataByte, int[] keyByte) {
    508.  
      int[][] keys = generateKeys(keyByte);
    509.  
      int[] ipByte = initPermute(dataByte);
    510.  
      int[] ipLeft = new int[32];
    511.  
      int[] ipRight = new int[32];
    512.  
      int[] tempLeft = new int[32];
    513.  
      int i = 0, j = 0, k = 0, m = 0, n = 0;
    514.  
      for (k = 0; k < 32; k++) {
    515.  
      ipLeft[k] = ipByte[k];
    516.  
      ipRight[k] = ipByte[32 + k];
    517.  
      }
    518.  
      for (i = 0; i < 16; i++) {
    519.  
      for (j = 0; j < 32; j++) {
    520.  
      tempLeft[j] = ipLeft[j];
    521.  
      ipLeft[j] = ipRight[j];
    522.  
      }
    523.  
      int[] key = new int[48];
    524.  
      for (m = 0; m < 48; m++) {
    525.  
      key[m] = keys[i][m];
    526.  
      }
    527.  
      int[] tempRight = xor(pPermute(sBoxPermute(xor(
    528.  
      expandPermute(ipRight), key))), tempLeft);
    529.  
      for (n = 0; n < 32; n++) {
    530.  
      ipRight[n] = tempRight[n];
    531.  
      }
    532.  
       
    533.  
      }
    534.  
       
    535.  
      int[] finalData = new int[64];
    536.  
      for (i = 0; i < 32; i++) {
    537.  
      finalData[i] = ipRight[i];
    538.  
      finalData[32 + i] = ipLeft[i];
    539.  
      }
    540.  
      return finallyPermute(finalData);
    541.  
      }
    542.  
       
    543.  
      public int[] dec(int[] dataByte, int[] keyByte) {
    544.  
      int[][] keys = generateKeys(keyByte);
    545.  
      int[] ipByte = initPermute(dataByte);
    546.  
      int[] ipLeft = new int[32];
    547.  
      int[] ipRight = new int[32];
    548.  
      int[] tempLeft = new int[32];
    549.  
      int i = 0, j = 0, k = 0, m = 0, n = 0;
    550.  
      for (k = 0; k < 32; k++) {
    551.  
      ipLeft[k] = ipByte[k];
    552.  
      ipRight[k] = ipByte[32 + k];
    553.  
      }
    554.  
      for (i = 15; i >= 0; i--) {
    555.  
      for (j = 0; j < 32; j++) {
    556.  
      tempLeft[j] = ipLeft[j];
    557.  
      ipLeft[j] = ipRight[j];
    558.  
      }
    559.  
      int[] key = new int[48];
    560.  
      for (m = 0; m < 48; m++) {
    561.  
      key[m] = keys[i][m];
    562.  
      }
    563.  
       
    564.  
      int[] tempRight = xor(pPermute(sBoxPermute(xor(
    565.  
      expandPermute(ipRight), key))), tempLeft);
    566.  
      for (n = 0; n < 32; n++) {
    567.  
      ipRight[n] = tempRight[n];
    568.  
      }
    569.  
      }
    570.  
       
    571.  
      int[] finalData = new int[64];
    572.  
      for (i = 0; i < 32; i++) {
    573.  
      finalData[i] = ipRight[i];
    574.  
      finalData[32 + i] = ipLeft[i];
    575.  
      }
    576.  
      return finallyPermute(finalData);
    577.  
      }
    578.  
       
    579.  
      public int[] initPermute(int[] originalData) {
    580.  
      int[] ipByte = new int[64];
    581.  
      int i = 0, m = 1, n = 0, j, k;
    582.  
      for (i = 0, m = 1, n = 0; i < 4; i++, m += 2, n += 2) {
    583.  
      for (j = 7, k = 0; j >= 0; j--, k++) {
    584.  
      ipByte[i * 8 + k] = originalData[j * 8 + m];
    585.  
      ipByte[i * 8 + k + 32] = originalData[j * 8 + n];
    586.  
      }
    587.  
      }
    588.  
      return ipByte;
    589.  
      }
    590.  
       
    591.  
      public int[] expandPermute(int[] rightData) {
    592.  
      int[] epByte = new int[48];
    593.  
      int i, j;
    594.  
      for (i = 0; i < 8; i++) {
    595.  
      if (i == 0) {
    596.  
      epByte[i * 6 + 0] = rightData[31];
    597.  
      } else {
    598.  
      epByte[i * 6 + 0] = rightData[i * 4 - 1];
    599.  
      }
    600.  
      epByte[i * 6 + 1] = rightData[i * 4 + 0];
    601.  
      epByte[i * 6 + 2] = rightData[i * 4 + 1];
    602.  
      epByte[i * 6 + 3] = rightData[i * 4 + 2];
    603.  
      epByte[i * 6 + 4] = rightData[i * 4 + 3];
    604.  
      if (i == 7) {
    605.  
      epByte[i * 6 + 5] = rightData[0];
    606.  
      } else {
    607.  
      epByte[i * 6 + 5] = rightData[i * 4 + 4];
    608.  
      }
    609.  
      }
    610.  
      return epByte;
    611.  
      }
    612.  
       
    613.  
      public int[] xor(int[] byteOne, int[] byteTwo) {
    614.  
      // var xorByte = new Array(byteOne.length);
    615.  
      // for(int i = 0;i < byteOne.length; i ++){
    616.  
      // xorByte[i] = byteOne[i] ^ byteTwo[i];
    617.  
      // }
    618.  
      // return xorByte;
    619.  
      int[] xorByte = new int[byteOne.length];
    620.  
      for (int i = 0; i < byteOne.length; i++) {
    621.  
      xorByte[i] = byteOne[i] ^ byteTwo[i];
    622.  
      }
    623.  
      return xorByte;
    624.  
      }
    625.  
       
    626.  
      public int[] sBoxPermute(int[] expandByte) {
    627.  
       
    628.  
      // var sBoxByte = new Array(32);
    629.  
      int[] sBoxByte = new int[32];
    630.  
      String binary = "";
    631.  
      int[][] s1 = {
    632.  
      { 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7 },
    633.  
      { 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8 },
    634.  
      { 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0 },
    635.  
      { 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 } };
    636.  
       
    637.  
      /* Table - s2 */
    638.  
      int[][] s2 = {
    639.  
      { 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10 },
    640.  
      { 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5 },
    641.  
      { 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15 },
    642.  
      { 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 } };
    643.  
       
    644.  
      /* Table - s3 */
    645.  
      int[][] s3 = {
    646.  
      { 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8 },
    647.  
      { 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1 },
    648.  
      { 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7 },
    649.  
      { 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 } };
    650.  
      /* Table - s4 */
    651.  
      int[][] s4 = {
    652.  
      { 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15 },
    653.  
      { 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9 },
    654.  
      { 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4 },
    655.  
      { 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 } };
    656.  
       
    657.  
      /* Table - s5 */
    658.  
      int[][] s5 = {
    659.  
      { 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9 },
    660.  
      { 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6 },
    661.  
      { 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14 },
    662.  
      { 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 } };
    663.  
       
    664.  
      /* Table - s6 */
    665.  
      int[][] s6 = {
    666.  
      { 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11 },
    667.  
      { 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8 },
    668.  
      { 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6 },
    669.  
      { 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 } };
    670.  
       
    671.  
      /* Table - s7 */
    672.  
      int[][] s7 = {
    673.  
      { 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1 },
    674.  
      { 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6 },
    675.  
      { 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2 },
    676.  
      { 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 } };
    677.  
       
    678.  
      /* Table - s8 */
    679.  
      int[][] s8 = {
    680.  
      { 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7 },
    681.  
      { 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2 },
    682.  
      { 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8 },
    683.  
      { 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 } };
    684.  
       
    685.  
      for (int m = 0; m < 8; m++) {
    686.  
      int i = 0, j = 0;
    687.  
      i = expandByte[m * 6 + 0] * 2 + expandByte[m * 6 + 5];
    688.  
      j = expandByte[m * 6 + 1] * 2 * 2 * 2 + expandByte[m * 6 + 2] * 2
    689.  
      * 2 + expandByte[m * 6 + 3] * 2 + expandByte[m * 6 + 4];
    690.  
      switch (m) {
    691.  
      case 0:
    692.  
      binary = getBoxBinary(s1[i][j]);
    693.  
      break;
    694.  
      case 1:
    695.  
      binary = getBoxBinary(s2[i][j]);
    696.  
      break;
    697.  
      case 2:
    698.  
      binary = getBoxBinary(s3[i][j]);
    699.  
      break;
    700.  
      case 3:
    701.  
      binary = getBoxBinary(s4[i][j]);
    702.  
      break;
    703.  
      case 4:
    704.  
      binary = getBoxBinary(s5[i][j]);
    705.  
      break;
    706.  
      case 5:
    707.  
      binary = getBoxBinary(s6[i][j]);
    708.  
      break;
    709.  
      case 6:
    710.  
      binary = getBoxBinary(s7[i][j]);
    711.  
      break;
    712.  
      case 7:
    713.  
      binary = getBoxBinary(s8[i][j]);
    714.  
      break;
    715.  
      }
    716.  
      sBoxByte[m * 4 + 0] = Integer.parseInt(binary.substring(0, 1));
    717.  
      sBoxByte[m * 4 + 1] = Integer.parseInt(binary.substring(1, 2));
    718.  
      sBoxByte[m * 4 + 2] = Integer.parseInt(binary.substring(2, 3));
    719.  
      sBoxByte[m * 4 + 3] = Integer.parseInt(binary.substring(3, 4));
    720.  
      }
    721.  
      return sBoxByte;
    722.  
      }
    723.  
       
    724.  
      public int[] pPermute(int[] sBoxByte) {
    725.  
      int[] pBoxPermute = new int[32];
    726.  
      pBoxPermute[0] = sBoxByte[15];
    727.  
      pBoxPermute[1] = sBoxByte[6];
    728.  
      pBoxPermute[2] = sBoxByte[19];
    729.  
      pBoxPermute[3] = sBoxByte[20];
    730.  
      pBoxPermute[4] = sBoxByte[28];
    731.  
      pBoxPermute[5] = sBoxByte[11];
    732.  
      pBoxPermute[6] = sBoxByte[27];
    733.  
      pBoxPermute[7] = sBoxByte[16];
    734.  
      pBoxPermute[8] = sBoxByte[0];
    735.  
      pBoxPermute[9] = sBoxByte[14];
    736.  
      pBoxPermute[10] = sBoxByte[22];
    737.  
      pBoxPermute[11] = sBoxByte[25];
    738.  
      pBoxPermute[12] = sBoxByte[4];
    739.  
      pBoxPermute[13] = sBoxByte[17];
    740.  
      pBoxPermute[14] = sBoxByte[30];
    741.  
      pBoxPermute[15] = sBoxByte[9];
    742.  
      pBoxPermute[16] = sBoxByte[1];
    743.  
      pBoxPermute[17] = sBoxByte[7];
    744.  
      pBoxPermute[18] = sBoxByte[23];
    745.  
      pBoxPermute[19] = sBoxByte[13];
    746.  
      pBoxPermute[20] = sBoxByte[31];
    747.  
      pBoxPermute[21] = sBoxByte[26];
    748.  
      pBoxPermute[22] = sBoxByte[2];
    749.  
      pBoxPermute[23] = sBoxByte[8];
    750.  
      pBoxPermute[24] = sBoxByte[18];
    751.  
      pBoxPermute[25] = sBoxByte[12];
    752.  
      pBoxPermute[26] = sBoxByte[29];
    753.  
      pBoxPermute[27] = sBoxByte[5];
    754.  
      pBoxPermute[28] = sBoxByte[21];
    755.  
      pBoxPermute[29] = sBoxByte[10];
    756.  
      pBoxPermute[30] = sBoxByte[3];
    757.  
      pBoxPermute[31] = sBoxByte[24];
    758.  
      return pBoxPermute;
    759.  
      }
    760.  
       
    761.  
      public int[] finallyPermute(int[] endByte) {
    762.  
      int[] fpByte = new int[64];
    763.  
      fpByte[0] = endByte[39];
    764.  
      fpByte[1] = endByte[7];
    765.  
      fpByte[2] = endByte[47];
    766.  
      fpByte[3] = endByte[15];
    767.  
      fpByte[4] = endByte[55];
    768.  
      fpByte[5] = endByte[23];
    769.  
      fpByte[6] = endByte[63];
    770.  
      fpByte[7] = endByte[31];
    771.  
      fpByte[8] = endByte[38];
    772.  
      fpByte[9] = endByte[6];
    773.  
      fpByte[10] = endByte[46];
    774.  
      fpByte[11] = endByte[14];
    775.  
      fpByte[12] = endByte[54];
    776.  
      fpByte[13] = endByte[22];
    777.  
      fpByte[14] = endByte[62];
    778.  
      fpByte[15] = endByte[30];
    779.  
      fpByte[16] = endByte[37];
    780.  
      fpByte[17] = endByte[5];
    781.  
      fpByte[18] = endByte[45];
    782.  
      fpByte[19] = endByte[13];
    783.  
      fpByte[20] = endByte[53];
    784.  
      fpByte[21] = endByte[21];
    785.  
      fpByte[22] = endByte[61];
    786.  
      fpByte[23] = endByte[29];
    787.  
      fpByte[24] = endByte[36];
    788.  
      fpByte[25] = endByte[4];
    789.  
      fpByte[26] = endByte[44];
    790.  
      fpByte[27] = endByte[12];
    791.  
      fpByte[28] = endByte[52];
    792.  
      fpByte[29] = endByte[20];
    793.  
      fpByte[30] = endByte[60];
    794.  
      fpByte[31] = endByte[28];
    795.  
      fpByte[32] = endByte[35];
    796.  
      fpByte[33] = endByte[3];
    797.  
      fpByte[34] = endByte[43];
    798.  
      fpByte[35] = endByte[11];
    799.  
      fpByte[36] = endByte[51];
    800.  
      fpByte[37] = endByte[19];
    801.  
      fpByte[38] = endByte[59];
    802.  
      fpByte[39] = endByte[27];
    803.  
      fpByte[40] = endByte[34];
    804.  
      fpByte[41] = endByte[2];
    805.  
      fpByte[42] = endByte[42];
    806.  
      fpByte[43] = endByte[10];
    807.  
      fpByte[44] = endByte[50];
    808.  
      fpByte[45] = endByte[18];
    809.  
      fpByte[46] = endByte[58];
    810.  
      fpByte[47] = endByte[26];
    811.  
      fpByte[48] = endByte[33];
    812.  
      fpByte[49] = endByte[1];
    813.  
      fpByte[50] = endByte[41];
    814.  
      fpByte[51] = endByte[9];
    815.  
      fpByte[52] = endByte[49];
    816.  
      fpByte[53] = endByte[17];
    817.  
      fpByte[54] = endByte[57];
    818.  
      fpByte[55] = endByte[25];
    819.  
      fpByte[56] = endByte[32];
    820.  
      fpByte[57] = endByte[0];
    821.  
      fpByte[58] = endByte[40];
    822.  
      fpByte[59] = endByte[8];
    823.  
      fpByte[60] = endByte[48];
    824.  
      fpByte[61] = endByte[16];
    825.  
      fpByte[62] = endByte[56];
    826.  
      fpByte[63] = endByte[24];
    827.  
      return fpByte;
    828.  
      }
    829.  
       
    830.  
      public String getBoxBinary(int i) {
    831.  
      String binary = "";
    832.  
      switch (i) {
    833.  
      case 0:
    834.  
      binary = "0000";
    835.  
      break;
    836.  
      case 1:
    837.  
      binary = "0001";
    838.  
      break;
    839.  
      case 2:
    840.  
      binary = "0010";
    841.  
      break;
    842.  
      case 3:
    843.  
      binary = "0011";
    844.  
      break;
    845.  
      case 4:
    846.  
      binary = "0100";
    847.  
      break;
    848.  
      case 5:
    849.  
      binary = "0101";
    850.  
      break;
    851.  
      case 6:
    852.  
      binary = "0110";
    853.  
      break;
    854.  
      case 7:
    855.  
      binary = "0111";
    856.  
      break;
    857.  
      case 8:
    858.  
      binary = "1000";
    859.  
      break;
    860.  
      case 9:
    861.  
      binary = "1001";
    862.  
      break;
    863.  
      case 10:
    864.  
      binary = "1010";
    865.  
      break;
    866.  
      case 11:
    867.  
      binary = "1011";
    868.  
      break;
    869.  
      case 12:
    870.  
      binary = "1100";
    871.  
      break;
    872.  
      case 13:
    873.  
      binary = "1101";
    874.  
      break;
    875.  
      case 14:
    876.  
      binary = "1110";
    877.  
      break;
    878.  
      case 15:
    879.  
      binary = "1111";
    880.  
      break;
    881.  
      }
    882.  
      return binary;
    883.  
      }
    884.  
       
    885.  
      /*
    886.  
      * generate 16 keys for xor
    887.  
      *
    888.  
      */
    889.  
      public int[][] generateKeys(int[] keyByte) {
    890.  
      int[] key = new int[56];
    891.  
      int[][] keys = new int[16][48];
    892.  
       
    893.  
      // keys[ 0] = new Array();
    894.  
      // keys[ 1] = new Array();
    895.  
      // keys[ 2] = new Array();
    896.  
      // keys[ 3] = new Array();
    897.  
      // keys[ 4] = new Array();
    898.  
      // keys[ 5] = new Array();
    899.  
      // keys[ 6] = new Array();
    900.  
      // keys[ 7] = new Array();
    901.  
      // keys[ 8] = new Array();
    902.  
      // keys[ 9] = new Array();
    903.  
      // keys[10] = new Array();
    904.  
      // keys[11] = new Array();
    905.  
      // keys[12] = new Array();
    906.  
      // keys[13] = new Array();
    907.  
      // keys[14] = new Array();
    908.  
      // keys[15] = new Array();
    909.  
      int[] loop = new int[] { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
    910.  
       
    911.  
      for (int i = 0; i < 7; i++) {
    912.  
      for (int j = 0, k = 7; j < 8; j++, k--) {
    913.  
      key[i * 8 + j] = keyByte[8 * k + i];
    914.  
      }
    915.  
      }
    916.  
       
    917.  
      int i = 0;
    918.  
      for (i = 0; i < 16; i++) {
    919.  
      int tempLeft = 0;
    920.  
      int tempRight = 0;
    921.  
      for (int j = 0; j < loop[i]; j++) {
    922.  
      tempLeft = key[0];
    923.  
      tempRight = key[28];
    924.  
      for (int k = 0; k < 27; k++) {
    925.  
      key[k] = key[k + 1];
    926.  
      key[28 + k] = key[29 + k];
    927.  
      }
    928.  
      key[27] = tempLeft;
    929.  
      key[55] = tempRight;
    930.  
      }
    931.  
      // var tempKey = new Array(48);
    932.  
      int[] tempKey = new int[48];
    933.  
      tempKey[0] = key[13];
    934.  
      tempKey[1] = key[16];
    935.  
      tempKey[2] = key[10];
    936.  
      tempKey[3] = key[23];
    937.  
      tempKey[4] = key[0];
    938.  
      tempKey[5] = key[4];
    939.  
      tempKey[6] = key[2];
    940.  
      tempKey[7] = key[27];
    941.  
      tempKey[8] = key[14];
    942.  
      tempKey[9] = key[5];
    943.  
      tempKey[10] = key[20];
    944.  
      tempKey[11] = key[9];
    945.  
      tempKey[12] = key[22];
    946.  
      tempKey[13] = key[18];
    947.  
      tempKey[14] = key[11];
    948.  
      tempKey[15] = key[3];
    949.  
      tempKey[16] = key[25];
    950.  
      tempKey[17] = key[7];
    951.  
      tempKey[18] = key[15];
    952.  
      tempKey[19] = key[6];
    953.  
      tempKey[20] = key[26];
    954.  
      tempKey[21] = key[19];
    955.  
      tempKey[22] = key[12];
    956.  
      tempKey[23] = key[1];
    957.  
      tempKey[24] = key[40];
    958.  
      tempKey[25] = key[51];
    959.  
      tempKey[26] = key[30];
    960.  
      tempKey[27] = key[36];
    961.  
      tempKey[28] = key[46];
    962.  
      tempKey[29] = key[54];
    963.  
      tempKey[30] = key[29];
    964.  
      tempKey[31] = key[39];
    965.  
      tempKey[32] = key[50];
    966.  
      tempKey[33] = key[44];
    967.  
      tempKey[34] = key[32];
    968.  
      tempKey[35] = key[47];
    969.  
      tempKey[36] = key[43];
    970.  
      tempKey[37] = key[48];
    971.  
      tempKey[38] = key[38];
    972.  
      tempKey[39] = key[55];
    973.  
      tempKey[40] = key[33];
    974.  
      tempKey[41] = key[52];
    975.  
      tempKey[42] = key[45];
    976.  
      tempKey[43] = key[41];
    977.  
      tempKey[44] = key[49];
    978.  
      tempKey[45] = key[35];
    979.  
      tempKey[46] = key[28];
    980.  
      tempKey[47] = key[31];
    981.  
      int m;
    982.  
      switch (i) {
    983.  
      case 0:
    984.  
      for (m = 0; m < 48; m++) {
    985.  
      keys[0][m] = tempKey[m];
    986.  
      }
    987.  
      break;
    988.  
      case 1:
    989.  
      for (m = 0; m < 48; m++) {
    990.  
      keys[1][m] = tempKey[m];
    991.  
      }
    992.  
      break;
    993.  
      case 2:
    994.  
      for (m = 0; m < 48; m++) {
    995.  
      keys[2][m] = tempKey[m];
    996.  
      }
    997.  
      break;
    998.  
      case 3:
    999.  
      for (m = 0; m < 48; m++) {
    1000.  
      keys[3][m] = tempKey[m];
    1001.  
      }
    1002.  
      break;
    1003.  
      case 4:
    1004.  
      for (m = 0; m < 48; m++) {
    1005.  
      keys[4][m] = tempKey[m];
    1006.  
      }
    1007.  
      break;
    1008.  
      case 5:
    1009.  
      for (m = 0; m < 48; m++) {
    1010.  
      keys[5][m] = tempKey[m];
    1011.  
      }
    1012.  
      break;
    1013.  
      case 6:
    1014.  
      for (m = 0; m < 48; m++) {
    1015.  
      keys[6][m] = tempKey[m];
    1016.  
      }
    1017.  
      break;
    1018.  
      case 7:
    1019.  
      for (m = 0; m < 48; m++) {
    1020.  
      keys[7][m] = tempKey[m];
    1021.  
      }
    1022.  
      break;
    1023.  
      case 8:
    1024.  
      for (m = 0; m < 48; m++) {
    1025.  
      keys[8][m] = tempKey[m];
    1026.  
      }
    1027.  
      break;
    1028.  
      case 9:
    1029.  
      for (m = 0; m < 48; m++) {
    1030.  
      keys[9][m] = tempKey[m];
    1031.  
      }
    1032.  
      break;
    1033.  
      case 10:
    1034.  
      for (m = 0; m < 48; m++) {
    1035.  
      keys[10][m] = tempKey[m];
    1036.  
      }
    1037.  
      break;
    1038.  
      case 11:
    1039.  
      for (m = 0; m < 48; m++) {
    1040.  
      keys[11][m] = tempKey[m];
    1041.  
      }
    1042.  
      break;
    1043.  
      case 12:
    1044.  
      for (m = 0; m < 48; m++) {
    1045.  
      keys[12][m] = tempKey[m];
    1046.  
      }
    1047.  
      break;
    1048.  
      case 13:
    1049.  
      for (m = 0; m < 48; m++) {
    1050.  
      keys[13][m] = tempKey[m];
    1051.  
      }
    1052.  
      break;
    1053.  
      case 14:
    1054.  
      for (m = 0; m < 48; m++) {
    1055.  
      keys[14][m] = tempKey[m];
    1056.  
      }
    1057.  
      break;
    1058.  
      case 15:
    1059.  
      for (m = 0; m < 48; m++) {
    1060.  
      keys[15][m] = tempKey[m];
    1061.  
      }
    1062.  
      break;
    1063.  
      }
    1064.  
      }
    1065.  
      return keys;
    1066.  
      }
    1067.  
      }

    二、JS实现

    1、des.js文件

      1.  
        /**
      2.  
        * DES加密/解密
      3.  
        * @Copyright Copyright (c) 2015
      4.  
        * @author liuyazhuang
      5.  
        * @see DESCore
      6.  
        */
      7.  
         
      8.  
        /*
      9.  
        * encrypt the string to string made up of hex
      10.  
        * return the encrypted string
      11.  
        */
      12.  
        function strEnc(data,firstKey,secondKey,thirdKey){
      13.  
        var leng = data.length;
      14.  
        var encData = "";
      15.  
        var firstKeyBt,secondKeyBt,thirdKeyBt,firstLength,secondLength,thirdLength;
      16.  
        if(firstKey != null && firstKey != ""){
      17.  
        firstKeyBt = getKeyBytes(firstKey);
      18.  
        firstLength = firstKeyBt.length;
      19.  
        }
      20.  
        if(secondKey != null && secondKey != ""){
      21.  
        secondKeyBt = getKeyBytes(secondKey);
      22.  
        secondLength = secondKeyBt.length;
      23.  
        }
      24.  
        if(thirdKey != null && thirdKey != ""){
      25.  
        thirdKeyBt = getKeyBytes(thirdKey);
      26.  
        thirdLength = thirdKeyBt.length;
      27.  
        }
      28.  
         
      29.  
        if(leng > 0){
      30.  
        if(leng < 4){
      31.  
        var bt = strToBt(data);
      32.  
        var encByte ;
      33.  
        if(firstKey != null && firstKey !="" && secondKey != null && secondKey != "" && thirdKey != null && thirdKey != ""){
      34.  
        var tempBt;
      35.  
        var x,y,z;
      36.  
        tempBt = bt;
      37.  
        for(x = 0;x < firstLength ;x ++){
      38.  
        tempBt = enc(tempBt,firstKeyBt[x]);
      39.  
        }
      40.  
        for(y = 0;y < secondLength ;y ++){
      41.  
        tempBt = enc(tempBt,secondKeyBt[y]);
      42.  
        }
      43.  
        for(z = 0;z < thirdLength ;z ++){
      44.  
        tempBt = enc(tempBt,thirdKeyBt[z]);
      45.  
        }
      46.  
        encByte = tempBt;
      47.  
        }else{
      48.  
        if(firstKey != null && firstKey !="" && secondKey != null && secondKey != ""){
      49.  
        var tempBt;
      50.  
        var x,y;
      51.  
        tempBt = bt;
      52.  
        for(x = 0;x < firstLength ;x ++){
      53.  
        tempBt = enc(tempBt,firstKeyBt[x]);
      54.  
        }
      55.  
        for(y = 0;y < secondLength ;y ++){
      56.  
        tempBt = enc(tempBt,secondKeyBt[y]);
      57.  
        }
      58.  
        encByte = tempBt;
      59.  
        }else{
      60.  
        if(firstKey != null && firstKey !=""){
      61.  
        var tempBt;
      62.  
        var x = 0;
      63.  
        tempBt = bt;
      64.  
        for(x = 0;x < firstLength ;x ++){
      65.  
        tempBt = enc(tempBt,firstKeyBt[x]);
      66.  
        }
      67.  
        encByte = tempBt;
      68.  
        }
      69.  
        }
      70.  
        }
      71.  
        encData = bt64ToHex(encByte);
      72.  
        }else{
      73.  
        var iterator = parseInt(leng/4);
      74.  
        var remainder = leng%4;
      75.  
        var i=0;
      76.  
        for(i = 0;i < iterator;i++){
      77.  
        var tempData = data.substring(i*4+0,i*4+4);
      78.  
        var tempByte = strToBt(tempData);
      79.  
        var encByte ;
      80.  
        if(firstKey != null && firstKey !="" && secondKey != null && secondKey != "" && thirdKey != null && thirdKey != ""){
      81.  
        var tempBt;
      82.  
        var x,y,z;
      83.  
        tempBt = tempByte;
      84.  
        for(x = 0;x < firstLength ;x ++){
      85.  
        tempBt = enc(tempBt,firstKeyBt[x]);
      86.  
        }
      87.  
        for(y = 0;y < secondLength ;y ++){
      88.  
        tempBt = enc(tempBt,secondKeyBt[y]);
      89.  
        }
      90.  
        for(z = 0;z < thirdLength ;z ++){
      91.  
        tempBt = enc(tempBt,thirdKeyBt[z]);
      92.  
        }
      93.  
        encByte = tempBt;
      94.  
        }else{
      95.  
        if(firstKey != null && firstKey !="" && secondKey != null && secondKey != ""){
      96.  
        var tempBt;
      97.  
        var x,y;
      98.  
        tempBt = tempByte;
      99.  
        for(x = 0;x < firstLength ;x ++){
      100.  
        tempBt = enc(tempBt,firstKeyBt[x]);
      101.  
        }
      102.  
        for(y = 0;y < secondLength ;y ++){
      103.  
        tempBt = enc(tempBt,secondKeyBt[y]);
      104.  
        }
      105.  
        encByte = tempBt;
      106.  
        }else{
      107.  
        if(firstKey != null && firstKey !=""){
      108.  
        var tempBt;
      109.  
        var x;
      110.  
        tempBt = tempByte;
      111.  
        for(x = 0;x < firstLength ;x ++){
      112.  
        tempBt = enc(tempBt,firstKeyBt[x]);
      113.  
        }
      114.  
        encByte = tempBt;
      115.  
        }
      116.  
        }
      117.  
        }
      118.  
        encData += bt64ToHex(encByte);
      119.  
        }
      120.  
        if(remainder > 0){
      121.  
        var remainderData = data.substring(iterator*4+0,leng);
      122.  
        var tempByte = strToBt(remainderData);
      123.  
        var encByte ;
      124.  
        if(firstKey != null && firstKey !="" && secondKey != null && secondKey != "" && thirdKey != null && thirdKey != ""){
      125.  
        var tempBt;
      126.  
        var x,y,z;
      127.  
        tempBt = tempByte;
      128.  
        for(x = 0;x < firstLength ;x ++){
      129.  
        tempBt = enc(tempBt,firstKeyBt[x]);
      130.  
        }
      131.  
        for(y = 0;y < secondLength ;y ++){
      132.  
        tempBt = enc(tempBt,secondKeyBt[y]);
      133.  
        }
      134.  
        for(z = 0;z < thirdLength ;z ++){
      135.  
        tempBt = enc(tempBt,thirdKeyBt[z]);
      136.  
        }
      137.  
        encByte = tempBt;
      138.  
        }else{
      139.  
        if(firstKey != null && firstKey !="" && secondKey != null && secondKey != ""){
      140.  
        var tempBt;
      141.  
        var x,y;
      142.  
        tempBt = tempByte;
      143.  
        for(x = 0;x < firstLength ;x ++){
      144.  
        tempBt = enc(tempBt,firstKeyBt[x]);
      145.  
        }
      146.  
        for(y = 0;y < secondLength ;y ++){
      147.  
        tempBt = enc(tempBt,secondKeyBt[y]);
      148.  
        }
      149.  
        encByte = tempBt;
      150.  
        }else{
      151.  
        if(firstKey != null && firstKey !=""){
      152.  
        var tempBt;
      153.  
        var x;
      154.  
        tempBt = tempByte;
      155.  
        for(x = 0;x < firstLength ;x ++){
      156.  
        tempBt = enc(tempBt,firstKeyBt[x]);
      157.  
        }
      158.  
        encByte = tempBt;
      159.  
        }
      160.  
        }
      161.  
        }
      162.  
        encData += bt64ToHex(encByte);
      163.  
        }
      164.  
        }
      165.  
        }
      166.  
        return encData;
      167.  
        }
      168.  
         
      169.  
        /*
      170.  
        * decrypt the encrypted string to the original string
      171.  
        *
      172.  
        * return the original string
      173.  
        */
      174.  
        function strDec(data,firstKey,secondKey,thirdKey){
      175.  
        var leng = data.length;
      176.  
        var decStr = "";
      177.  
        var firstKeyBt,secondKeyBt,thirdKeyBt,firstLength,secondLength,thirdLength;
      178.  
        if(firstKey != null && firstKey != ""){
      179.  
        firstKeyBt = getKeyBytes(firstKey);
      180.  
        firstLength = firstKeyBt.length;
      181.  
        }
      182.  
        if(secondKey != null && secondKey != ""){
      183.  
        secondKeyBt = getKeyBytes(secondKey);
      184.  
        secondLength = secondKeyBt.length;
      185.  
        }
      186.  
        if(thirdKey != null && thirdKey != ""){
      187.  
        thirdKeyBt = getKeyBytes(thirdKey);
      188.  
        thirdLength = thirdKeyBt.length;
      189.  
        }
      190.  
         
      191.  
        var iterator = parseInt(leng/16);
      192.  
        var i=0;
      193.  
        for(i = 0;i < iterator;i++){
      194.  
        var tempData = data.substring(i*16+0,i*16+16);
      195.  
        var strByte = hexToBt64(tempData);
      196.  
        var intByte = new Array(64);
      197.  
        var j = 0;
      198.  
        for(j = 0;j < 64; j++){
      199.  
        intByte[j] = parseInt(strByte.substring(j,j+1));
      200.  
        }
      201.  
        var decByte;
      202.  
        if(firstKey != null && firstKey !="" && secondKey != null && secondKey != "" && thirdKey != null && thirdKey != ""){
      203.  
        var tempBt;
      204.  
        var x,y,z;
      205.  
        tempBt = intByte;
      206.  
        for(x = thirdLength - 1;x >= 0;x --){
      207.  
        tempBt = dec(tempBt,thirdKeyBt[x]);
      208.  
        }
      209.  
        for(y = secondLength - 1;y >= 0;y --){
      210.  
        tempBt = dec(tempBt,secondKeyBt[y]);
      211.  
        }
      212.  
        for(z = firstLength - 1;z >= 0 ;z --){
      213.  
        tempBt = dec(tempBt,firstKeyBt[z]);
      214.  
        }
      215.  
        decByte = tempBt;
      216.  
        }else{
      217.  
        if(firstKey != null && firstKey !="" && secondKey != null && secondKey != ""){
      218.  
        var tempBt;
      219.  
        var x,y,z;
      220.  
        tempBt = intByte;
      221.  
        for(x = secondLength - 1;x >= 0 ;x --){
      222.  
        tempBt = dec(tempBt,secondKeyBt[x]);
      223.  
        }
      224.  
        for(y = firstLength - 1;y >= 0 ;y --){
      225.  
        tempBt = dec(tempBt,firstKeyBt[y]);
      226.  
        }
      227.  
        decByte = tempBt;
      228.  
        }else{
      229.  
        if(firstKey != null && firstKey !=""){
      230.  
        var tempBt;
      231.  
        var x,y,z;
      232.  
        tempBt = intByte;
      233.  
        for(x = firstLength - 1;x >= 0 ;x --){
      234.  
        tempBt = dec(tempBt,firstKeyBt[x]);
      235.  
        }
      236.  
        decByte = tempBt;
      237.  
        }
      238.  
        }
      239.  
        }
      240.  
        decStr += byteToString(decByte);
      241.  
        }
      242.  
        return decStr;
      243.  
        }
      244.  
        /*
      245.  
        * chang the string into the bit array
      246.  
        *
      247.  
        * return bit array(it's length % 64 = 0)
      248.  
        */
      249.  
        function getKeyBytes(key){
      250.  
        var keyBytes = new Array();
      251.  
        var leng = key.length;
      252.  
        var iterator = parseInt(leng/4);
      253.  
        var remainder = leng%4;
      254.  
        var i = 0;
      255.  
        for(i = 0;i < iterator; i ++){
      256.  
        keyBytes[i] = strToBt(key.substring(i*4+0,i*4+4));
      257.  
        }
      258.  
        if(remainder > 0){
      259.  
        keyBytes[i] = strToBt(key.substring(i*4+0,leng));
      260.  
        }
      261.  
        return keyBytes;
      262.  
        }
      263.  
         
      264.  
        /*
      265.  
        * chang the string(it's length <= 4) into the bit array
      266.  
        *
      267.  
        * return bit array(it's length = 64)
      268.  
        */
      269.  
        function strToBt(str){
      270.  
        var leng = str.length;
      271.  
        var bt = new Array(64);
      272.  
        if(leng < 4){
      273.  
        var i=0,j=0,p=0,q=0;
      274.  
        for(i = 0;i<leng;i++){
      275.  
        var k = str.charCodeAt(i);
      276.  
        for(j=0;j<16;j++){
      277.  
        var pow=1,m=0;
      278.  
        for(m=15;m>j;m--){
      279.  
        pow *= 2;
      280.  
        }
      281.  
        bt[16*i+j]=parseInt(k/pow)%2;
      282.  
        }
      283.  
        }
      284.  
        for(p = leng;p<4;p++){
      285.  
        var k = 0;
      286.  
        for(q=0;q<16;q++){
      287.  
        var pow=1,m=0;
      288.  
        for(m=15;m>q;m--){
      289.  
        pow *= 2;
      290.  
        }
      291.  
        bt[16*p+q]=parseInt(k/pow)%2;
      292.  
        }
      293.  
        }
      294.  
        }else{
      295.  
        for(i = 0;i<4;i++){
      296.  
        var k = str.charCodeAt(i);
      297.  
        for(j=0;j<16;j++){
      298.  
        var pow=1;
      299.  
        for(m=15;m>j;m--){
      300.  
        pow *= 2;
      301.  
        }
      302.  
        bt[16*i+j]=parseInt(k/pow)%2;
      303.  
        }
      304.  
        }
      305.  
        }
      306.  
        return bt;
      307.  
        }
      308.  
         
      309.  
        /*
      310.  
        * chang the bit(it's length = 4) into the hex
      311.  
        *
      312.  
        * return hex
      313.  
        */
      314.  
        function bt4ToHex(binary) {
      315.  
        var hex;
      316.  
        switch (binary) {
      317.  
        case "0000" : hex = "0"; break;
      318.  
        case "0001" : hex = "1"; break;
      319.  
        case "0010" : hex = "2"; break;
      320.  
        case "0011" : hex = "3"; break;
      321.  
        case "0100" : hex = "4"; break;
      322.  
        case "0101" : hex = "5"; break;
      323.  
        case "0110" : hex = "6"; break;
      324.  
        case "0111" : hex = "7"; break;
      325.  
        case "1000" : hex = "8"; break;
      326.  
        case "1001" : hex = "9"; break;
      327.  
        case "1010" : hex = "A"; break;
      328.  
        case "1011" : hex = "B"; break;
      329.  
        case "1100" : hex = "C"; break;
      330.  
        case "1101" : hex = "D"; break;
      331.  
        case "1110" : hex = "E"; break;
      332.  
        case "1111" : hex = "F"; break;
      333.  
        }
      334.  
        return hex;
      335.  
        }
      336.  
         
      337.  
        /*
      338.  
        * chang the hex into the bit(it's length = 4)
      339.  
        *
      340.  
        * return the bit(it's length = 4)
      341.  
        */
      342.  
        function hexToBt4(hex) {
      343.  
        var binary;
      344.  
        switch (hex) {
      345.  
        case "0" : binary = "0000"; break;
      346.  
        case "1" : binary = "0001"; break;
      347.  
        case "2" : binary = "0010"; break;
      348.  
        case "3" : binary = "0011"; break;
      349.  
        case "4" : binary = "0100"; break;
      350.  
        case "5" : binary = "0101"; break;
      351.  
        case "6" : binary = "0110"; break;
      352.  
        case "7" : binary = "0111"; break;
      353.  
        case "8" : binary = "1000"; break;
      354.  
        case "9" : binary = "1001"; break;
      355.  
        case "A" : binary = "1010"; break;
      356.  
        case "B" : binary = "1011"; break;
      357.  
        case "C" : binary = "1100"; break;
      358.  
        case "D" : binary = "1101"; break;
      359.  
        case "E" : binary = "1110"; break;
      360.  
        case "F" : binary = "1111"; break;
      361.  
        }
      362.  
        return binary;
      363.  
        }
      364.  
         
      365.  
        /*
      366.  
        * chang the bit(it's length = 64) into the string
      367.  
        *
      368.  
        * return string
      369.  
        */
      370.  
        function byteToString(byteData){
      371.  
        var str="";
      372.  
        for(i = 0;i<4;i++){
      373.  
        var count=0;
      374.  
        for(j=0;j<16;j++){
      375.  
        var pow=1;
      376.  
        for(m=15;m>j;m--){
      377.  
        pow*=2;
      378.  
        }
      379.  
        count+=byteData[16*i+j]*pow;
      380.  
        }
      381.  
        if(count != 0){
      382.  
        str+=String.fromCharCode(count);
      383.  
        }
      384.  
        }
      385.  
        return str;
      386.  
        }
      387.  
         
      388.  
        function bt64ToHex(byteData){
      389.  
        var hex = "";
      390.  
        for(i = 0;i<16;i++){
      391.  
        var bt = "";
      392.  
        for(j=0;j<4;j++){
      393.  
        bt += byteData[i*4+j];
      394.  
        }
      395.  
        hex+=bt4ToHex(bt);
      396.  
        }
      397.  
        return hex;
      398.  
        }
      399.  
         
      400.  
        function hexToBt64(hex){
      401.  
        var binary = "";
      402.  
        for(i = 0;i<16;i++){
      403.  
        binary+=hexToBt4(hex.substring(i,i+1));
      404.  
        }
      405.  
        return binary;
      406.  
        }
      407.  
         
      408.  
        /*
      409.  
        * the 64 bit des core arithmetic
      410.  
        */
      411.  
         
      412.  
        function enc(dataByte,keyByte){
      413.  
        var keys = generateKeys(keyByte);
      414.  
        var ipByte = initPermute(dataByte);
      415.  
        var ipLeft = new Array(32);
      416.  
        var ipRight = new Array(32);
      417.  
        var tempLeft = new Array(32);
      418.  
        var i = 0,j = 0,k = 0,m = 0, n = 0;
      419.  
        for(k = 0;k < 32;k ++){
      420.  
        ipLeft[k] = ipByte[k];
      421.  
        ipRight[k] = ipByte[32+k];
      422.  
        }
      423.  
        for(i = 0;i < 16;i ++){
      424.  
        for(j = 0;j < 32;j ++){
      425.  
        tempLeft[j] = ipLeft[j];
      426.  
        ipLeft[j] = ipRight[j];
      427.  
        }
      428.  
        var key = new Array(48);
      429.  
        for(m = 0;m < 48;m ++){
      430.  
        key[m] = keys[i][m];
      431.  
        }
      432.  
        var tempRight = xor(pPermute(sBoxPermute(xor(expandPermute(ipRight),key))), tempLeft);
      433.  
        for(n = 0;n < 32;n ++){
      434.  
        ipRight[n] = tempRight[n];
      435.  
        }
      436.  
         
      437.  
        }
      438.  
         
      439.  
         
      440.  
        var finalData =new Array(64);
      441.  
        for(i = 0;i < 32;i ++){
      442.  
        finalData[i] = ipRight[i];
      443.  
        finalData[32+i] = ipLeft[i];
      444.  
        }
      445.  
        return finallyPermute(finalData);
      446.  
        }
      447.  
         
      448.  
        function dec(dataByte,keyByte){
      449.  
        var keys = generateKeys(keyByte);
      450.  
        var ipByte = initPermute(dataByte);
      451.  
        var ipLeft = new Array(32);
      452.  
        var ipRight = new Array(32);
      453.  
        var tempLeft = new Array(32);
      454.  
        var i = 0,j = 0,k = 0,m = 0, n = 0;
      455.  
        for(k = 0;k < 32;k ++){
      456.  
        ipLeft[k] = ipByte[k];
      457.  
        ipRight[k] = ipByte[32+k];
      458.  
        }
      459.  
        for(i = 15;i >= 0;i --){
      460.  
        for(j = 0;j < 32;j ++){
      461.  
        tempLeft[j] = ipLeft[j];
      462.  
        ipLeft[j] = ipRight[j];
      463.  
        }
      464.  
        var key = new Array(48);
      465.  
        for(m = 0;m < 48;m ++){
      466.  
        key[m] = keys[i][m];
      467.  
        }
      468.  
         
      469.  
        var tempRight = xor(pPermute(sBoxPermute(xor(expandPermute(ipRight),key))), tempLeft);
      470.  
        for(n = 0;n < 32;n ++){
      471.  
        ipRight[n] = tempRight[n];
      472.  
        }
      473.  
        }
      474.  
         
      475.  
         
      476.  
        var finalData =new Array(64);
      477.  
        for(i = 0;i < 32;i ++){
      478.  
        finalData[i] = ipRight[i];
      479.  
        finalData[32+i] = ipLeft[i];
      480.  
        }
      481.  
        return finallyPermute(finalData);
      482.  
        }
      483.  
         
      484.  
        function initPermute(originalData){
      485.  
        var ipByte = new Array(64);
      486.  
        for (i = 0, m = 1, n = 0; i < 4; i++, m += 2, n += 2) {
      487.  
        for (j = 7, k = 0; j >= 0; j--, k++) {
      488.  
        ipByte[i * 8 + k] = originalData[j * 8 + m];
      489.  
        ipByte[i * 8 + k + 32] = originalData[j * 8 + n];
      490.  
        }
      491.  
        }
      492.  
        return ipByte;
      493.  
        }
      494.  
         
      495.  
        function expandPermute(rightData){
      496.  
        var epByte = new Array(48);
      497.  
        for (i = 0; i < 8; i++) {
      498.  
        if (i == 0) {
      499.  
        epByte[i * 6 + 0] = rightData[31];
      500.  
        } else {
      501.  
        epByte[i * 6 + 0] = rightData[i * 4 - 1];
      502.  
        }
      503.  
        epByte[i * 6 + 1] = rightData[i * 4 + 0];
      504.  
        epByte[i * 6 + 2] = rightData[i * 4 + 1];
      505.  
        epByte[i * 6 + 3] = rightData[i * 4 + 2];
      506.  
        epByte[i * 6 + 4] = rightData[i * 4 + 3];
      507.  
        if (i == 7) {
      508.  
        epByte[i * 6 + 5] = rightData[0];
      509.  
        } else {
      510.  
        epByte[i * 6 + 5] = rightData[i * 4 + 4];
      511.  
        }
      512.  
        }
      513.  
        return epByte;
      514.  
        }
      515.  
         
      516.  
        function xor(byteOne,byteTwo){
      517.  
        var xorByte = new Array(byteOne.length);
      518.  
        for(i = 0;i < byteOne.length; i ++){
      519.  
        xorByte[i] = byteOne[i] ^ byteTwo[i];
      520.  
        }
      521.  
        return xorByte;
      522.  
        }
      523.  
         
      524.  
        function sBoxPermute(expandByte){
      525.  
         
      526.  
        var sBoxByte = new Array(32);
      527.  
        var binary = "";
      528.  
        var s1 = [
      529.  
        [14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7],
      530.  
        [0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8],
      531.  
        [4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0],
      532.  
        [15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 ]];
      533.  
         
      534.  
        /* Table - s2 */
      535.  
        var s2 = [
      536.  
        [15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10],
      537.  
        [3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5],
      538.  
        [0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15],
      539.  
        [13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 ]];
      540.  
         
      541.  
        /* Table - s3 */
      542.  
        var s3= [
      543.  
        [10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8],
      544.  
        [13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1],
      545.  
        [13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7],
      546.  
        [1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 ]];
      547.  
        /* Table - s4 */
      548.  
        var s4 = [
      549.  
        [7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15],
      550.  
        [13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9],
      551.  
        [10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4],
      552.  
        [3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 ]];
      553.  
         
      554.  
        /* Table - s5 */
      555.  
        var s5 = [
      556.  
        [2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9],
      557.  
        [14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6],
      558.  
        [4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14],
      559.  
        [11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 ]];
      560.  
         
      561.  
        /* Table - s6 */
      562.  
        var s6 = [
      563.  
        [12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11],
      564.  
        [10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8],
      565.  
        [9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6],
      566.  
        [4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 ]];
      567.  
         
      568.  
        /* Table - s7 */
      569.  
        var s7 = [
      570.  
        [4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1],
      571.  
        [13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6],
      572.  
        [1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2],
      573.  
        [6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12]];
      574.  
         
      575.  
        /* Table - s8 */
      576.  
        var s8 = [
      577.  
        [13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7],
      578.  
        [1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2],
      579.  
        [7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8],
      580.  
        [2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11]];
      581.  
         
      582.  
        for(m=0;m<8;m++){
      583.  
        var i=0,j=0;
      584.  
        i = expandByte[m*6+0]*2+expandByte[m*6+5];
      585.  
        j = expandByte[m * 6 + 1] * 2 * 2 * 2
      586.  
        + expandByte[m * 6 + 2] * 2* 2
      587.  
        + expandByte[m * 6 + 3] * 2
      588.  
        + expandByte[m * 6 + 4];
      589.  
        switch (m) {
      590.  
        case 0 :
      591.  
        binary = getBoxBinary(s1[i][j]);
      592.  
        break;
      593.  
        case 1 :
      594.  
        binary = getBoxBinary(s2[i][j]);
      595.  
        break;
      596.  
        case 2 :
      597.  
        binary = getBoxBinary(s3[i][j]);
      598.  
        break;
      599.  
        case 3 :
      600.  
        binary = getBoxBinary(s4[i][j]);
      601.  
        break;
      602.  
        case 4 :
      603.  
        binary = getBoxBinary(s5[i][j]);
      604.  
        break;
      605.  
        case 5 :
      606.  
        binary = getBoxBinary(s6[i][j]);
      607.  
        break;
      608.  
        case 6 :
      609.  
        binary = getBoxBinary(s7[i][j]);
      610.  
        break;
      611.  
        case 7 :
      612.  
        binary = getBoxBinary(s8[i][j]);
      613.  
        break;
      614.  
        }
      615.  
        sBoxByte[m*4+0] = parseInt(binary.substring(0,1));
      616.  
        sBoxByte[m*4+1] = parseInt(binary.substring(1,2));
      617.  
        sBoxByte[m*4+2] = parseInt(binary.substring(2,3));
      618.  
        sBoxByte[m*4+3] = parseInt(binary.substring(3,4));
      619.  
        }
      620.  
        return sBoxByte;
      621.  
        }
      622.  
         
      623.  
        function pPermute(sBoxByte){
      624.  
        var pBoxPermute = new Array(32);
      625.  
        pBoxPermute[ 0] = sBoxByte[15];
      626.  
        pBoxPermute[ 1] = sBoxByte[ 6];
      627.  
        pBoxPermute[ 2] = sBoxByte[19];
      628.  
        pBoxPermute[ 3] = sBoxByte[20];
      629.  
        pBoxPermute[ 4] = sBoxByte[28];
      630.  
        pBoxPermute[ 5] = sBoxByte[11];
      631.  
        pBoxPermute[ 6] = sBoxByte[27];
      632.  
        pBoxPermute[ 7] = sBoxByte[16];
      633.  
        pBoxPermute[ 8] = sBoxByte[ 0];
      634.  
        pBoxPermute[ 9] = sBoxByte[14];
      635.  
        pBoxPermute[10] = sBoxByte[22];
      636.  
        pBoxPermute[11] = sBoxByte[25];
      637.  
        pBoxPermute[12] = sBoxByte[ 4];
      638.  
        pBoxPermute[13] = sBoxByte[17];
      639.  
        pBoxPermute[14] = sBoxByte[30];
      640.  
        pBoxPermute[15] = sBoxByte[ 9];
      641.  
        pBoxPermute[16] = sBoxByte[ 1];
      642.  
        pBoxPermute[17] = sBoxByte[ 7];
      643.  
        pBoxPermute[18] = sBoxByte[23];
      644.  
        pBoxPermute[19] = sBoxByte[13];
      645.  
        pBoxPermute[20] = sBoxByte[31];
      646.  
        pBoxPermute[21] = sBoxByte[26];
      647.  
        pBoxPermute[22] = sBoxByte[ 2];
      648.  
        pBoxPermute[23] = sBoxByte[ 8];
      649.  
        pBoxPermute[24] = sBoxByte[18];
      650.  
        pBoxPermute[25] = sBoxByte[12];
      651.  
        pBoxPermute[26] = sBoxByte[29];
      652.  
        pBoxPermute[27] = sBoxByte[ 5];
      653.  
        pBoxPermute[28] = sBoxByte[21];
      654.  
        pBoxPermute[29] = sBoxByte[10];
      655.  
        pBoxPermute[30] = sBoxByte[ 3];
      656.  
        pBoxPermute[31] = sBoxByte[24];
      657.  
        return pBoxPermute;
      658.  
        }
      659.  
         
      660.  
        function finallyPermute(endByte){
      661.  
        var fpByte = new Array(64);
      662.  
        fpByte[ 0] = endByte[39];
      663.  
        fpByte[ 1] = endByte[ 7];
      664.  
        fpByte[ 2] = endByte[47];
      665.  
        fpByte[ 3] = endByte[15];
      666.  
        fpByte[ 4] = endByte[55];
      667.  
        fpByte[ 5] = endByte[23];
      668.  
        fpByte[ 6] = endByte[63];
      669.  
        fpByte[ 7] = endByte[31];
      670.  
        fpByte[ 8] = endByte[38];
      671.  
        fpByte[ 9] = endByte[ 6];
      672.  
        fpByte[10] = endByte[46];
      673.  
        fpByte[11] = endByte[14];
      674.  
        fpByte[12] = endByte[54];
      675.  
        fpByte[13] = endByte[22];
      676.  
        fpByte[14] = endByte[62];
      677.  
        fpByte[15] = endByte[30];
      678.  
        fpByte[16] = endByte[37];
      679.  
        fpByte[17] = endByte[ 5];
      680.  
        fpByte[18] = endByte[45];
      681.  
        fpByte[19] = endByte[13];
      682.  
        fpByte[20] = endByte[53];
      683.  
        fpByte[21] = endByte[21];
      684.  
        fpByte[22] = endByte[61];
      685.  
        fpByte[23] = endByte[29];
      686.  
        fpByte[24] = endByte[36];
      687.  
        fpByte[25] = endByte[ 4];
      688.  
        fpByte[26] = endByte[44];
      689.  
        fpByte[27] = endByte[12];
      690.  
        fpByte[28] = endByte[52];
      691.  
        fpByte[29] = endByte[20];
      692.  
        fpByte[30] = endByte[60];
      693.  
        fpByte[31] = endByte[28];
      694.  
        fpByte[32] = endByte[35];
      695.  
        fpByte[33] = endByte[ 3];
      696.  
        fpByte[34] = endByte[43];
      697.  
        fpByte[35] = endByte[11];
      698.  
        fpByte[36] = endByte[51];
      699.  
        fpByte[37] = endByte[19];
      700.  
        fpByte[38] = endByte[59];
      701.  
        fpByte[39] = endByte[27];
      702.  
        fpByte[40] = endByte[34];
      703.  
        fpByte[41] = endByte[ 2];
      704.  
        fpByte[42] = endByte[42];
      705.  
        fpByte[43] = endByte[10];
      706.  
        fpByte[44] = endByte[50];
      707.  
        fpByte[45] = endByte[18];
      708.  
        fpByte[46] = endByte[58];
      709.  
        fpByte[47] = endByte[26];
      710.  
        fpByte[48] = endByte[33];
      711.  
        fpByte[49] = endByte[ 1];
      712.  
        fpByte[50] = endByte[41];
      713.  
        fpByte[51] = endByte[ 9];
      714.  
        fpByte[52] = endByte[49];
      715.  
        fpByte[53] = endByte[17];
      716.  
        fpByte[54] = endByte[57];
      717.  
        fpByte[55] = endByte[25];
      718.  
        fpByte[56] = endByte[32];
      719.  
        fpByte[57] = endByte[ 0];
      720.  
        fpByte[58] = endByte[40];
      721.  
        fpByte[59] = endByte[ 8];
      722.  
        fpByte[60] = endByte[48];
      723.  
        fpByte[61] = endByte[16];
      724.  
        fpByte[62] = endByte[56];
      725.  
        fpByte[63] = endByte[24];
      726.  
        return fpByte;
      727.  
        }
      728.  
         
      729.  
        function getBoxBinary(i) {
      730.  
        var binary = "";
      731.  
        switch (i) {
      732.  
        case 0 :binary = "0000";break;
      733.  
        case 1 :binary = "0001";break;
      734.  
        case 2 :binary = "0010";break;
      735.  
        case 3 :binary = "0011";break;
      736.  
        case 4 :binary = "0100";break;
      737.  
        case 5 :binary = "0101";break;
      738.  
        case 6 :binary = "0110";break;
      739.  
        case 7 :binary = "0111";break;
      740.  
        case 8 :binary = "1000";break;
      741.  
        case 9 :binary = "1001";break;
      742.  
        case 10 :binary = "1010";break;
      743.  
        case 11 :binary = "1011";break;
      744.  
        case 12 :binary = "1100";break;
      745.  
        case 13 :binary = "1101";break;
      746.  
        case 14 :binary = "1110";break;
      747.  
        case 15 :binary = "1111";break;
      748.  
        }
      749.  
        return binary;
      750.  
        }
      751.  
        /*
      752.  
        * generate 16 keys for xor
      753.  
        *
      754.  
        */
      755.  
        function generateKeys(keyByte){
      756.  
        var key = new Array(56);
      757.  
        var keys = new Array();
      758.  
         
      759.  
        keys[ 0] = new Array();
      760.  
        keys[ 1] = new Array();
      761.  
        keys[ 2] = new Array();
      762.  
        keys[ 3] = new Array();
      763.  
        keys[ 4] = new Array();
      764.  
        keys[ 5] = new Array();
      765.  
        keys[ 6] = new Array();
      766.  
        keys[ 7] = new Array();
      767.  
        keys[ 8] = new Array();
      768.  
        keys[ 9] = new Array();
      769.  
        keys[10] = new Array();
      770.  
        keys[11] = new Array();
      771.  
        keys[12] = new Array();
      772.  
        keys[13] = new Array();
      773.  
        keys[14] = new Array();
      774.  
        keys[15] = new Array();
      775.  
        var loop = [1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1];
      776.  
         
      777.  
        for(i=0;i<7;i++){
      778.  
        for(j=0,k=7;j<8;j++,k--){
      779.  
        key[i*8+j]=keyByte[8*k+i];
      780.  
        }
      781.  
        }
      782.  
         
      783.  
        var i = 0;
      784.  
        for(i = 0;i < 16;i ++){
      785.  
        var tempLeft=0;
      786.  
        var tempRight=0;
      787.  
        for(j = 0; j < loop[i];j ++){
      788.  
        tempLeft = key[0];
      789.  
        tempRight = key[28];
      790.  
        for(k = 0;k < 27 ;k ++){
      791.  
        key[k] = key[k+1];
      792.  
        key[28+k] = key[29+k];
      793.  
        }
      794.  
        key[27]=tempLeft;
      795.  
        key[55]=tempRight;
      796.  
        }
      797.  
        var tempKey = new Array(48);
      798.  
        tempKey[ 0] = key[13];
      799.  
        tempKey[ 1] = key[16];
      800.  
        tempKey[ 2] = key[10];
      801.  
        tempKey[ 3] = key[23];
      802.  
        tempKey[ 4] = key[ 0];
      803.  
        tempKey[ 5] = key[ 4];
      804.  
        tempKey[ 6] = key[ 2];
      805.  
        tempKey[ 7] = key[27];
      806.  
        tempKey[ 8] = key[14];
      807.  
        tempKey[ 9] = key[ 5];
      808.  
        tempKey[10] = key[20];
      809.  
        tempKey[11] = key[ 9];
      810.  
        tempKey[12] = key[22];
      811.  
        tempKey[13] = key[18];
      812.  
        tempKey[14] = key[11];
      813.  
        tempKey[15] = key[ 3];
      814.  
        tempKey[16] = key[25];
      815.  
        tempKey[17] = key[ 7];
      816.  
        tempKey[18] = key[15];
      817.  
        tempKey[19] = key[ 6];
      818.  
        tempKey[20] = key[26];
      819.  
        tempKey[21] = key[19];
      820.  
        tempKey[22] = key[12];
      821.  
        tempKey[23] = key[ 1];
      822.  
        tempKey[24] = key[40];
      823.  
        tempKey[25] = key[51];
      824.  
        tempKey[26] = key[30];
      825.  
        tempKey[27] = key[36];
      826.  
        tempKey[28] = key[46];
      827.  
        tempKey[29] = key[54];
      828.  
        tempKey[30] = key[29];
      829.  
        tempKey[31] = key[39];
      830.  
        tempKey[32] = key[50];
      831.  
        tempKey[33] = key[44];
      832.  
        tempKey[34] = key[32];
      833.  
        tempKey[35] = key[47];
      834.  
        tempKey[36] = key[43];
      835.  
        tempKey[37] = key[48];
      836.  
        tempKey[38] = key[38];
      837.  
        tempKey[39] = key[55];
      838.  
        tempKey[40] = key[33];
      839.  
        tempKey[41] = key[52];
      840.  
        tempKey[42] = key[45];
      841.  
        tempKey[43] = key[41];
      842.  
        tempKey[44] = key[49];
      843.  
        tempKey[45] = key[35];
      844.  
        tempKey[46] = key[28];
      845.  
        tempKey[47] = key[31];
      846.  
        switch(i){
      847.  
        case 0: for(m=0;m < 48 ;m++){ keys[ 0][m] = tempKey[m]; } break;
      848.  
        case 1: for(m=0;m < 48 ;m++){ keys[ 1][m] = tempKey[m]; } break;
      849.  
        case 2: for(m=0;m < 48 ;m++){ keys[ 2][m] = tempKey[m]; } break;
      850.  
        case 3: for(m=0;m < 48 ;m++){ keys[ 3][m] = tempKey[m]; } break;
      851.  
        case 4: for(m=0;m < 48 ;m++){ keys[ 4][m] = tempKey[m]; } break;
      852.  
        case 5: for(m=0;m < 48 ;m++){ keys[ 5][m] = tempKey[m]; } break;
      853.  
        case 6: for(m=0;m < 48 ;m++){ keys[ 6][m] = tempKey[m]; } break;
      854.  
        case 7: for(m=0;m < 48 ;m++){ keys[ 7][m] = tempKey[m]; } break;
      855.  
        case 8: for(m=0;m < 48 ;m++){ keys[ 8][m] = tempKey[m]; } break;
      856.  
        case 9: for(m=0;m < 48 ;m++){ keys[ 9][m] = tempKey[m]; } break;
      857.  
        case 10: for(m=0;m < 48 ;m++){ keys[10][m] = tempKey[m]; } break;
      858.  
        case 11: for(m=0;m < 48 ;m++){ keys[11][m] = tempKey[m]; } break;
      859.  
        case 12: for(m=0;m < 48 ;m++){ keys[12][m] = tempKey[m]; } break;
      860.  
        case 13: for(m=0;m < 48 ;m++){ keys[13][m] = tempKey[m]; } break;
      861.  
        case 14: for(m=0;m < 48 ;m++){ keys[14][m] = tempKey[m]; } break;
      862.  
        case 15: for(m=0;m < 48 ;m++){ keys[15][m] = tempKey[m]; } break;
      863.  
        }
      864.  
        }
      865.  
        return keys;
      866.  
        }
  • 相关阅读:
    2018年4月22日笔记
    2018年4月19日笔记
    2018年4月17日笔记
    2018年4月14日笔记
    2018年4月12日笔记
    课堂练习找水王
    评价软件
    第十一周进度条
    典型用户场景、用户场景描述
    构建之法阅读笔记04
  • 原文地址:https://www.cnblogs.com/zengxiangcai/p/9246817.html
Copyright © 2011-2022 走看看