明文:0123456789abcdef
秘钥:0123456789abcdef
密文:56CC09E7CFDC5CAF
import java.util.Scanner;
public class Des
{
public static char[] c0=new char[28], d0=new char[28];
static char [][]KorderBinary=new char[16][48];//16个子秘钥的二进制空间
static String []KorderHex=new String [16];//16个子秘钥的十六进制空间
static char[]l0=new char[32],r0=new char[32];//LR 32位空间
static char[]cryptograph=new char[64];
public static void main(String args[])
{
// System.out.print("请输入16位16进制秘钥:");
key64to56cd();
for(int q=0;q<16;q++)//计算16个子秘钥
{
KorderBinary[q]=getKey(q);
outKHex48(q);
KorderHex[q]=KorderHex[q].replace("null","").toUpperCase();
System.out.println("K"+(q+1)+" "+KorderHex[q]);
System.out.println("K"+(q+1)+" "+String.valueOf(KorderBinary[q]));
}
// System.out.print("请输入16位16进制明文:");
char [] MBinary=inHexOutBinary();
ipDivide(MBinary);
for(int i=0;i<16;i++)
{
getLR(pBox(sBox(xor48(Expansion(r0),KorderBinary[i]))),i);
}
char[]RL=new char[64];
for(int i=0;i<32;i++)
RL[i]=r0[i];
for(int i=32;i<64;i++)
RL[i]=l0[i-32];
cryptograph=ipreverse(RL);
// System.out.println(String.valueOf(cryptograph));
System.out.print("密文为:"+outHex64());
System.out.println("解密后:");
MBinary=cryptograph;
ipDivide(MBinary);
for(int i=0;i<16;i++)
{
getLR(pBox(sBox(xor48(Expansion(r0),KorderBinary[15-i]))),i);
}
RL=new char[64];
for(int i=0;i<32;i++)
RL[i]=r0[i];
for(int i=32;i<64;i++)
RL[i]=l0[i-32];
cryptograph=ipreverse(RL);
// System.out.println(String.valueOf(cryptograph));
System.out.print(outHex64());
}
public static String singleHexToBinary(String h)//一位十六进制字符串转四位二进制字符串
{
int d=Integer.parseInt(h,16);
String b=Integer.toBinaryString(d);
if (b.length()<2)
b="000"+b;
else if(b.length()<3)
b="00"+b;
else if(b.length()<4)
b="0"+b;
// System.out.println(b);
return b;
}
public static String BinaryToHex(String b)//二进制字符串转换十六进制字符串
{
int d=Integer.parseInt(b,2);
String h=Integer.toHexString(d);
return h;
}
public static char[] inHexOutBinary() //输入16位16进制,输出64位二进制
{
// Scanner m_in=new Scanner(System.in);
// String message=m_in.next();
String message="0123456789abcdef";
char [] MCharHex=message.toCharArray();//16位分开的数组
String MStrHex[]=new String [16];//16个Char转String的数组
String Mbinary[]=new String [16];//16个二进制字符串的数组
char MbinarySplit[]=new char [64];//64个二进制字符的数组
String Mbinary64="";
for(int i=0;i<message.length();i++)
{
MStrHex[i]=String.valueOf( MCharHex[i]);
Mbinary[i]=singleHexToBinary(MStrHex[i]);
Mbinary64+=Mbinary[i];
}
// System.out.println();
MbinarySplit=Mbinary64.toCharArray();
/* for(int i=0;i<64;i++)
{
System.out.print(MbinarySplit[i]+" ");
if (i==15|i==31|i==47)
System.out.println();
}*/
return MbinarySplit;
}
public static char[] pc1(char[] k)//pc1转置 56位
{
char [] pc1dia=new char[]
{
57,49,41,33,25,17,9,1,58,50,42,34,26,18,10,2,
59,51,43,35,27,19,11,3,60,52,44,36,63,55,47,39,
31,23,15,7,62,54,46,38,30,22,14,6,61,53,45,37,
29,21,13,5,28,20,12,4};
char[]k56=new char[56]