Des加密js+java结果一致精讲.docx

上传人:b****5 文档编号:8353474 上传时间:2023-01-30 格式:DOCX 页数:35 大小:21.43KB
下载 相关 举报
Des加密js+java结果一致精讲.docx_第1页
第1页 / 共35页
Des加密js+java结果一致精讲.docx_第2页
第2页 / 共35页
Des加密js+java结果一致精讲.docx_第3页
第3页 / 共35页
Des加密js+java结果一致精讲.docx_第4页
第4页 / 共35页
Des加密js+java结果一致精讲.docx_第5页
第5页 / 共35页
点击查看更多>>
下载资源
资源描述

Des加密js+java结果一致精讲.docx

《Des加密js+java结果一致精讲.docx》由会员分享,可在线阅读,更多相关《Des加密js+java结果一致精讲.docx(35页珍藏版)》请在冰豆网上搜索。

Des加密js+java结果一致精讲.docx

Des加密js+java结果一致精讲

Des加密(js+java结果一致)

正文:

1.des资源文件下载(des.js+des.html+des.java)

DOCTYPEHTML>

字符串:

加密结果:

解密结果:

admin
加密key:

key1=1;key2=2;key3=3

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!

=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;

intx,y,z;

tempBt=bt;

for(x=0;x

tempBt=enc(tempBt,(int[])firstKeyBt.get(x));

}

for(y=0;y

tempBt=enc(tempBt,(int[])secondKeyBt.get(y));

}

for(z=0;z

tempBt=enc(tempBt,(int[])thirdKeyBt.get(z));

}

encByte=tempBt;

}else{

if(firstKey!

=null&&firstKey!

=""&&secondKey!

=null

&&secondKey!

=""){

int[]tempBt;

intx,y;

tempBt=bt;

for(x=0;x

tempBt=enc(tempBt,(int[])firstKeyBt.get(x));

}

for(y=0;y

tempBt=enc(tempBt,(int[])secondKeyBt.get(y));

}

encByte=tempBt;

}else{

if(firstKey!

=null&&firstKey!

=""){

int[]tempBt;

intx=0;

tempBt=bt;

for(x=0;x

tempBt=enc(tempBt,(int[])firstKeyBt.get(x));

}

encByte=tempBt;

}

}

}

encData=bt64ToHex(encByte);

}else{

intiterator=(leng/4);

intremainder=leng%4;

inti=0;

for(i=0;i

StringtempData=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;

intx,y,z;

tempBt=tempByte;

for(x=0;x

tempBt=enc(tempBt,(int[])firstKeyBt.get(x));

}

for(y=0;y

tempBt=enc(tempBt,(int[])secondKeyBt.get(y));

}

for(z=0;z

tempBt=enc(tempBt,(int[])thirdKeyBt.get(z));

}

encByte=tempBt;

}else{

if(firstKey!

=null&&firstKey!

=""

&&secondKey!

=null&&secondKey!

=""){

int[]tempBt;

intx,y;

tempBt=tempByte;

for(x=0;x

tempBt=enc(tempBt,(int[])firstKeyBt.get(x));

}

for(y=0;y

tempBt=enc(tempBt,(int[])secondKeyBt.get(y));

}

encByte=tempBt;

}else{

if(firstKey!

=null&&firstKey!

=""){

int[]tempBt;

intx;

tempBt=tempByte;

for(x=0;x

tempBt=enc(tempBt,(int[])firstKeyBt

.get(x));

}

encByte=tempBt;

}

}

}

encData+=bt64ToHex(encByte);

}

if(remainder>0){

StringremainderData=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;

intx,y,z;

tempBt=tempByte;

for(x=0;x

tempBt=enc(tempBt,(int[])firstKeyBt.get(x));

}

for(y=0;y

tempBt=enc(tempBt,(int[])secondKeyBt.get(y));

}

for(z=0;z

tempBt=enc(tempBt,(int[])thirdKeyBt.get(z));

}

encByte=tempBt;

}else{

if(firstKey!

=null&&firstKey!

=""

&&secondKey!

=null&&secondKey!

=""){

int[]tempBt;

intx,y;

tempBt=tempByte;

for(x=0;x

tempBt=enc(tempBt,(int[])firstKeyBt.get(x));

}

for(y=0;y

tempBt=enc(tempBt,(int[])secondKeyBt.get(y));

}

encByte=tempBt;

}else{

if(firstKey!

=null&&firstKey!

=""){

int[]tempBt;

intx;

tempBt=tempByte;

for(x=0;x

tempBt=enc(tempBt,(int[])firstKeyBt

.get(x));

}

encByte=tempBt;

}

}

}

encData+=bt64ToHex(encByte);

}

}

}

returnencData;

}

/*

*decrypttheencryptedstringtotheoriginalstring

*

*returntheoriginalstring

*/

publicStringstrDec(Stringdata,StringfirstKey,StringsecondKey,

StringthirdKey){

intleng=data.length();

StringdecStr="";

ListfirstKeyBt=null,secondKeyBt=null,thirdKeyBt=null;

intfirstLength=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();

}

intiterator=leng/16;

inti=0;

for(i=0;i

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;

if(firstKey!

=null&&firstKey!

=""&&secondKey!

=null

&&secondKey!

=""&&thirdKey!

=null&&thirdKey!

=""){

int[]tempBt;

intx,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;

intx,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;

intx,y,z;

tempBt=intByte;

for(x=firstLength-1;x>=0;x--){

tempBt=dec(tempBt,(int[])firstKeyBt.get(x));

}

decByte=tempBt;

}

}

}

decStr+=byteToString(decByte);

}

returndecStr;

}

/*

*changthestringintothebitarray

*

*returnbitarray(it'slength%64=0)

*/

publicListgetKeyBytes(Stringkey){

ListkeyBytes=newArrayList();

intleng=key.length();

intiterator=(leng/4);

intremainder=leng%4;

inti=0;

for(i=0;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)));

}

returnkeyBytes;

}

/*

*changthestring(it'slength<=4)intothebitarray

*

*returnbitarray(it'slength=64)

*/

publicint[]strToBt(Stringstr){

intleng=str.length();

int[]bt=newint[64];

if(leng<4){

inti=0,j=0,p=0,q=0;

for(i=0;i

intk=str.charAt(i);

for(j=0;j<16;j++){

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<16;q++){

intpow=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(inti=0;i<4;i++){

intk=str.charAt(i);

for(intj=0;j<16;j++){

展开阅读全文
相关搜索

当前位置:首页 > 初中教育

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1