欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java实现与JS相同的Des加解密算法完整实例

程序员文章站 2023-12-22 13:41:28
本文实例讲述了java实现与js相同的des加解密算法。分享给大家供大家参考,具体如下: 这里演示java与js实现相同的des加解密算法,不多说,不废话,直接上代码...

本文实例讲述了java实现与js相同的des加解密算法。分享给大家供大家参考,具体如下:

这里演示java与js实现相同的des加解密算法,不多说,不废话,直接上代码

一、java实现

package com.lyz.base.des;
import java.util.arraylist;
import java.util.list;
/**
 * des加密/解密
 *
 * @copyright copyright (c) 2015
 * @author liuyazhuang
 * @see descore
 */
public class des {
  public des() {
  }
  public static void main(string[] args) {
    des desobj = new des();
    string key1 = "1";
    string key2 = "2";
    string key3 = "3";
    string data = "jb51.net";
    string str = desobj.strenc(data, key1, key2, key3);
    system.out.println("测试结果:");
    system.out.println(str);
    string dec = desobj.strdec(str, key1, key2, key3);
    system.out.println(dec);
  }
  /**
   * des加密/解密
   *
   * @copyright copyright (c) 2015
   * @author liuyazhuang
   * @see descore
   */
  /*
   * encrypt the string to string made up of hex return the encrypted string
   */
  public string strenc(string data, string firstkey, string secondkey,
      string thirdkey) {
    int leng = data.length();
    string encdata = "";
    list firstkeybt = null, secondkeybt = null, thirdkeybt = null;
    int firstlength = 0, secondlength = 0, thirdlength = 0;
    if (firstkey != null && firstkey != "") {
      firstkeybt = getkeybytes(firstkey);
      firstlength = firstkeybt.size();
    }
    if (secondkey != null && secondkey != "") {
      secondkeybt = getkeybytes(secondkey);
      secondlength = secondkeybt.size();
    }
    if (thirdkey != null && thirdkey != "") {
      thirdkeybt = getkeybytes(thirdkey);
      thirdlength = thirdkeybt.size();
    }
    if (leng > 0) {
      if (leng < 4) {
        int[] bt = strtobt(data);
        int[] encbyte = null;
        if (firstkey != null && firstkey != "" && secondkey != null
            && secondkey != "" && thirdkey != null
            && thirdkey != "") {
          int[] tempbt;
          int x, y, z;
          tempbt = bt;
          for (x = 0; x < firstlength; x++) {
            tempbt = enc(tempbt, (int[]) firstkeybt.get(x));
          }
          for (y = 0; y < secondlength; y++) {
            tempbt = enc(tempbt, (int[]) secondkeybt.get(y));
          }
          for (z = 0; z < thirdlength; z++) {
            tempbt = enc(tempbt, (int[]) thirdkeybt.get(z));
          }
          encbyte = tempbt;
        } else {
          if (firstkey != null && firstkey != "" && secondkey != null
              && secondkey != "") {
            int[] tempbt;
            int x, y;
            tempbt = bt;
            for (x = 0; x < firstlength; x++) {
              tempbt = enc(tempbt, (int[]) firstkeybt.get(x));
            }
            for (y = 0; y < secondlength; y++) {
              tempbt = enc(tempbt, (int[]) secondkeybt.get(y));
            }
            encbyte = tempbt;
          } else {
            if (firstkey != null && firstkey != "") {
              int[] tempbt;
              int x = 0;
              tempbt = bt;
              for (x = 0; x < firstlength; x++) {
                tempbt = enc(tempbt, (int[]) firstkeybt.get(x));
              }
              encbyte = tempbt;
            }
          }
        }
        encdata = bt64tohex(encbyte);
      } else {
        int iterator = (leng / 4);
        int remainder = leng % 4;
        int i = 0;
        for (i = 0; i < iterator; i++) {
          string tempdata = data.substring(i * 4 + 0, i * 4 + 4);
          int[] tempbyte = strtobt(tempdata);
          int[] encbyte = null;
          if (firstkey != null && firstkey != "" && secondkey != null
              && secondkey != "" && thirdkey != null
              && thirdkey != "") {
            int[] tempbt;
            int x, y, z;
            tempbt = tempbyte;
            for (x = 0; x < firstlength; x++) {
              tempbt = enc(tempbt, (int[]) firstkeybt.get(x));
            }
            for (y = 0; y < secondlength; y++) {
              tempbt = enc(tempbt, (int[]) secondkeybt.get(y));
            }
            for (z = 0; z < thirdlength; z++) {
              tempbt = enc(tempbt, (int[]) thirdkeybt.get(z));
            }
            encbyte = tempbt;
          } else {
            if (firstkey != null && firstkey != ""
                && secondkey != null && secondkey != "") {
              int[] tempbt;
              int x, y;
              tempbt = tempbyte;
              for (x = 0; x < firstlength; x++) {
                tempbt = enc(tempbt, (int[]) firstkeybt.get(x));
              }
              for (y = 0; y < secondlength; y++) {
                tempbt = enc(tempbt, (int[]) secondkeybt.get(y));
              }
              encbyte = tempbt;
            } else {
              if (firstkey != null && firstkey != "") {
                int[] tempbt;
                int x;
                tempbt = tempbyte;
                for (x = 0; x < firstlength; x++) {
                  tempbt = enc(tempbt, (int[]) firstkeybt
                      .get(x));
                }
                encbyte = tempbt;
              }
            }
          }
          encdata += bt64tohex(encbyte);
        }
        if (remainder > 0) {
          string remainderdata = data.substring(iterator * 4 + 0,
              leng);
          int[] tempbyte = strtobt(remainderdata);
          int[] encbyte = null;
          if (firstkey != null && firstkey != "" && secondkey != null
              && secondkey != "" && thirdkey != null
              && thirdkey != "") {
            int[] tempbt;
            int x, y, z;
            tempbt = tempbyte;
            for (x = 0; x < firstlength; x++) {
              tempbt = enc(tempbt, (int[]) firstkeybt.get(x));
            }
            for (y = 0; y < secondlength; y++) {
              tempbt = enc(tempbt, (int[]) secondkeybt.get(y));
            }
            for (z = 0; z < thirdlength; z++) {
              tempbt = enc(tempbt, (int[]) thirdkeybt.get(z));
            }
            encbyte = tempbt;
          } else {
            if (firstkey != null && firstkey != ""
                && secondkey != null && secondkey != "") {
              int[] tempbt;
              int x, y;
              tempbt = tempbyte;
              for (x = 0; x < firstlength; x++) {
                tempbt = enc(tempbt, (int[]) firstkeybt.get(x));
              }
              for (y = 0; y < secondlength; y++) {
                tempbt = enc(tempbt, (int[]) secondkeybt.get(y));
              }
              encbyte = tempbt;
            } else {
              if (firstkey != null && firstkey != "") {
                int[] tempbt;
                int x;
                tempbt = tempbyte;
                for (x = 0; x < firstlength; x++) {
                  tempbt = enc(tempbt, (int[]) firstkeybt
                      .get(x));
                }
                encbyte = tempbt;
              }
            }
          }
          encdata += bt64tohex(encbyte);
        }
      }
    }
    return encdata;
  }
  /*
   * decrypt the encrypted string to the original string
   * return the original string
   */
  public string strdec(string data, string firstkey, string secondkey,
      string thirdkey) {
    int leng = data.length();
    string decstr = "";
    list firstkeybt = null, secondkeybt = null, thirdkeybt = null;
    int firstlength = 0, secondlength = 0, thirdlength = 0;
    if (firstkey != null && firstkey != "") {
      firstkeybt = getkeybytes(firstkey);
      firstlength = firstkeybt.size();
    }
    if (secondkey != null && secondkey != "") {
      secondkeybt = getkeybytes(secondkey);
      secondlength = secondkeybt.size();
    }
    if (thirdkey != null && thirdkey != "") {
      thirdkeybt = getkeybytes(thirdkey);
      thirdlength = thirdkeybt.size();
    }
    int iterator = leng / 16;
    int i = 0;
    for (i = 0; i < iterator; i++) {
      string tempdata = data.substring(i * 16 + 0, i * 16 + 16);
      string strbyte = hextobt64(tempdata);
      int[] intbyte = new int[64];
      int j = 0;
      for (j = 0; j < 64; j++) {
        intbyte[j] = integer.parseint(strbyte.substring(j, j + 1));
      }
      int[] decbyte = null;
      if (firstkey != null && firstkey != "" && secondkey != null
          && secondkey != "" && thirdkey != null && thirdkey != "") {
        int[] tempbt;
        int x, y, z;
        tempbt = intbyte;
        for (x = thirdlength - 1; x >= 0; x--) {
          tempbt = dec(tempbt, (int[]) thirdkeybt.get(x));
        }
        for (y = secondlength - 1; y >= 0; y--) {
          tempbt = dec(tempbt, (int[]) secondkeybt.get(y));
        }
        for (z = firstlength - 1; z >= 0; z--) {
          tempbt = dec(tempbt, (int[]) firstkeybt.get(z));
        }
        decbyte = tempbt;
      } else {
        if (firstkey != null && firstkey != "" && secondkey != null
            && secondkey != "") {
          int[] tempbt;
          int x, y, z;
          tempbt = intbyte;
          for (x = secondlength - 1; x >= 0; x--) {
            tempbt = dec(tempbt, (int[]) secondkeybt.get(x));
          }
          for (y = firstlength - 1; y >= 0; y--) {
            tempbt = dec(tempbt, (int[]) firstkeybt.get(y));
          }
          decbyte = tempbt;
        } else {
          if (firstkey != null && firstkey != "") {
            int[] tempbt;
            int x, y, z;
            tempbt = intbyte;
            for (x = firstlength - 1; x >= 0; x--) {
              tempbt = dec(tempbt, (int[]) firstkeybt.get(x));
            }
            decbyte = tempbt;
          }
        }
      }
      decstr += bytetostring(decbyte);
    }
    return decstr;
  }
  /*
   * chang the string into the bit array
   *
   * return bit array(it's length % 64 = 0)
   */
  public list getkeybytes(string key) {
    list keybytes = new arraylist();
    int leng = key.length();
    int iterator = (leng / 4);
    int remainder = leng % 4;
    int i = 0;
    for (i = 0; i < iterator; i++) {
      keybytes.add(i, strtobt(key.substring(i * 4 + 0, i * 4 + 4)));
    }
    if (remainder > 0) {
      // keybytes[i] = strtobt(key.substring(i*4+0,leng));
      keybytes.add(i, strtobt(key.substring(i * 4 + 0, leng)));
    }
    return keybytes;
  }
  /*
   * chang the string(it's length <= 4) into the bit array
   *
   * return bit array(it's length = 64)
   */
  public int[] strtobt(string str) {
    int leng = str.length();
    int[] bt = new int[64];
    if (leng < 4) {
      int i = 0, j = 0, p = 0, q = 0;
      for (i = 0; i < leng; i++) {
        int k = str.charat(i);
        for (j = 0; j < 16; j++) {
          int pow = 1, m = 0;
          for (m = 15; m > j; m--) {
            pow *= 2;
          }
          // bt.set(16*i+j,""+(k/pow)%2));
          bt[16 * i + j] = (k / pow) % 2;
        }
      }
      for (p = leng; p < 4; p++) {
        int k = 0;
        for (q = 0; q < 16; q++) {
          int pow = 1, m = 0;
          for (m = 15; m > q; m--) {
            pow *= 2;
          }
          // bt[16*p+q]=parseint(k/pow)%2;
          // bt.add(16*p+q,""+((k/pow)%2));
          bt[16 * p + q] = (k / pow) % 2;
        }
      }
    } else {
      for (int i = 0; i < 4; i++) {
        int k = str.charat(i);
        for (int j = 0; j < 16; j++) {
          int pow = 1;
          for (int m = 15; m > j; m--) {
            pow *= 2;
          }
          // bt[16*i+j]=parseint(k/pow)%2;
          // bt.add(16*i+j,""+((k/pow)%2));
          bt[16 * i + j] = (k / pow) % 2;
        }
      }
    }
    return bt;
  }
  /*
   * chang the bit(it's length = 4) into the hex
   *
   * return hex
   */
  public string bt4tohex(string binary) {
    string hex = "";
    if (binary.equalsignorecase("0000")) {
      hex = "0";
    } else if (binary.equalsignorecase("0001")) {
      hex = "1";
    } else if (binary.equalsignorecase("0010")) {
      hex = "2";
    } else if (binary.equalsignorecase("0011")) {
      hex = "3";
    } else if (binary.equalsignorecase("0100")) {
      hex = "4";
    } else if (binary.equalsignorecase("0101")) {
      hex = "5";
    } else if (binary.equalsignorecase("0110")) {
      hex = "6";
    } else if (binary.equalsignorecase("0111")) {
      hex = "7";
    } else if (binary.equalsignorecase("1000")) {
      hex = "8";
    } else if (binary.equalsignorecase("1001")) {
      hex = "9";
    } else if (binary.equalsignorecase("1010")) {
      hex = "a";
    } else if (binary.equalsignorecase("1011")) {
      hex = "b";
    } else if (binary.equalsignorecase("1100")) {
      hex = "c";
    } else if (binary.equalsignorecase("1101")) {
      hex = "d";
    } else if (binary.equalsignorecase("1110")) {
      hex = "e";
    } else if (binary.equalsignorecase("1111")) {
      hex = "f";
    }
    return hex;
  }
  /*
   * chang the hex into the bit(it's length = 4)
   *
   * return the bit(it's length = 4)
   */
  public string hextobt4(string hex) {
    string binary = "";
    if (hex.equalsignorecase("0")) {
      binary = "0000";
    } else if (hex.equalsignorecase("1")) {
      binary = "0001";
    }
    if (hex.equalsignorecase("2")) {
      binary = "0010";
    }
    if (hex.equalsignorecase("3")) {
      binary = "0011";
    }
    if (hex.equalsignorecase("4")) {
      binary = "0100";
    }
    if (hex.equalsignorecase("5")) {
      binary = "0101";
    }
    if (hex.equalsignorecase("6")) {
      binary = "0110";
    }
    if (hex.equalsignorecase("7")) {
      binary = "0111";
    }
    if (hex.equalsignorecase("8")) {
      binary = "1000";
    }
    if (hex.equalsignorecase("9")) {
      binary = "1001";
    }
    if (hex.equalsignorecase("a")) {
      binary = "1010";
    }
    if (hex.equalsignorecase("b")) {
      binary = "1011";
    }
    if (hex.equalsignorecase("c")) {
      binary = "1100";
    }
    if (hex.equalsignorecase("d")) {
      binary = "1101";
    }
    if (hex.equalsignorecase("e")) {
      binary = "1110";
    }
    if (hex.equalsignorecase("f")) {
      binary = "1111";
    }
    return binary;
  }
  /*
   * chang the bit(it's length = 64) into the string
   *
   * return string
   */
  public string bytetostring(int[] bytedata) {
    string str = "";
    for (int i = 0; i < 4; i++) {
      int count = 0;
      for (int j = 0; j < 16; j++) {
        int pow = 1;
        for (int m = 15; m > j; m--) {
          pow *= 2;
        }
        count += bytedata[16 * i + j] * pow;
      }
      if (count != 0) {
        str += "" + (char) (count);
      }
    }
    return str;
  }
  public string bt64tohex(int[] bytedata) {
    string hex = "";
    for (int i = 0; i < 16; i++) {
      string bt = "";
      for (int j = 0; j < 4; j++) {
        bt += bytedata[i * 4 + j];
      }
      hex += bt4tohex(bt);
    }
    return hex;
  }
  public string hextobt64(string hex) {
    string binary = "";
    for (int i = 0; i < 16; i++) {
      binary += hextobt4(hex.substring(i, i + 1));
    }
    return binary;
  }
  /*
   * the 64 bit des core arithmetic
   */
  public int[] enc(int[] databyte, int[] keybyte) {
    int[][] keys = generatekeys(keybyte);
    int[] ipbyte = initpermute(databyte);
    int[] ipleft = new int[32];
    int[] ipright = new int[32];
    int[] templeft = new int[32];
    int i = 0, j = 0, k = 0, m = 0, n = 0;
    for (k = 0; k < 32; k++) {
      ipleft[k] = ipbyte[k];
      ipright[k] = ipbyte[32 + k];
    }
    for (i = 0; i < 16; i++) {
      for (j = 0; j < 32; j++) {
        templeft[j] = ipleft[j];
        ipleft[j] = ipright[j];
      }
      int[] key = new int[48];
      for (m = 0; m < 48; m++) {
        key[m] = keys[i][m];
      }
      int[] tempright = xor(ppermute(sboxpermute(xor(
          expandpermute(ipright), key))), templeft);
      for (n = 0; n < 32; n++) {
        ipright[n] = tempright[n];
      }
    }
    int[] finaldata = new int[64];
    for (i = 0; i < 32; i++) {
      finaldata[i] = ipright[i];
      finaldata[32 + i] = ipleft[i];
    }
    return finallypermute(finaldata);
  }
  public int[] dec(int[] databyte, int[] keybyte) {
    int[][] keys = generatekeys(keybyte);
    int[] ipbyte = initpermute(databyte);
    int[] ipleft = new int[32];
    int[] ipright = new int[32];
    int[] templeft = new int[32];
    int i = 0, j = 0, k = 0, m = 0, n = 0;
    for (k = 0; k < 32; k++) {
      ipleft[k] = ipbyte[k];
      ipright[k] = ipbyte[32 + k];
    }
    for (i = 15; i >= 0; i--) {
      for (j = 0; j < 32; j++) {
        templeft[j] = ipleft[j];
        ipleft[j] = ipright[j];
      }
      int[] key = new int[48];
      for (m = 0; m < 48; m++) {
        key[m] = keys[i][m];
      }
      int[] tempright = xor(ppermute(sboxpermute(xor(
          expandpermute(ipright), key))), templeft);
      for (n = 0; n < 32; n++) {
        ipright[n] = tempright[n];
      }
    }
    int[] finaldata = new int[64];
    for (i = 0; i < 32; i++) {
      finaldata[i] = ipright[i];
      finaldata[32 + i] = ipleft[i];
    }
    return finallypermute(finaldata);
  }
  public int[] initpermute(int[] originaldata) {
    int[] ipbyte = new int[64];
    int i = 0, m = 1, n = 0, j, k;
    for (i = 0, m = 1, n = 0; i < 4; i++, m += 2, n += 2) {
      for (j = 7, k = 0; j >= 0; j--, k++) {
        ipbyte[i * 8 + k] = originaldata[j * 8 + m];
        ipbyte[i * 8 + k + 32] = originaldata[j * 8 + n];
      }
    }
    return ipbyte;
  }
  public int[] expandpermute(int[] rightdata) {
    int[] epbyte = new int[48];
    int i, j;
    for (i = 0; i < 8; i++) {
      if (i == 0) {
        epbyte[i * 6 + 0] = rightdata[31];
      } else {
        epbyte[i * 6 + 0] = rightdata[i * 4 - 1];
      }
      epbyte[i * 6 + 1] = rightdata[i * 4 + 0];
      epbyte[i * 6 + 2] = rightdata[i * 4 + 1];
      epbyte[i * 6 + 3] = rightdata[i * 4 + 2];
      epbyte[i * 6 + 4] = rightdata[i * 4 + 3];
      if (i == 7) {
        epbyte[i * 6 + 5] = rightdata[0];
      } else {
        epbyte[i * 6 + 5] = rightdata[i * 4 + 4];
      }
    }
    return epbyte;
  }
  public int[] xor(int[] byteone, int[] bytetwo) {
    // var xorbyte = new array(byteone.length);
    // for(int i = 0;i < byteone.length; i ++){
    // xorbyte[i] = byteone[i] ^ bytetwo[i];
    // }
    // return xorbyte;
    int[] xorbyte = new int[byteone.length];
    for (int i = 0; i < byteone.length; i++) {
      xorbyte[i] = byteone[i] ^ bytetwo[i];
    }
    return xorbyte;
  }
  public int[] sboxpermute(int[] expandbyte) {
    // var sboxbyte = new array(32);
    int[] sboxbyte = new int[32];
    string binary = "";
    int[][] s1 = {
        { 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 } };
    /* table - s2 */
    int[][] s2 = {
        { 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 } };
    /* table - s3 */
    int[][] s3 = {
        { 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 } };
    /* table - s4 */
    int[][] s4 = {
        { 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 } };
    /* table - s5 */
    int[][] s5 = {
        { 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 } };
    /* table - s6 */
    int[][] s6 = {
        { 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 } };
    /* table - s7 */
    int[][] s7 = {
        { 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 } };
    /* table - s8 */
    int[][] s8 = {
        { 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 } };
    for (int m = 0; m < 8; m++) {
      int i = 0, j = 0;
      i = expandbyte[m * 6 + 0] * 2 + expandbyte[m * 6 + 5];
      j = expandbyte[m * 6 + 1] * 2 * 2 * 2 + expandbyte[m * 6 + 2] * 2
          * 2 + expandbyte[m * 6 + 3] * 2 + expandbyte[m * 6 + 4];
      switch (m) {
      case 0:
        binary = getboxbinary(s1[i][j]);
        break;
      case 1:
        binary = getboxbinary(s2[i][j]);
        break;
      case 2:
        binary = getboxbinary(s3[i][j]);
        break;
      case 3:
        binary = getboxbinary(s4[i][j]);
        break;
      case 4:
        binary = getboxbinary(s5[i][j]);
        break;
      case 5:
        binary = getboxbinary(s6[i][j]);
        break;
      case 6:
        binary = getboxbinary(s7[i][j]);
        break;
      case 7:
        binary = getboxbinary(s8[i][j]);
        break;
      }
      sboxbyte[m * 4 + 0] = integer.parseint(binary.substring(0, 1));
      sboxbyte[m * 4 + 1] = integer.parseint(binary.substring(1, 2));
      sboxbyte[m * 4 + 2] = integer.parseint(binary.substring(2, 3));
      sboxbyte[m * 4 + 3] = integer.parseint(binary.substring(3, 4));
    }
    return sboxbyte;
  }
  public int[] ppermute(int[] sboxbyte) {
    int[] pboxpermute = new int[32];
    pboxpermute[0] = sboxbyte[15];
    pboxpermute[1] = sboxbyte[6];
    pboxpermute[2] = sboxbyte[19];
    pboxpermute[3] = sboxbyte[20];
    pboxpermute[4] = sboxbyte[28];
    pboxpermute[5] = sboxbyte[11];
    pboxpermute[6] = sboxbyte[27];
    pboxpermute[7] = sboxbyte[16];
    pboxpermute[8] = sboxbyte[0];
    pboxpermute[9] = sboxbyte[14];
    pboxpermute[10] = sboxbyte[22];
    pboxpermute[11] = sboxbyte[25];
    pboxpermute[12] = sboxbyte[4];
    pboxpermute[13] = sboxbyte[17];
    pboxpermute[14] = sboxbyte[30];
    pboxpermute[15] = sboxbyte[9];
    pboxpermute[16] = sboxbyte[1];
    pboxpermute[17] = sboxbyte[7];
    pboxpermute[18] = sboxbyte[23];
    pboxpermute[19] = sboxbyte[13];
    pboxpermute[20] = sboxbyte[31];
    pboxpermute[21] = sboxbyte[26];
    pboxpermute[22] = sboxbyte[2];
    pboxpermute[23] = sboxbyte[8];
    pboxpermute[24] = sboxbyte[18];
    pboxpermute[25] = sboxbyte[12];
    pboxpermute[26] = sboxbyte[29];
    pboxpermute[27] = sboxbyte[5];
    pboxpermute[28] = sboxbyte[21];
    pboxpermute[29] = sboxbyte[10];
    pboxpermute[30] = sboxbyte[3];
    pboxpermute[31] = sboxbyte[24];
    return pboxpermute;
  }
  public int[] finallypermute(int[] endbyte) {
    int[] fpbyte = new int[64];
    fpbyte[0] = endbyte[39];
    fpbyte[1] = endbyte[7];
    fpbyte[2] = endbyte[47];
    fpbyte[3] = endbyte[15];
    fpbyte[4] = endbyte[55];
    fpbyte[5] = endbyte[23];
    fpbyte[6] = endbyte[63];
    fpbyte[7] = endbyte[31];
    fpbyte[8] = endbyte[38];
    fpbyte[9] = endbyte[6];
    fpbyte[10] = endbyte[46];
    fpbyte[11] = endbyte[14];
    fpbyte[12] = endbyte[54];
    fpbyte[13] = endbyte[22];
    fpbyte[14] = endbyte[62];
    fpbyte[15] = endbyte[30];
    fpbyte[16] = endbyte[37];
    fpbyte[17] = endbyte[5];
    fpbyte[18] = endbyte[45];
    fpbyte[19] = endbyte[13];
    fpbyte[20] = endbyte[53];
    fpbyte[21] = endbyte[21];
    fpbyte[22] = endbyte[61];
    fpbyte[23] = endbyte[29];
    fpbyte[24] = endbyte[36];
    fpbyte[25] = endbyte[4];
    fpbyte[26] = endbyte[44];
    fpbyte[27] = endbyte[12];
    fpbyte[28] = endbyte[52];
    fpbyte[29] = endbyte[20];
    fpbyte[30] = endbyte[60];
    fpbyte[31] = endbyte[28];
    fpbyte[32] = endbyte[35];
    fpbyte[33] = endbyte[3];
    fpbyte[34] = endbyte[43];
    fpbyte[35] = endbyte[11];
    fpbyte[36] = endbyte[51];
    fpbyte[37] = endbyte[19];
    fpbyte[38] = endbyte[59];
    fpbyte[39] = endbyte[27];
    fpbyte[40] = endbyte[34];
    fpbyte[41] = endbyte[2];
    fpbyte[42] = endbyte[42];
    fpbyte[43] = endbyte[10];
    fpbyte[44] = endbyte[50];
    fpbyte[45] = endbyte[18];
    fpbyte[46] = endbyte[58];
    fpbyte[47] = endbyte[26];
    fpbyte[48] = endbyte[33];
    fpbyte[49] = endbyte[1];
    fpbyte[50] = endbyte[41];
    fpbyte[51] = endbyte[9];
    fpbyte[52] = endbyte[49];
    fpbyte[53] = endbyte[17];
    fpbyte[54] = endbyte[57];
    fpbyte[55] = endbyte[25];
    fpbyte[56] = endbyte[32];
    fpbyte[57] = endbyte[0];
    fpbyte[58] = endbyte[40];
    fpbyte[59] = endbyte[8];
    fpbyte[60] = endbyte[48];
    fpbyte[61] = endbyte[16];
    fpbyte[62] = endbyte[56];
    fpbyte[63] = endbyte[24];
    return fpbyte;
  }
  public string getboxbinary(int i) {
    string binary = "";
    switch (i) {
    case 0:
      binary = "0000";
      break;
    case 1:
      binary = "0001";
      break;
    case 2:
      binary = "0010";
      break;
    case 3:
      binary = "0011";
      break;
    case 4:
      binary = "0100";
      break;
    case 5:
      binary = "0101";
      break;
    case 6:
      binary = "0110";
      break;
    case 7:
      binary = "0111";
      break;
    case 8:
      binary = "1000";
      break;
    case 9:
      binary = "1001";
      break;
    case 10:
      binary = "1010";
      break;
    case 11:
      binary = "1011";
      break;
    case 12:
      binary = "1100";
      break;
    case 13:
      binary = "1101";
      break;
    case 14:
      binary = "1110";
      break;
    case 15:
      binary = "1111";
      break;
    }
    return binary;
  }
  /*
   * generate 16 keys for xor
   *
   */
  public int[][] generatekeys(int[] keybyte) {
    int[] key = new int[56];
    int[][] keys = new int[16][48];
    // keys[ 0] = new array();
    // keys[ 1] = new array();
    // keys[ 2] = new array();
    // keys[ 3] = new array();
    // keys[ 4] = new array();
    // keys[ 5] = new array();
    // keys[ 6] = new array();
    // keys[ 7] = new array();
    // keys[ 8] = new array();
    // keys[ 9] = new array();
    // keys[10] = new array();
    // keys[11] = new array();
    // keys[12] = new array();
    // keys[13] = new array();
    // keys[14] = new array();
    // keys[15] = new array();
    int[] loop = new int[] { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
    for (int i = 0; i < 7; i++) {
      for (int j = 0, k = 7; j < 8; j++, k--) {
        key[i * 8 + j] = keybyte[8 * k + i];
      }
    }
    int i = 0;
    for (i = 0; i < 16; i++) {
      int templeft = 0;
      int tempright = 0;
      for (int j = 0; j < loop[i]; j++) {
        templeft = key[0];
        tempright = key[28];
        for (int k = 0; k < 27; k++) {
          key[k] = key[k + 1];
          key[28 + k] = key[29 + k];
        }
        key[27] = templeft;
        key[55] = tempright;
      }
      // var tempkey = new array(48);
      int[] tempkey = new int[48];
      tempkey[0] = key[13];
      tempkey[1] = key[16];
      tempkey[2] = key[10];
      tempkey[3] = key[23];
      tempkey[4] = key[0];
      tempkey[5] = key[4];
      tempkey[6] = key[2];
      tempkey[7] = key[27];
      tempkey[8] = key[14];
      tempkey[9] = key[5];
      tempkey[10] = key[20];
      tempkey[11] = key[9];
      tempkey[12] = key[22];
      tempkey[13] = key[18];
      tempkey[14] = key[11];
      tempkey[15] = key[3];
      tempkey[16] = key[25];
      tempkey[17] = key[7];
      tempkey[18] = key[15];
      tempkey[19] = key[6];
      tempkey[20] = key[26];
      tempkey[21] = key[19];
      tempkey[22] = key[12];
      tempkey[23] = key[1];
      tempkey[24] = key[40];
      tempkey[25] = key[51];
      tempkey[26] = key[30];
      tempkey[27] = key[36];
      tempkey[28] = key[46];
      tempkey[29] = key[54];
      tempkey[30] = key[29];
      tempkey[31] = key[39];
      tempkey[32] = key[50];
      tempkey[33] = key[44];
      tempkey[34] = key[32];
      tempkey[35] = key[47];
      tempkey[36] = key[43];
      tempkey[37] = key[48];
      tempkey[38] = key[38];
      tempkey[39] = key[55];
      tempkey[40] = key[33];
      tempkey[41] = key[52];
      tempkey[42] = key[45];
      tempkey[43] = key[41];
      tempkey[44] = key[49];
      tempkey[45] = key[35];
      tempkey[46] = key[28];
      tempkey[47] = key[31];
      int m;
      switch (i) {
      case 0:
        for (m = 0; m < 48; m++) {
          keys[0][m] = tempkey[m];
        }
        break;
      case 1:
        for (m = 0; m < 48; m++) {
          keys[1][m] = tempkey[m];
        }
        break;
      case 2:
        for (m = 0; m < 48; m++) {
          keys[2][m] = tempkey[m];
        }
        break;
      case 3:
        for (m = 0; m < 48; m++) {
          keys[3][m] = tempkey[m];
        }
        break;
      case 4:
        for (m = 0; m < 48; m++) {
          keys[4][m] = tempkey[m];
        }
        break;
      case 5:
        for (m = 0; m < 48; m++) {
          keys[5][m] = tempkey[m];
        }
        break;
      case 6:
        for (m = 0; m < 48; m++) {
          keys[6][m] = tempkey[m];
        }
        break;
      case 7:
        for (m = 0; m < 48; m++) {
          keys[7][m] = tempkey[m];
        }
        break;
      case 8:
        for (m = 0; m < 48; m++) {
          keys[8][m] = tempkey[m];
        }
        break;
      case 9:
        for (m = 0; m < 48; m++) {
          keys[9][m] = tempkey[m];
        }
        break;
      case 10:
        for (m = 0; m < 48; m++) {
          keys[10][m] = tempkey[m];
        }
        break;
      case 11:
        for (m = 0; m < 48; m++) {
          keys[11][m] = tempkey[m];
        }
        break;
      case 12:
        for (m = 0; m < 48; m++) {
          keys[12][m] = tempkey[m];
        }
        break;
      case 13:
        for (m = 0; m < 48; m++) {
          keys[13][m] = tempkey[m];
        }
        break;
      case 14:
        for (m = 0; m < 48; m++) {
          keys[14][m] = tempkey[m];
        }
        break;
      case 15:
        for (m = 0; m < 48; m++) {
          keys[15][m] = tempkey[m];
        }
        break;
      }
    }
    return keys;
  }
}

运行结果:

Java实现与JS相同的Des加解密算法完整实例

二、js实现

1、des.js文件

/**
* des加密/解密
* @copyright copyright (c) 2015
* @author liuyazhuang
* @see descore
*/
/*
* encrypt the string to string made up of hex
* return the encrypted string
*/
function strenc(data,firstkey,secondkey,thirdkey){
 var leng = data.length;
 var encdata = "";
 var firstkeybt,secondkeybt,thirdkeybt,firstlength,secondlength,thirdlength;
 if(firstkey != null && firstkey != ""){
  firstkeybt = getkeybytes(firstkey);
  firstlength = firstkeybt.length;
 }
 if(secondkey != null && secondkey != ""){
  secondkeybt = getkeybytes(secondkey);
  secondlength = secondkeybt.length;
 }
 if(thirdkey != null && thirdkey != ""){
  thirdkeybt = getkeybytes(thirdkey);
  thirdlength = thirdkeybt.length;
 }
 if(leng > 0){
  if(leng < 4){
   var bt = strtobt(data);
   var encbyte ;
   if(firstkey != null && firstkey !="" && secondkey != null && secondkey != "" && thirdkey != null && thirdkey != ""){
    var tempbt;
    var x,y,z;
    tempbt = bt;
    for(x = 0;x < firstlength ;x ++){
     tempbt = enc(tempbt,firstkeybt[x]);
    }
    for(y = 0;y < secondlength ;y ++){
     tempbt = enc(tempbt,secondkeybt[y]);
    }
    for(z = 0;z < thirdlength ;z ++){
     tempbt = enc(tempbt,thirdkeybt[z]);
    }
    encbyte = tempbt;
   }else{
    if(firstkey != null && firstkey !="" && secondkey != null && secondkey != ""){
     var tempbt;
     var x,y;
     tempbt = bt;
     for(x = 0;x < firstlength ;x ++){
      tempbt = enc(tempbt,firstkeybt[x]);
     }
     for(y = 0;y < secondlength ;y ++){
      tempbt = enc(tempbt,secondkeybt[y]);
     }
     encbyte = tempbt;
    }else{
     if(firstkey != null && firstkey !=""){
      var tempbt;
      var x = 0;
      tempbt = bt;
      for(x = 0;x < firstlength ;x ++){
       tempbt = enc(tempbt,firstkeybt[x]);
      }
      encbyte = tempbt;
     }
    }
   }
   encdata = bt64tohex(encbyte);
  }else{
   var iterator = parseint(leng/4);
   var remainder = leng%4;
   var i=0;
   for(i = 0;i < iterator;i++){
    var tempdata = data.substring(i*4+0,i*4+4);
    var tempbyte = strtobt(tempdata);
    var encbyte ;
    if(firstkey != null && firstkey !="" && secondkey != null && secondkey != "" && thirdkey != null && thirdkey != ""){
     var tempbt;
     var x,y,z;
     tempbt = tempbyte;
     for(x = 0;x < firstlength ;x ++){
      tempbt = enc(tempbt,firstkeybt[x]);
     }
     for(y = 0;y < secondlength ;y ++){
      tempbt = enc(tempbt,secondkeybt[y]);
     }
     for(z = 0;z < thirdlength ;z ++){
      tempbt = enc(tempbt,thirdkeybt[z]);
     }
     encbyte = tempbt;
    }else{
     if(firstkey != null && firstkey !="" && secondkey != null && secondkey != ""){
      var tempbt;
      var x,y;
      tempbt = tempbyte;
      for(x = 0;x < firstlength ;x ++){
       tempbt = enc(tempbt,firstkeybt[x]);
      }
      for(y = 0;y < secondlength ;y ++){
       tempbt = enc(tempbt,secondkeybt[y]);
      }
      encbyte = tempbt;
     }else{
      if(firstkey != null && firstkey !=""){
       var tempbt;
       var x;
       tempbt = tempbyte;
       for(x = 0;x < firstlength ;x ++){
        tempbt = enc(tempbt,firstkeybt[x]);
       }
       encbyte = tempbt;
      }
     }
    }
    encdata += bt64tohex(encbyte);
   }
   if(remainder > 0){
    var remainderdata = data.substring(iterator*4+0,leng);
    var tempbyte = strtobt(remainderdata);
    var encbyte ;
    if(firstkey != null && firstkey !="" && secondkey != null && secondkey != "" && thirdkey != null && thirdkey != ""){
     var tempbt;
     var x,y,z;
     tempbt = tempbyte;
     for(x = 0;x < firstlength ;x ++){
      tempbt = enc(tempbt,firstkeybt[x]);
     }
     for(y = 0;y < secondlength ;y ++){
      tempbt = enc(tempbt,secondkeybt[y]);
     }
     for(z = 0;z < thirdlength ;z ++){
      tempbt = enc(tempbt,thirdkeybt[z]);
     }
     encbyte = tempbt;
    }else{
     if(firstkey != null && firstkey !="" && secondkey != null && secondkey != ""){
      var tempbt;
      var x,y;
      tempbt = tempbyte;
      for(x = 0;x < firstlength ;x ++){
       tempbt = enc(tempbt,firstkeybt[x]);
      }
      for(y = 0;y < secondlength ;y ++){
       tempbt = enc(tempbt,secondkeybt[y]);
      }
      encbyte = tempbt;
     }else{
      if(firstkey != null && firstkey !=""){
       var tempbt;
       var x;
       tempbt = tempbyte;
       for(x = 0;x < firstlength ;x ++){
        tempbt = enc(tempbt,firstkeybt[x]);
       }
       encbyte = tempbt;
      }
     }
    }
    encdata += bt64tohex(encbyte);
   }
  }
 }
 return encdata;
}
/*
* decrypt the encrypted string to the original string
*
* return the original string
*/
function strdec(data,firstkey,secondkey,thirdkey){
 var leng = data.length;
 var decstr = "";
 var firstkeybt,secondkeybt,thirdkeybt,firstlength,secondlength,thirdlength;
 if(firstkey != null && firstkey != ""){
  firstkeybt = getkeybytes(firstkey);
  firstlength = firstkeybt.length;
 }
 if(secondkey != null && secondkey != ""){
  secondkeybt = getkeybytes(secondkey);
  secondlength = secondkeybt.length;
 }
 if(thirdkey != null && thirdkey != ""){
  thirdkeybt = getkeybytes(thirdkey);
  thirdlength = thirdkeybt.length;
 }
 var iterator = parseint(leng/16);
 var i=0;
 for(i = 0;i < iterator;i++){
  var tempdata = data.substring(i*16+0,i*16+16);
  var strbyte = hextobt64(tempdata);
  var intbyte = new array(64);
  var j = 0;
  for(j = 0;j < 64; j++){
   intbyte[j] = parseint(strbyte.substring(j,j+1));
  }
  var decbyte;
  if(firstkey != null && firstkey !="" && secondkey != null && secondkey != "" && thirdkey != null && thirdkey != ""){
   var tempbt;
   var x,y,z;
   tempbt = intbyte;
   for(x = thirdlength - 1;x >= 0;x --){
    tempbt = dec(tempbt,thirdkeybt[x]);
   }
   for(y = secondlength - 1;y >= 0;y --){
    tempbt = dec(tempbt,secondkeybt[y]);
   }
   for(z = firstlength - 1;z >= 0 ;z --){
    tempbt = dec(tempbt,firstkeybt[z]);
   }
   decbyte = tempbt;
  }else{
   if(firstkey != null && firstkey !="" && secondkey != null && secondkey != ""){
    var tempbt;
    var x,y,z;
    tempbt = intbyte;
    for(x = secondlength - 1;x >= 0 ;x --){
     tempbt = dec(tempbt,secondkeybt[x]);
    }
    for(y = firstlength - 1;y >= 0 ;y --){
     tempbt = dec(tempbt,firstkeybt[y]);
    }
    decbyte = tempbt;
   }else{
    if(firstkey != null && firstkey !=""){
     var tempbt;
     var x,y,z;
     tempbt = intbyte;
     for(x = firstlength - 1;x >= 0 ;x --){
      tempbt = dec(tempbt,firstkeybt[x]);
     }
     decbyte = tempbt;
    }
   }
  }
  decstr += bytetostring(decbyte);
 }
 return decstr;
}
/*
* chang the string into the bit array
*
* return bit array(it's length % 64 = 0)
*/
function getkeybytes(key){
 var keybytes = new array();
 var leng = key.length;
 var iterator = parseint(leng/4);
 var remainder = leng%4;
 var i = 0;
 for(i = 0;i < iterator; i ++){
  keybytes[i] = strtobt(key.substring(i*4+0,i*4+4));
 }
 if(remainder > 0){
  keybytes[i] = strtobt(key.substring(i*4+0,leng));
 }
 return keybytes;
}
/*
* chang the string(it's length <= 4) into the bit array
*
* return bit array(it's length = 64)
*/
function strtobt(str){
 var leng = str.length;
 var bt = new array(64);
 if(leng < 4){
  var i=0,j=0,p=0,q=0;
  for(i = 0;i<leng;i++){
   var k = str.charcodeat(i);
   for(j=0;j<16;j++){
    var pow=1,m=0;
    for(m=15;m>j;m--){
     pow *= 2;
    }
    bt[16*i+j]=parseint(k/pow)%2;
   }
  }
  for(p = leng;p<4;p++){
   var k = 0;
   for(q=0;q<16;q++){
    var pow=1,m=0;
    for(m=15;m>q;m--){
     pow *= 2;
    }
    bt[16*p+q]=parseint(k/pow)%2;
   }
  }
 }else{
  for(i = 0;i<4;i++){
   var k = str.charcodeat(i);
   for(j=0;j<16;j++){
    var pow=1;
    for(m=15;m>j;m--){
     pow *= 2;
    }
    bt[16*i+j]=parseint(k/pow)%2;
   }
  }
 }
 return bt;
}
/*
* chang the bit(it's length = 4) into the hex
*
* return hex
*/
function bt4tohex(binary) {
 var hex;
 switch (binary) {
  case "0000" : hex = "0"; break;
  case "0001" : hex = "1"; break;
  case "0010" : hex = "2"; break;
  case "0011" : hex = "3"; break;
  case "0100" : hex = "4"; break;
  case "0101" : hex = "5"; break;
  case "0110" : hex = "6"; break;
  case "0111" : hex = "7"; break;
  case "1000" : hex = "8"; break;
  case "1001" : hex = "9"; break;
  case "1010" : hex = "a"; break;
  case "1011" : hex = "b"; break;
  case "1100" : hex = "c"; break;
  case "1101" : hex = "d"; break;
  case "1110" : hex = "e"; break;
  case "1111" : hex = "f"; break;
 }
 return hex;
}
/*
* chang the hex into the bit(it's length = 4)
*
* return the bit(it's length = 4)
*/
function hextobt4(hex) {
 var binary;
 switch (hex) {
  case "0" : binary = "0000"; break;
  case "1" : binary = "0001"; break;
  case "2" : binary = "0010"; break;
  case "3" : binary = "0011"; break;
  case "4" : binary = "0100"; break;
  case "5" : binary = "0101"; break;
  case "6" : binary = "0110"; break;
  case "7" : binary = "0111"; break;
  case "8" : binary = "1000"; break;
  case "9" : binary = "1001"; break;
  case "a" : binary = "1010"; break;
  case "b" : binary = "1011"; break;
  case "c" : binary = "1100"; break;
  case "d" : binary = "1101"; break;
  case "e" : binary = "1110"; break;
  case "f" : binary = "1111"; break;
 }
 return binary;
}
/*
* chang the bit(it's length = 64) into the string
*
* return string
*/
function bytetostring(bytedata){
 var str="";
 for(i = 0;i<4;i++){
  var count=0;
  for(j=0;j<16;j++){
   var pow=1;
   for(m=15;m>j;m--){
    pow*=2;
   }
   count+=bytedata[16*i+j]*pow;
  }
  if(count != 0){
   str+=string.fromcharcode(count);
  }
 }
 return str;
}
function bt64tohex(bytedata){
 var hex = "";
 for(i = 0;i<16;i++){
  var bt = "";
  for(j=0;j<4;j++){
   bt += bytedata[i*4+j];
  }
  hex+=bt4tohex(bt);
 }
 return hex;
}
function hextobt64(hex){
 var binary = "";
 for(i = 0;i<16;i++){
  binary+=hextobt4(hex.substring(i,i+1));
 }
 return binary;
}
/*
* the 64 bit des core arithmetic
*/
function enc(databyte,keybyte){
 var keys = generatekeys(keybyte);
 var ipbyte  = initpermute(databyte);
 var ipleft  = new array(32);
 var ipright = new array(32);
 var templeft = new array(32);
 var i = 0,j = 0,k = 0,m = 0, n = 0;
 for(k = 0;k < 32;k ++){
  ipleft[k] = ipbyte[k];
  ipright[k] = ipbyte[32+k];
 }
 for(i = 0;i < 16;i ++){
  for(j = 0;j < 32;j ++){
   templeft[j] = ipleft[j];
   ipleft[j] = ipright[j];
  }
  var key = new array(48);
  for(m = 0;m < 48;m ++){
   key[m] = keys[i][m];
  }
  var tempright = xor(ppermute(sboxpermute(xor(expandpermute(ipright),key))), templeft);
  for(n = 0;n < 32;n ++){
   ipright[n] = tempright[n];
  }
 }
 var finaldata =new array(64);
 for(i = 0;i < 32;i ++){
  finaldata[i] = ipright[i];
  finaldata[32+i] = ipleft[i];
 }
 return finallypermute(finaldata);
}
function dec(databyte,keybyte){
 var keys = generatekeys(keybyte);
 var ipbyte  = initpermute(databyte);
 var ipleft  = new array(32);
 var ipright = new array(32);
 var templeft = new array(32);
 var i = 0,j = 0,k = 0,m = 0, n = 0;
 for(k = 0;k < 32;k ++){
  ipleft[k] = ipbyte[k];
  ipright[k] = ipbyte[32+k];
 }
 for(i = 15;i >= 0;i --){
  for(j = 0;j < 32;j ++){
   templeft[j] = ipleft[j];
   ipleft[j] = ipright[j];
  }
  var key = new array(48);
  for(m = 0;m < 48;m ++){
   key[m] = keys[i][m];
  }
  var tempright = xor(ppermute(sboxpermute(xor(expandpermute(ipright),key))), templeft);
  for(n = 0;n < 32;n ++){
   ipright[n] = tempright[n];
  }
 }
 var finaldata =new array(64);
 for(i = 0;i < 32;i ++){
  finaldata[i] = ipright[i];
  finaldata[32+i] = ipleft[i];
 }
 return finallypermute(finaldata);
}
function initpermute(originaldata){
 var ipbyte = new array(64);
 for (i = 0, m = 1, n = 0; i < 4; i++, m += 2, n += 2) {
  for (j = 7, k = 0; j >= 0; j--, k++) {
   ipbyte[i * 8 + k] = originaldata[j * 8 + m];
   ipbyte[i * 8 + k + 32] = originaldata[j * 8 + n];
  }
 }
 return ipbyte;
}
function expandpermute(rightdata){
 var epbyte = new array(48);
 for (i = 0; i < 8; i++) {
  if (i == 0) {
   epbyte[i * 6 + 0] = rightdata[31];
  } else {
   epbyte[i * 6 + 0] = rightdata[i * 4 - 1];
  }
  epbyte[i * 6 + 1] = rightdata[i * 4 + 0];
  epbyte[i * 6 + 2] = rightdata[i * 4 + 1];
  epbyte[i * 6 + 3] = rightdata[i * 4 + 2];
  epbyte[i * 6 + 4] = rightdata[i * 4 + 3];
  if (i == 7) {
   epbyte[i * 6 + 5] = rightdata[0];
  } else {
   epbyte[i * 6 + 5] = rightdata[i * 4 + 4];
  }
 }
 return epbyte;
}
function xor(byteone,bytetwo){
 var xorbyte = new array(byteone.length);
 for(i = 0;i < byteone.length; i ++){
  xorbyte[i] = byteone[i] ^ bytetwo[i];
 }
 return xorbyte;
}
function sboxpermute(expandbyte){
  var sboxbyte = new array(32);
  var binary = "";
  var s1 = [
    [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 ]];
    /* table - s2 */
  var s2 = [
    [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 ]];
    /* table - s3 */
  var s3= [
    [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 ]];
    /* table - s4 */
  var s4 = [
    [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 ]];
    /* table - s5 */
  var s5 = [
    [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 ]];
    /* table - s6 */
  var s6 = [
    [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 ]];
    /* table - s7 */
  var s7 = [
    [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]];
    /* table - s8 */
  var s8 = [
    [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]];
  for(m=0;m<8;m++){
  var i=0,j=0;
  i = expandbyte[m*6+0]*2+expandbyte[m*6+5];
  j = expandbyte[m * 6 + 1] * 2 * 2 * 2
   + expandbyte[m * 6 + 2] * 2* 2
   + expandbyte[m * 6 + 3] * 2
   + expandbyte[m * 6 + 4];
  switch (m) {
   case 0 :
    binary = getboxbinary(s1[i][j]);
    break;
   case 1 :
    binary = getboxbinary(s2[i][j]);
    break;
   case 2 :
    binary = getboxbinary(s3[i][j]);
    break;
   case 3 :
    binary = getboxbinary(s4[i][j]);
    break;
   case 4 :
    binary = getboxbinary(s5[i][j]);
    break;
   case 5 :
    binary = getboxbinary(s6[i][j]);
    break;
   case 6 :
    binary = getboxbinary(s7[i][j]);
    break;
   case 7 :
    binary = getboxbinary(s8[i][j]);
    break;
  }
  sboxbyte[m*4+0] = parseint(binary.substring(0,1));
  sboxbyte[m*4+1] = parseint(binary.substring(1,2));
  sboxbyte[m*4+2] = parseint(binary.substring(2,3));
  sboxbyte[m*4+3] = parseint(binary.substring(3,4));
 }
 return sboxbyte;
}
function ppermute(sboxbyte){
 var pboxpermute = new array(32);
 pboxpermute[ 0] = sboxbyte[15];
 pboxpermute[ 1] = sboxbyte[ 6];
 pboxpermute[ 2] = sboxbyte[19];
 pboxpermute[ 3] = sboxbyte[20];
 pboxpermute[ 4] = sboxbyte[28];
 pboxpermute[ 5] = sboxbyte[11];
 pboxpermute[ 6] = sboxbyte[27];
 pboxpermute[ 7] = sboxbyte[16];
 pboxpermute[ 8] = sboxbyte[ 0];
 pboxpermute[ 9] = sboxbyte[14];
 pboxpermute[10] = sboxbyte[22];
 pboxpermute[11] = sboxbyte[25];
 pboxpermute[12] = sboxbyte[ 4];
 pboxpermute[13] = sboxbyte[17];
 pboxpermute[14] = sboxbyte[30];
 pboxpermute[15] = sboxbyte[ 9];
 pboxpermute[16] = sboxbyte[ 1];
 pboxpermute[17] = sboxbyte[ 7];
 pboxpermute[18] = sboxbyte[23];
 pboxpermute[19] = sboxbyte[13];
 pboxpermute[20] = sboxbyte[31];
 pboxpermute[21] = sboxbyte[26];
 pboxpermute[22] = sboxbyte[ 2];
 pboxpermute[23] = sboxbyte[ 8];
 pboxpermute[24] = sboxbyte[18];
 pboxpermute[25] = sboxbyte[12];
 pboxpermute[26] = sboxbyte[29];
 pboxpermute[27] = sboxbyte[ 5];
 pboxpermute[28] = sboxbyte[21];
 pboxpermute[29] = sboxbyte[10];
 pboxpermute[30] = sboxbyte[ 3];
 pboxpermute[31] = sboxbyte[24];
 return pboxpermute;
}
function finallypermute(endbyte){
 var fpbyte = new array(64);
 fpbyte[ 0] = endbyte[39];
 fpbyte[ 1] = endbyte[ 7];
 fpbyte[ 2] = endbyte[47];
 fpbyte[ 3] = endbyte[15];
 fpbyte[ 4] = endbyte[55];
 fpbyte[ 5] = endbyte[23];
 fpbyte[ 6] = endbyte[63];
 fpbyte[ 7] = endbyte[31];
 fpbyte[ 8] = endbyte[38];
 fpbyte[ 9] = endbyte[ 6];
 fpbyte[10] = endbyte[46];
 fpbyte[11] = endbyte[14];
 fpbyte[12] = endbyte[54];
 fpbyte[13] = endbyte[22];
 fpbyte[14] = endbyte[62];
 fpbyte[15] = endbyte[30];
 fpbyte[16] = endbyte[37];
 fpbyte[17] = endbyte[ 5];
 fpbyte[18] = endbyte[45];
 fpbyte[19] = endbyte[13];
 fpbyte[20] = endbyte[53];
 fpbyte[21] = endbyte[21];
 fpbyte[22] = endbyte[61];
 fpbyte[23] = endbyte[29];
 fpbyte[24] = endbyte[36];
 fpbyte[25] = endbyte[ 4];
 fpbyte[26] = endbyte[44];
 fpbyte[27] = endbyte[12];
 fpbyte[28] = endbyte[52];
 fpbyte[29] = endbyte[20];
 fpbyte[30] = endbyte[60];
 fpbyte[31] = endbyte[28];
 fpbyte[32] = endbyte[35];
 fpbyte[33] = endbyte[ 3];
 fpbyte[34] = endbyte[43];
 fpbyte[35] = endbyte[11];
 fpbyte[36] = endbyte[51];
 fpbyte[37] = endbyte[19];
 fpbyte[38] = endbyte[59];
 fpbyte[39] = endbyte[27];
 fpbyte[40] = endbyte[34];
 fpbyte[41] = endbyte[ 2];
 fpbyte[42] = endbyte[42];
 fpbyte[43] = endbyte[10];
 fpbyte[44] = endbyte[50];
 fpbyte[45] = endbyte[18];
 fpbyte[46] = endbyte[58];
 fpbyte[47] = endbyte[26];
 fpbyte[48] = endbyte[33];
 fpbyte[49] = endbyte[ 1];
 fpbyte[50] = endbyte[41];
 fpbyte[51] = endbyte[ 9];
 fpbyte[52] = endbyte[49];
 fpbyte[53] = endbyte[17];
 fpbyte[54] = endbyte[57];
 fpbyte[55] = endbyte[25];
 fpbyte[56] = endbyte[32];
 fpbyte[57] = endbyte[ 0];
 fpbyte[58] = endbyte[40];
 fpbyte[59] = endbyte[ 8];
 fpbyte[60] = endbyte[48];
 fpbyte[61] = endbyte[16];
 fpbyte[62] = endbyte[56];
 fpbyte[63] = endbyte[24];
 return fpbyte;
}
function getboxbinary(i) {
 var binary = "";
 switch (i) {
  case 0 :binary = "0000";break;
  case 1 :binary = "0001";break;
  case 2 :binary = "0010";break;
  case 3 :binary = "0011";break;
  case 4 :binary = "0100";break;
  case 5 :binary = "0101";break;
  case 6 :binary = "0110";break;
  case 7 :binary = "0111";break;
  case 8 :binary = "1000";break;
  case 9 :binary = "1001";break;
  case 10 :binary = "1010";break;
  case 11 :binary = "1011";break;
  case 12 :binary = "1100";break;
  case 13 :binary = "1101";break;
  case 14 :binary = "1110";break;
  case 15 :binary = "1111";break;
 }
 return binary;
}
/*
* generate 16 keys for xor
*
*/
function generatekeys(keybyte){
 var key  = new array(56);
 var keys = new array();
 keys[ 0] = new array();
 keys[ 1] = new array();
 keys[ 2] = new array();
 keys[ 3] = new array();
 keys[ 4] = new array();
 keys[ 5] = new array();
 keys[ 6] = new array();
 keys[ 7] = new array();
 keys[ 8] = new array();
 keys[ 9] = new array();
 keys[10] = new array();
 keys[11] = new array();
 keys[12] = new array();
 keys[13] = new array();
 keys[14] = new array();
 keys[15] = new array();
 var loop = [1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1];
 for(i=0;i<7;i++){
  for(j=0,k=7;j<8;j++,k--){
   key[i*8+j]=keybyte[8*k+i];
  }
 }
 var i = 0;
 for(i = 0;i < 16;i ++){
  var templeft=0;
  var tempright=0;
  for(j = 0; j < loop[i];j ++){
   templeft = key[0];
   tempright = key[28];
   for(k = 0;k < 27 ;k ++){
    key[k] = key[k+1];
    key[28+k] = key[29+k];
   }
   key[27]=templeft;
   key[55]=tempright;
  }
  var tempkey = new array(48);
  tempkey[ 0] = key[13];
  tempkey[ 1] = key[16];
  tempkey[ 2] = key[10];
  tempkey[ 3] = key[23];
  tempkey[ 4] = key[ 0];
  tempkey[ 5] = key[ 4];
  tempkey[ 6] = key[ 2];
  tempkey[ 7] = key[27];
  tempkey[ 8] = key[14];
  tempkey[ 9] = key[ 5];
  tempkey[10] = key[20];
  tempkey[11] = key[ 9];
  tempkey[12] = key[22];
  tempkey[13] = key[18];
  tempkey[14] = key[11];
  tempkey[15] = key[ 3];
  tempkey[16] = key[25];
  tempkey[17] = key[ 7];
  tempkey[18] = key[15];
  tempkey[19] = key[ 6];
  tempkey[20] = key[26];
  tempkey[21] = key[19];
  tempkey[22] = key[12];
  tempkey[23] = key[ 1];
  tempkey[24] = key[40];
  tempkey[25] = key[51];
  tempkey[26] = key[30];
  tempkey[27] = key[36];
  tempkey[28] = key[46];
  tempkey[29] = key[54];
  tempkey[30] = key[29];
  tempkey[31] = key[39];
  tempkey[32] = key[50];
  tempkey[33] = key[44];
  tempkey[34] = key[32];
  tempkey[35] = key[47];
  tempkey[36] = key[43];
  tempkey[37] = key[48];
  tempkey[38] = key[38];
  tempkey[39] = key[55];
  tempkey[40] = key[33];
  tempkey[41] = key[52];
  tempkey[42] = key[45];
  tempkey[43] = key[41];
  tempkey[44] = key[49];
  tempkey[45] = key[35];
  tempkey[46] = key[28];
  tempkey[47] = key[31];
  switch(i){
   case 0: for(m=0;m < 48 ;m++){ keys[ 0][m] = tempkey[m]; } break;
   case 1: for(m=0;m < 48 ;m++){ keys[ 1][m] = tempkey[m]; } break;
   case 2: for(m=0;m < 48 ;m++){ keys[ 2][m] = tempkey[m]; } break;
   case 3: for(m=0;m < 48 ;m++){ keys[ 3][m] = tempkey[m]; } break;
   case 4: for(m=0;m < 48 ;m++){ keys[ 4][m] = tempkey[m]; } break;
   case 5: for(m=0;m < 48 ;m++){ keys[ 5][m] = tempkey[m]; } break;
   case 6: for(m=0;m < 48 ;m++){ keys[ 6][m] = tempkey[m]; } break;
   case 7: for(m=0;m < 48 ;m++){ keys[ 7][m] = tempkey[m]; } break;
   case 8: for(m=0;m < 48 ;m++){ keys[ 8][m] = tempkey[m]; } break;
   case 9: for(m=0;m < 48 ;m++){ keys[ 9][m] = tempkey[m]; } break;
   case 10: for(m=0;m < 48 ;m++){ keys[10][m] = tempkey[m]; } break;
   case 11: for(m=0;m < 48 ;m++){ keys[11][m] = tempkey[m]; } break;
   case 12: for(m=0;m < 48 ;m++){ keys[12][m] = tempkey[m]; } break;
   case 13: for(m=0;m < 48 ;m++){ keys[13][m] = tempkey[m]; } break;
   case 14: for(m=0;m < 48 ;m++){ keys[14][m] = tempkey[m]; } break;
   case 15: for(m=0;m < 48 ;m++){ keys[15][m] = tempkey[m]; } break;
  }
 }
 return keys;
}

2、des.html文件

<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <script type="text/javascript" src="des.js"></script>
  <script>
    function getresult(){
      //待加密字符串
      var str = document.getelementbyid("str").innerhtml;
      //第一个参数必须;第二个、第三个参数可选
      var key1 = document.getelementbyid("key1").innerhtml;
      var key2 = document.getelementbyid("key2").innerhtml;
      var key3 = document.getelementbyid("key3").innerhtml;
      //加密方法
      var enresult = strenc(str,key1,key2,key3);
      //解密方法
      var deresult = strdec(enresult,key1,key2,key3);
      //展示结果
      document.getelementbyid("enstr").innerhtml = enresult;
      document.getelementbyid("dnstr").innerhtml = deresult;
    }
  </script>
</head>
<body>
<input type="button" value="获取加密结果与解密结果" onclick="getresult()" />
  <table>
    <tr>
     <td align="left">字符串:</td>
     <td><span id="str">jb51.net</span></td>
    </tr>
    <tr>
     <td>加密key:</td>
     <td>key1=<span id="key1">1</span>;key2=<span id="key2">2</span>;key3=<span id="key3">3</span></td>
    </tr>
    <tr>
     <td align="left">加密结果:</td>
     <td align="left"><label id = "enstr"></label></td>
    </tr>
    <tr>
     <td align="left">解密结果: </td>
     <td align="left"><label id = "dnstr"></label></td>
    </tr>
  <table>
</body>
</html>

运行结果:

Java实现与JS相同的Des加解密算法完整实例

ps:关于加密解密感兴趣的朋友还可以参考本站在线工具:

文字在线加密解密工具(包含aes、des、rc4等):

md5在线加密工具:
http://tools.jb51.net/password/createmd5password

在线散列/哈希算法加密工具:

在线md5/hash/sha-1/sha-2/sha-256/sha-512/sha-3/ripemd-160加密工具:

在线sha1/sha224/sha256/sha384/sha512加密工具:

更多关于java相关内容感兴趣的读者可查看本站专题:《java数学运算技巧总结》、《java数据结构与算法教程》、《java字符与字符串操作技巧总结》、《java操作dom节点技巧总结》和《java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。

上一篇:

下一篇: