一聚教程网:一个值得你收藏的教程网站

热门教程

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

时间:2022-06-29 01:06:03 编辑:袖梨 来源:一聚教程网

本文实例讲述了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  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 = 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  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  j; m--) {
            pow *= 2;
          }
          // bt.set(16*i+j,""+(k/pow)%2));
          bt[16 * i + j] = (k / pow) % 2;
        }
      }
      for (p = leng; p  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  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  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 = 0; i--) {
      for (j = 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 

运行结果:

二、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  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 = 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  0){
  keyBytes[i] = strToBt(key.substring(i*4+0,leng));
 }
 return keyBytes;
}
/*
* chang the string(it's length j;m--){
     pow *= 2;
    }
    bt[16*i+j]=parseInt(k/pow)%2;
   }
  }
  for(p = leng;pq;m--){
     pow *= 2;
    }
    bt[16*p+q]=parseInt(k/pow)%2;
   }
  }
 }else{
  for(i = 0;ij;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;ij;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= 0;i --){
  for(j = 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 

2、des.html文件




  
  



  
字符串: jb51.net
加密key: key1=1;key2=2;key3=3
加密结果:
解密结果:

运行结果:

热门栏目