Des加密(js+java结果一致)精讲
Des加密(js+java结果一致)正文:1.des资源文件下载(des.js+des.html+des.java)<!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").innerText; /第一个参数必须;第二个、第三个参数可选 var key1 = document.getElementById("key1").innerText; var key2 = document.getElementById("key2").innerText; var key3 = document.getElementById("key3").innerText; /加密方法 var enResult = strEnc(str,key1,key2,key3); /解密方法 var deResult = strDec(enResult,key1,key2,key3); /展示结果 document.getElementById("enStr").innerText = enResult; document.getElementById("dnStr").innerText = deResult; </script></head><body><input type="button" value="获取加密结果与解密结果" onclick="getResult()" /> <table> <tr> <td align="left">字符串:</td> <td><span id="str">admin</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>4.java文件(des.java)package com.zz.test;import java.util.ArrayList;import java.util.List;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 = "admin" String str = desObj.strEnc(data, key1, key2, key3); System.out.println(str); String dec = desObj.strDec(str, key1, key2, key3); System.out.println(dec); /* * DES加密/解密 * * Copyright Copyright (c) 2006 * author Guapo * 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;