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;xtempBt=enc(tempBt,(int[])firstKeyBt.get(x));
}
for(y=0;ytempBt=enc(tempBt,(int[])secondKeyBt.get(y));
}
for(z=0;ztempBt=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;xtempBt=enc(tempBt,(int[])firstKeyBt.get(x));
}
for(y=0;ytempBt=enc(tempBt,(int[])secondKeyBt.get(y));
}
encByte=tempBt;
}else{
if(firstKey!
=null&&firstKey!
=""){
int[]tempBt;
intx=0;
tempBt=bt;
for(x=0;xtempBt=enc(tempBt,(int[])firstKeyBt.get(x));
}
encByte=tempBt;
}
}
}
encData=bt64ToHex(encByte);
}else{
intiterator=(leng/4);
intremainder=leng%4;
inti=0;
for(i=0;iStringtempData=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;xtempBt=enc(tempBt,(int[])firstKeyBt.get(x));
}
for(y=0;ytempBt=enc(tempBt,(int[])secondKeyBt.get(y));
}
for(z=0;ztempBt=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;xtempBt=enc(tempBt,(int[])firstKeyBt.get(x));
}
for(y=0;ytempBt=enc(tempBt,(int[])secondKeyBt.get(y));
}
encByte=tempBt;
}else{
if(firstKey!
=null&&firstKey!
=""){
int[]tempBt;
intx;
tempBt=tempByte;
for(x=0;xtempBt=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;xtempBt=enc(tempBt,(int[])firstKeyBt.get(x));
}
for(y=0;ytempBt=enc(tempBt,(int[])secondKeyBt.get(y));
}
for(z=0;ztempBt=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;xtempBt=enc(tempBt,(int[])firstKeyBt.get(x));
}
for(y=0;ytempBt=enc(tempBt,(int[])secondKeyBt.get(y));
}
encByte=tempBt;
}else{
if(firstKey!
=null&&firstKey!
=""){
int[]tempBt;
intx;
tempBt=tempByte;
for(x=0;xtempBt=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;iStringtempData=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;ikeyBytes.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;iintk=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++)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
-
Des
加密
js
java
结果
一致
![提示](https://static.bdocx.com/images/bang_tan.gif)
冰豆网所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。