《Des加密(js+java结果一致)精讲》由会员分享,可在线阅读,更多相关《Des加密(js+java结果一致)精讲(28页珍藏版)》请在金锄头文库上搜索。
1、Des加密(js+java结果一致)正文:1.des资源文件下载(des.js+des.html+des.java) function getResult() /待加密字符串 var str = document.getElementById(str).innerText; /第一个参数必须;第二个、第三个参数可选 var key1 = document.getElementById(key1).innerText; var key2 = document.getElementById(key2).innerText; var key3 = document.getElementById(ke
2、y3).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; 字符串: admin 加密key: key1=1;key2=2;key3=3 加密结果: 解密结果: 4.java文件(des.java)p
3、ackage 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.printl
4、n(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
5、 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(); i
6、f (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 !=
7、 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 th
8、irdLength; 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;