📄 sdes.java
字号:
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
public class SDes {
static int[][] S0={{1,0,3,2},{3,2,1,0},{0,2,1,3},{3,1,3,2}};
static int[][] S1={{0,1,2,3},{2,0,1,3},{3,0,1,0},{2,1,0,3}};
int[] K1=new int[8];
int[] K2=new int[8];
public static void main(String[] args){
//Integer i=new Integer(89);
//System.out.println((char)i.byteValue());
//System.out.println(Integer.toBinaryString(105));
SDes sDes=new SDes();
String encrypt=sDes.enCrypt("djiejifjeijlkjieji中 fefa ");
sDes.deCrypt(encrypt);
}
public SDes(){
DesKey key=new DesKey();
key.generateKey(null);
K1=key.getK1();
K2=key.getK2();
}
public SDes(String args){
DesKey key=new DesKey();
key.generateKey(args);
K1=key.getK1();
K2=key.getK2();
}
public String enCrypt(String toDes){
byte[] srcbytes=null;
try {
srcbytes = toDes.getBytes("iso-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//Byte b2enc=new Byte("85");
int[] enRet=new int[8];
//byte[] tagetbytes=new byte[];
ArrayList targetbytesList=new ArrayList();
for(int ipos=0;ipos<srcbytes.length;ipos++){
enRet=invIP(fK(SW(fK(IP(Byte2Array(srcbytes[ipos])),K1)),K2));
//debug
System.out.println("encrypted("+srcbytes[ipos]+")=");
debugOutput(enRet);
/*
int[] debugret=invIP(fK(SW(fK(IP(enRet),K2)),K1));
byte[] testout=new byte[1];
testout[0]=(byte)binaryArray2Int(debugret);
System.out.println("out="+new String(testout));
*/
//end debug
targetbytesList.add(new Byte((byte)binaryArray2Int(enRet)));
}
System.out.println("加密后");
Object[] resultBytes=targetbytesList.toArray();
byte[] resultbytes=new byte[resultBytes.length];
for(int i=0;i<resultBytes.length ;i++){
resultbytes[i]=((Byte)resultBytes[i]).byteValue();
}
String ResultStr=null;
try{
ResultStr=new String(resultbytes,"iso-8859-1");
}catch(Exception ex){}
System.out.println(ResultStr);
//debugOutput(enRet);
return ResultStr;
}
public String deCrypt(String toUnDes){
byte b2enc=0;
ArrayList targetbytesList=new ArrayList();
int[] enRet=new int[8];
byte[] bytes2UnDes=null;
try {
bytes2UnDes = toUnDes.getBytes("iso-8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int ipos=0;ipos<bytes2UnDes.length;ipos++){
b2enc=bytes2UnDes[ipos];
//debug
System.out.println("to decrypted("+bytes2UnDes[ipos]+")");
//enddebug
enRet=invIP(fK(SW(fK(IP(Byte2Array(b2enc)),K2)),K1));
//binaryArray2Int(b2enc);
targetbytesList.add(new Byte((byte)binaryArray2Int(enRet)));
//debug
debugOutput(enRet);
}
Object[] resultBytes=targetbytesList.toArray();
byte[] resultbytes=new byte[resultBytes.length];
for(int i=0;i<resultBytes.length ;i++){
resultbytes[i]=((Byte)resultBytes[i]).byteValue();
}
String ResultStr=null;
try{
ResultStr=new String(resultbytes,"iso-8859-1");
System.out.println("解密后");
System.out.println(ResultStr);
}catch(Exception ex){}
debugOutput(enRet);
return ResultStr;
}
public int[] Byte2Array(byte bValue){
int[] result=new int[8];
String sBinary=Integer.toBinaryString((int)bValue);
sBinary="00000000"+sBinary;
sBinary=sBinary.substring(sBinary.length()-8,sBinary.length());
for(int iloop=0;iloop<8;iloop++)
result[iloop]=Integer.valueOf( sBinary.substring(iloop,iloop+1));
return result;
}
/**
* IP
* */
public int[] IP(int[] data){
int[] IPDefine={2,6,3,1,4,8,5,7};
return DesKey.reOrder(data, IPDefine);
}
/**
* 逆IP
* */
public int[] invIP(int[] data){
int[] invIPDefine={4,1,3,5,7,2,8,6};
return DesKey.reOrder(data,invIPDefine);
}
/**
* fK
* */
public int[] fK(int[] byte2Opr,int[] sk){
int[] leftSrc=new int[4];
int[] rightSrc=new int[4];
int[] result=new int[8];
System.arraycopy(byte2Opr, 0, leftSrc, 0, 4);
System.arraycopy(byte2Opr, 4, rightSrc, 0, 4);
System.arraycopy( Xor(leftSrc,F(rightSrc,sk)),0,result,0,4);
System.arraycopy(rightSrc,0,result,4,4);
return result;
/*double iret=0;
for(int iloop=0;iloop<8;iloop++){
iret+=result[iloop]*Math.pow(2,7-iloop);
}
return new Byte((new Integer((int)iret)).byteValue());
*/
//return new Byte((int)iret);
}
/**
* */
public int[] F(int[] data,int[] sk){
int[] EPDefine={4,1,2,3,2,3,4,1};
int[] AfterBoxDefine={2,4,3,1};
int[] dest=new int[8];
int[] result=new int[4];
int[] first4Bits=new int[4];
int[] last4Bits=new int[4];
DesKey.reOrderCopy(data, dest, EPDefine);
dest=Xor(dest,sk);
System.arraycopy(dest,0,first4Bits,0,4);
System.arraycopy(dest, 4, last4Bits, 0, 4);
System.arraycopy(sBox(first4Bits,S0), 0, result, 0, 2);
System.arraycopy(sBox(last4Bits,S1),0,result,2,2);
DesKey.reOrder(result, AfterBoxDefine);
//TODO
return result;
}
/**
* 异或操作
* */
private int[] Xor(int[] first,int[] second){
int[] result=new int[first.length];
for(int i=0;i<first.length ;i++){
if(first[i]==second[i])
result[i]=0;
else
result[i]=1;
}
return result;
}
public int[] sBox(int[] data,int[][] box){
int[] result=new int[2];
int iRow=data[0]*2+data[3];
int iCol=data[1]*2+data[2];
result[1]=box[iRow][iCol]/2;
result[0]=box[iRow][iCol]-result[1]*2;
return result;
}
public int[] SW(int[] data){
int[] result=new int[8];
System.arraycopy(data, 0, result, 4, 4);
System.arraycopy(data, 4, result, 0, 4);
return result;
}
public void debugOutput(int[] input){
for(int i=0;i<input.length ;i++){
System.out.print(input[i]);
}
System.out.println();
}
public int binaryArray2Int(int[] bins){
int iret=0;
for(int iloop=0;iloop<bins.length;iloop++){
iret+=bins[iloop]*Math.pow(2, bins.length-iloop-1);
}
if(iret>127)
iret=-1*(256-iret);
System.out.println(iret);
return iret;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -