一、DES加密算法的举例详解请点击我
二、以下为程序的整体框架结构如下
- 首先声明的是,输入的明文,最终会被转换为二进制形式再进行加密,具体转换函数,在代码中都有注释。当然也可以根据你自己想要的输入格式,但是最后必须转换为二进制形式参与运算。
- 程序中含有一个内部类,这个Key类用来产生16轮所需要的16个子密钥,具体产生密钥的详细过程,在详解中阐述的十分详细,在这里就不仔细说明。
- 调用主函数来加密或者解密,加密还是解密由函数中的参数check决定,check为1,加密;check为2,解密。
- 然后执行常规的加密流程,这些加密流程都是指定的规则,所以不用去刨根问底,我们做的是具体实现它。然后具体的加密流程细节在上面的详解中说的十分清楚,自行查阅。
- 加密和解密唯一不同的是:在进行加密时,异或的密钥是从第一轮开始的;进行解密时,异或的密钥是从最后一轮密钥开始的。也就是说,加密的时候,第一轮用的是第一轮的密钥........;解密时,第一轮用的是第16轮的密钥..........
三、具体实现代码
import java.util.Arrays;
import java.util.Scanner;
public class DES算法 {
static int []IP=
{58,50,42,34,26,18,10,2,
60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6,
64,56,48,40,32,24,16,8,
57,49,41,33,25,17,9,1,
59,51,43,35,27,19,11,3,
61,53,45,37,29,21,13,5,
63,55,47,39,31,23,15,7
};
static int []E = {
32, 1, 2, 3, 4, 5,
4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13,
12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21,
20, 21, 22, 23, 24, 25,
24, 25, 26, 27, 28, 29,
28,29,30,31,32,1
};
static int [][]S1= {
{14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7},
{0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8},
{4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0},
{15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13}
};
static int [][]S2= {
{15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10},
{3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5},
{0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15},
{13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9}
};
static int [][]S3= {
{10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8},
{13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1},
{13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7},
{1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12}
};
static int [][]S4= {
{7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15},
{13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9},
{10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4},
{3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14}
};
static int [][]S5= {
{2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9},
{14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6},
{4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14},
{11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3}
};
static int [][]S6= {
{12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11},
{10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8},
{9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6},
{4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13}
};
static int [][]S7= {
{4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1},
{13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6},
{1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2},
{6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12}
};
static int [][]S8= {
{13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7},
{1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2},
{7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8},
{2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11}
};
static int[]P= {
16, 7, 20, 21,
29, 12, 28, 17,
1, 15, 23, 26,
5, 18, 31, 10,
2, 8, 24, 14,
32, 27, 3, 9,
19, 13, 30, 6,
22, 11, 4, 25,
};
static int[] IP_1= {
40, 8, 48, 16, 56, 24, 64, 32,
39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30,
37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28,
35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25
};
static int [] mBinarytext = new int[64]; //存储明文的二进制形式
static int [] IPtext = new int[64]; //存储IP置换后的二进制
static int[] L = new int[32]; //存储明文左边部分
static int[] R = new int[32]; //存储明文右半部分
static int [] Rk = new int[48];//存储R从32扩展到48位后的值
static String XorResult; //存储异或结果
static int [] sboxResult = new int[32];//存储经过S-box后的结果
static int [] pboxResult = new int[32];//存储经过P-box置换后的结果
static int [] Ctext = new int[64]; //最终加密或者解密的二进制形式
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("**********************DES加密*******************************");
System.out.println("请选择加密还是解密: ");
System.out.println("1、加密");
System.out.println("2、解密");
while (in.hasNext()) {
int n = in.nextInt();
if (n==1) {
System.out.println("请输入8个字符表示的明文:");
String M = in.next();
/*
System.out.println("明文的16进制形式为:");
System.out.println(chToHex(M));*/
System.out.println("请输入8个字符密钥: ");
String ckey = in.next();
System.out.println("(十六进制)密文是:");
System.out.println();
getCiptext(StrToHex(M),StrToHex(ckey),1);
}else if (n==2) {
System.out.println("请输入你的十六进制表示的密文:");
String M = in.next();
System.out.println("请输入8个字符密钥: ");
String ckey = in.next();
System.out.println("明文是:");
System.out.println();
getCiptext(M,StrToHex(ckey),2);
}
System.out.println();
System.out.println("**********************DES加密*******************************");
System.out.println("请选择加密还是解密: ");
System.out.println("1、加密");
System.out.println("2、解密");
}
}
/**
* 获取密文或明文
* @param M
* @param ckey 密钥
* @param check 为1,加密。为2,解密
*/
private static void getCiptext(String M,String ckey,int check) {
//1、将16进制密钥变为二进制表示,并且存入mBinarytext数组中
StringBuilder sb = new StringBuilder();
for (int i = 0; i < M.length(); i++) {
int ten = Integer.parseInt(M.charAt(i)+"", 16);
String binary = Integer.toBinaryString(ten);
int len = binary.length();
if (len<4) {
for (int j = 0; j <4-len ; j++) {
sb.append(0);
}
}
sb.append(binary);
}
//System.out.println(sb.toString());
for (int i = 0; i < sb.length(); i++) {
mBinarytext[i] = Integer.parseInt(sb.toString().charAt(i)+"");
}
//2、通过IP表进行置换
for (int i = 0; i < IP.length; i++) {
IPtext[i] = mBinarytext[IP[i]-1];
}
//System.out.println(Arrays.toString(IPtext));
//3、分为L和R两部分
System.arraycopy(IPtext, 0, L, 0, L.length); //填充左半部分
System.arraycopy(IPtext, L.length, R, 0, R.length); //填充右半部分
//4、开始循环迭代,获取最终的L16R16
Key.getSubKey(ckey); //调用内部类,产生下面需要迭代的所有子密钥
System.out.println("DES加密算法过程如下: ");
System.out.println();
/*
* 1、打印初始的64位的二进制明文、64位二进制密钥,L0和R0
*/
System.out.println("64位明文:"+Arrays.toString(mBinarytext));
System.out.println("64位密钥: "+Arrays.toString(Key.cBinarytext));
System.out.println("IP置换后的明文: "+Arrays.toString(IPtext));
System.out.println("L0: "+Arrays.toString(L));
System.out.println("R0: "+Arrays.toString(R));
System.out.println();
for (int i = 0; i < 16; i++){
//用一个临时数组保存L,因为L后面计算会用到
int temp[] = new int[32];
System.arraycopy(L, 0, temp, 0, L.length);
//L1 = R0
System.arraycopy(R, 0, L, 0, R.length);
//E扩展,将R从32位扩展为48位
Eextend();
//System.out.println("RK: "+Arrays.toString(Rk));
//扩展后的结果与子密钥进行异或,check为1加密异或,check为2解密异或
if (check==1) {
RkXoKey(Key.key,i);
}else if (check==2) {
JRkXoKey(Key.key, i);
}
//System.out.println(XorResult);
//然后再将48位异或结果映射为32位
S_BOX();
//System.out.println(Arrays.toString(sboxResult));
//P-box置换
P_BOX();
//System.out.println(Arrays.toString(pboxResult));
//temp or pboxResult
LorpboxResult(temp);
//System.out.println(Arrays.toString(R));
System.out.println("===============第"+(i+1)+"轮"+"========================");
System.out.println();
System.out.println("E变换: "+Arrays.toString(Rk));
System.out.println("E变换后与本轮子密钥异或的结果: "+XorResult);
System.out.println("经过8个S盒后的结果: "+Arrays.toString(sboxResult));
System.out.println("P盒置换的结果: "+Arrays.toString(pboxResult));
System.out.println("L"+(i+1)+": "+Arrays.toString(L));
System.out.println("R"+(i+1)+": "+Arrays.toString(R));
System.out.println("K"+(i+1)+": "+Arrays.toString(check==1?Key.key[i]:Key.key[16-i-1]));
System.out.println();
}
RLandIP_1(); //IP逆置换
System.out.println("IP逆置换: "+Arrays.toString(Ctext));
System.out.println();
if (check==1) {
System.out.println("以下为最终的密文: ");
printHexString1(); //将密文以16进制打印出来
}else if (check==2) {
System.out.println("以下为最终的明文: ");
binaryToch(); //将二进制明文以字符形式打印出来
}
}
/**
* E扩展
*/
private static void Eextend() {
int temp[] = new int[48];
for (int i = 0; i < E.length; i++) {
temp[i] = R[E[i]-1];
}
System.arraycopy(temp, 0, Rk, 0, Rk.length);; //将扩展后的48位数组,赋值给Rk
}
/**
* 加密异或
* @param key
* @param 指定需要第几轮的密钥
*/
private static void RkXoKey(int[][] key, int x) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 48; i++) {
int res = Rk[i]^key[x][i];
sb.append(res);
}
//System.out.println(sb.toString());
XorResult = sb.toString();
//System.out.println("K1:"+Arrays.toString(key[x]));
//System.out.println("xorresult: "+XorResult);
}
/**
* 解密异或
* @param key
* @param x 指定需要第几轮的密钥
*/
private static void JRkXoKey(int[][] key, int x) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 48; i++) {
int res = Rk[i]^key[16-x-1][i];
sb.append(res);
}
//System.out.println(sb.toString());
XorResult = sb.toString();
//System.out.println("K1:"+Arrays.toString(key[x]));
//System.out.println("xorresult: "+XorResult);
}
/**
* 通过S-BOX48位压缩成32位
*/
private static void S_BOX() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i*6+6 <= XorResult.length(); i++) {
String str = XorResult.substring(i*6, i*6+6);
//System.out.println("str:"+str);
if (i==0) {
int row = Integer.parseInt(str.charAt(0)+""+str.charAt(5),2);
//System.out.println("行:"+row);
int col = Integer.parseInt(str.substring(1, 5), 2);
//System.out.println("列:"+col);
String res = Integer.toString(S1[row][col],2);
int len = res.length();
if (len<4) {
for (int j = 0; j < 4-len; j++) {
sb.append(0);
}
}
sb.append(res);
}else if (i==1) {
int row = Integer.parseInt(str.charAt(0)+""+str.charAt(5),2);
//System.out.println("行:"+row);
int col = Integer.parseInt(str.substring(1, 5), 2);
//System.out.println("列:"+col);
String res = Integer.toString(S2[row][col],2);
int len = res.length();
if (len<4) {
for (int j = 0; j < 4-len; j++) {
sb.append(0);
}
}
sb.append(res);
}else if (i==2) {
int row = Integer.parseInt(str.charAt(0)+""+str.charAt(5),2);
//System.out.println("行:"+row);
int col = Integer.parseInt(str.substring(1, 5), 2);
//System.out.println("列:"+col);
String res = Integer.toString(S3[row][col],2);
int len = res.length();
if (len<4) {
for (int j = 0; j < 4-len; j++) {
sb.append(0);
}
}
sb.append(res);
}else if (i==3) {
int row = Integer.parseInt(str.charAt(0)+""+str.charAt(5),2);
//System.out.println("行:"+row);
int col = Integer.parseInt(str.substring(1, 5), 2);
//System.out.println("列:"+col);
String res = Integer.toString(S4[row][col],2);
int len = res.length();
if (len<4) {
for (int j = 0; j < 4-len; j++) {
sb.append(0);
}
}
sb.append(res);
}else if (i==4) {
int row = Integer.parseInt(str.charAt(0)+""+str.charAt(5),2);
//System.out.println("行:"+row);
int col = Integer.parseInt(str.substring(1, 5), 2);
//System.out.println("列:"+col);
String res = Integer.toString(S5[row][col],2);
int len = res.length();
if (len<4) {
for (int j = 0; j < 4-len; j++) {
sb.append(0);
}
}
sb.append(res);
}else if (i==5) {
int row = Integer.parseInt(str.charAt(0)+""+str.charAt(5),2);
//System.out.println("行:"+row);
int col = Integer.parseInt(str.substring(1, 5), 2);
//System.out.println("列:"+col);
String res = Integer.toString(S6[row][col],2);
int len = res.length();
if (len<4) {
for (int j = 0; j < 4-len; j++) {
sb.append(0);
}
}
sb.append(res);
}else if (i==6) {
int row = Integer.parseInt(str.charAt(0)+""+str.charAt(5),2);
//System.out.println("行:"+row);
int col = Integer.parseInt(str.substring(1, 5), 2);
//System.out.println("列:"+col);
String res = Integer.toString(S7[row][col],2);
int len = res.length();
if (len<4) {
for (int j = 0; j < 4-len; j++) {
sb.append(0);
}
}
sb.append(res);
}else if (i==7) {
int row = Integer.parseInt(str.charAt(0)+""+str.charAt(5),2);
//System.out.println("行:"+row);
int col = Integer.parseInt(str.substring(1, 5), 2);
//System.out.println("列:"+col);
String res = Integer.toString(S8[row][col],2);
int len = res.length();
if (len<4) {
for (int j = 0; j < 4-len; j++) {
sb.append(0);
}
}
sb.append(res);
}
}
String res = sb.toString();
for (int i = 0; i < res.length(); i++) {
sboxResult[i] = Integer.parseInt(res.charAt(i)+"");
}
}
/**
* 通过P_Box进行置换
*/
private static void P_BOX() {
int temp[] = new int[32];
for (int i = 0; i < temp.length; i++) {
temp[i] = sboxResult[P[i]-1];
}
System.arraycopy(temp, 0, pboxResult, 0, temp.length);
}
/**
* 新的右半部分等于旧的左半部分异或上经过P-box的结果
* @param temp
*/
private static void LorpboxResult(int[] temp) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < temp.length; i++) {
int res =temp[i]^pboxResult[i];
sb.append(res);
}
String s = sb.toString();
for (int i = 0; i < R.length; i++) {
R[i] = Integer.parseInt(s.charAt(i)+"");
}
}
/**
* 左右两部逆转,并且进行逆IP^-1
*/
private static void RLandIP_1() {
System.arraycopy(R, 0, Ctext, 0, R.length);
System.arraycopy(L, 0, Ctext, R.length, L.length);
//System.out.println(Arrays.toString(Ctext));
//进行逆IP
int temp[] = new int[64];
for (int i = 0; i < temp.length; i++) {
temp[i] = Ctext[IP_1[i]-1];
}
System.arraycopy(temp, 0, Ctext, 0, temp.length);
//System.out.println(Arrays.toString(Ctext));
}
/**
* 将二进制密文以十六进制打印出来
*/
private static void printHexString1() {
StringBuilder sb = new StringBuilder();
String c = "";
for (int i = 0; i < Ctext.length; i++) {
c = c + Ctext[i];
}
for (int i = 0; i*8+8 <= c.length(); i++) {
String str = c.substring(i*8, i*8+8);
int ten = Integer.parseInt(str,2);
String sixteen = Integer.toHexString(ten);
if (sixteen.length()==1) {
sb.append(0);
}
sb.append(sixteen);
}
System.out.println(sb.toString());
}
/**
* 将得到的二进制明文以字符串形式打印
*/
private static void binaryToch() {
StringBuilder sb = new StringBuilder();
String c = "";
for (int i = 0; i < Ctext.length; i++) {
c = c + Ctext[i];
}
for (int i = 0; i*8+8 <= c.length(); i++) {
String str = c.substring(i*8, i*8+8);
int ten = Integer.parseInt(str,2);
char ch = (char)ten;
sb.append(ch);
}
System.out.println(sb.toString());
}
/**
* 字符串转换成16进制表现形式
* @param M
* @return
*/
public static String StrToHex(String M) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < M.length(); i++) {
int ten = (int)M.charAt(i);
String sixteen = Integer.toHexString(ten);
if (sixteen.length()==1) {
sb.append(0);
}
sb.append(sixteen);
}
return sb.toString();
}
//内部类,用来产生所有的子密钥
public static class Key {
static int[] PC_1= {
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
};
static int[] PC_2= {
14, 17, 11, 24, 1, 5,
3, 28, 15, 6, 21, 10,
23, 19, 12, 4, 26, 8,
16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55,
30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53,
46, 42, 50, 36, 29, 32
};
static int key[][] = new int[16][48]; //存储16轮的子密钥
static int cBinarytext[] = new int[64]; //存储密钥的二进制文本
static int [] newcBinarytext = new int[56]; //存储经过pc-1变为56位的密钥
static int [] C = new int[28];
static int [] D = new int[28];
static int []CD = new int[56]; //暂时存放C和D整合的密钥
/*
//133457799BBCDFF1 输入的64位密钥
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String c = in.next(); //获取密钥
getSubKey(c);
}*/
/**
* 获得16轮所需要的16个子密钥
* @param c
*/
private static void getSubKey(String c) {
//1、将16进制密钥变为二进制表示,并且存入cBinarytext数组中
StringBuilder sb = new StringBuilder();
for (int i = 0; i < c.length(); i++) {
int ten = Integer.parseInt(c.charAt(i)+"", 16);
String binary = Integer.toBinaryString(ten);
int len = binary.length();
if (len<4) {
for (int j = 0; j <4-len ; j++) {
sb.append(0);
}
}
sb.append(binary);
}
//System.out.println(sb.toString());
for (int i = 0; i < sb.length(); i++) {
cBinarytext[i] = Integer.parseInt(sb.toString().charAt(i)+"");
}
//System.out.println(Arrays.toString(cBinarytext));
//2、通过PC-1表,将64位密钥压缩为56位
for (int i = 0; i < PC_1.length; i++) {
newcBinarytext[i] = cBinarytext[PC_1[i]-1];
}
//System.out.println(Arrays.toString(newcBinarytext));
//3、将56位密钥分为两部分C0和D0,一边28位
System.arraycopy(newcBinarytext, 0, C, 0, C.length);
System.arraycopy(newcBinarytext, C.length, D, 0, D.length);
//System.out.println(Arrays.toString(C));
//System.out.println(Arrays.toString(D));
//4、通过16轮循环,获得每一轮的密钥
for (int i = 1; i < 17; i++) {
//左右两部分进行移位,获得第一轮需要的密钥
if (i==1||i==2||i==9||i==16) {//左右两部分分别移动1位
int x = C[0];
int y = D[0];
for (int j = 0; j<27; j++) {
C[j] = C[j+1];
D[j] = D[j+1];
}
C[C.length-1] = x;
D[D.length-1] = y;
}else { //左右两部分分别移位2位
int x = C[0];
int x1 = C[1];
int y = D[0];
int y1 = D[1];
for (int j = 0; j<26; j++) {
C[j] = C[j+2];
D[j] = D[j+2];
}
C[C.length-2] = x;
C[C.length-1] = x1;
D[D.length-2] = y;
D[D.length-1] = y1;
}
//System.out.println("C"+i+" = "+Arrays.toString(C));
//System.out.println("D"+i+" = "+Arrays.toString(D));
//System.out.println();
//整个C和D
System.arraycopy(C, 0, CD, 0, C.length);
System.arraycopy(D, 0, CD, C.length, D.length);
//将C和D拼凑起来,再经过PC-2表得到真正的每轮的48位子密钥
int temp[] = new int[48];
for (int j = 0; j < temp.length; j++) {
temp[j] = CD[PC_2[j]-1];
}
for (int j = 0; j < temp.length; j++) {
key[i-1][j] = temp[j];
}
}
/*
//打印16轮的子密钥
for (int i = 0; i < key.length; i++) {
System.out.println("K"+(i+1)+":"+Arrays.toString(key[i]));
}*/
}
}
}
四、运行结果
1、加密
2、解密