Des加密js+java结果一致精讲Word格式文档下载.docx
《Des加密js+java结果一致精讲Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《Des加密js+java结果一致精讲Word格式文档下载.docx(35页珍藏版)》请在冰豆网上搜索。
varstr=document.getElementById("
str"
).innerText;
//第一个参数必须;
第二个、第三个参数可选
varkey1=document.getElementById("
key1"
varkey2=document.getElementById("
key2"
varkey3=document.getElementById("
key3"
//加密方法
varenResult=strEnc(str,key1,key2,key3);
//解密方法
vardeResult=strDec(enResult,key1,key2,key3);
//展示结果
document.getElementById("
enStr"
).innerText=enResult;
dnStr"
).innerText=deResult;
}
/head>
body>
inputtype="
button"
value="
获取加密结果与解密结果"
onclick="
getResult()"
table>
tr>
tdalign="
left"
字符串:
/td>
td>
spanid="
admin<
/span>
/tr>
加密key:
key1=<
1<
;
key2=<
2<
key3=<
3<
加密结果:
labelid="
/label>
解密结果:
/body>
/html>
4.java文件(des.java)
packagecom.zz.test;
importjava.util.ArrayList;
importjava.util.List;
publicclassDes{
publicDes(){
publicstaticvoidmain(String[]args){
DesdesObj=newDes();
Stringkey1="
1"
Stringkey2="
2"
Stringkey3="
3"
Stringdata="
admin"
Stringstr=desObj.strEnc(data,key1,key2,key3);
System.out.println(str);
Stringdec=desObj.strDec(str,key1,key2,key3);
System.out.println(dec);
/**
*DES加密/解密
*
*@CopyrightCopyright(c)2006
*@authorGuapo
*@seeDESCore
*/
/*
*encryptthestringtostringmadeupofhexreturntheencryptedstring
publicStringstrEnc(Stringdata,StringfirstKey,StringsecondKey,
StringthirdKey){
intleng=data.length();
StringencData="
"
ListfirstKeyBt=null,secondKeyBt=null,thirdKeyBt=null;
intfirstLength=0,secondLength=0,thirdLength=0;
if(firstKey!
=null&
&
firstKey!
="
){
firstKeyBt=getKeyBytes(firstKey);
firstLength=firstKeyBt.size();
if(secondKey!
secondKey!
secondKeyBt=getKeyBytes(secondKey);
secondLength=secondKeyBt.size();
if(thirdKey!
thirdKey!
thirdKeyBt=getKeyBytes(thirdKey);
thirdLength=thirdKeyBt.size();
if(leng>
0){
if(leng<
4){
int[]bt=strToBt(data);
int[]encByte=null;
&
=null
int[]tempBt;
intx,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{
intx,y;
intx=0;
encData=bt64ToHex(encByte);
intiterator=(leng/4);
intremainder=leng%4;
inti=0;
for(i=0;
i<
iterator;
i++){
StringtempData=data.substring(i*4+0,i*4+4);
int[]tempByte=strToBt(tempData);
tempBt=tempByte;
intx;
tempBt=enc(tempBt,(int[])firstKeyBt
.get(x));
encData+=bt64ToHex(encByte);
if(remainder>
StringremainderData=data.substring(iterator*4+0,
leng);
int[]tempByte=strToBt(remainderData);
returnencData;
*decrypttheencryptedstringtotheoriginalstring
*returntheoriginalstring
publicStringstrDec(Stringdata,StringfirstKey,StringsecondKey,
StringdecStr="
intiterator=leng/16;
StringtempData=data.substring(i*16+0,i*16+16);
StringstrByte=hexToBt64(tempData);
int[]intByte=newint[64];
intj=0;
for(j=0;
j<
64;
j++){
intByte[j]=Integer.parseInt(strByte.substring(j,j+1));
int[]decByte=null;
tempBt=intByte;
for(x=thirdLength-1;
x>
=0;
x--){
tempBt=dec(tempBt,(int[])thirdKeyBt.get(x));
for(y=secondLength-1;
y>
y--){
tempBt=dec(tempBt,(int[])secondKeyBt.get(y));
for(z=firstLength-1;
z>
z--){
tempBt=dec(tempBt,(int[])firstKeyBt.get(z));
decByte=tempBt;
for(x=secondLength-1;
tempBt=dec(tempBt,(int[])secondKeyBt.get(x));
for(y=firstLength-1;
tempBt=dec(tempBt,(int[])firstKeyBt.get(y));
for(x=firstLength-1;
tempBt=dec(tempBt,(int[])firstKeyBt.get(x));
decStr+=byteToString(decByte);
returndecStr;
*changthestringintothebitarray
*returnbitarray(it'
slength%64=0)
publicListgetKeyBytes(Stringkey){
ListkeyBytes=newArrayList();
intleng=key.length();
keyBytes.add(i,strToBt(key.substring(i*4+0,i*4+4)));
//keyBytes[i]=strToBt(key.substring(i*4+0,leng));
keyBytes.add(i,strToBt(key.substring(i*4+0,leng)));
returnkeyBytes;
*changthestring(it'
slength<
=4)intothebitarray
slength=64)
publicint[]strToBt(Stringstr){
intleng=str.length();
int[]bt=newint[64];
inti=0,j=0,p=0,q=0;
leng;
intk=str.charAt(i);
16;
intpow=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++){
intk=0;
for(q=0;
q<
q++){
q;
//bt[16*p+q]=parseInt(k/pow)%2;
//bt.add(16*p+q,"
+((k/pow)%2));
bt[16*p+q]=(k/pow)%2;
for(inti=0;
for(intj=0;