🍬 博主介绍
👨🎓 博主介绍:大家好,我是 JTZ_ ,很高兴认识大家~
✨主攻领域:【WEB 安全】
✨兼攻领域:【逆向分析】【SSM 网页制作】【应用程序编写】
🎉点赞➕评论➕收藏 == 养成习惯(一键三连)😋
🎉欢迎关注💗一起学习👍一起讨论⭐️一起进步📝文末有彩蛋
🙏作者水平有限,欢迎各位大佬指点,相互学习进步!
混乱的目的是: 隐藏任何明文同密文、或者密钥之间的关系
扩散的目的: 使明文的有效位和密钥一起组成尽可能多的密文。两者结合在一起就使得安全性变的相对较高
DES 子密钥生成

加密和解密数据块

目的: 将输入的 64 位数据块按照指定的规则重新组合
置换规则表如下:
| 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 |
表达的意思: 将原来数据中的数据按照此表的规则进行重新排列, eg : 58 表明将原始字符串的第 58 位,放到新字符串的第一位
不考虑每个字节的第8位,DES的密钥由64位减至56位,每个字节的第8位作为奇偶校验位。产生的56位密钥由下表生成(注意表中没有8,16,24,32,40,48,56和64这8位):
| 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 |
在 DES 的每一轮中,是从 56 位密钥中产生出不同的 48 位子密钥, 确定子密钥的方式为:
| 轮数 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 位数 | 1 | 1 | 2 | 2 | 2 | 2 | 2 | 2 | 1 | 2 | 2 | 2 | 2 | 2 | 2 | 1 |
| 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 |
| 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 |
原理就是将原先的 32 位数据分为 8组 4位的组合,在每一组的第一位插入上一组的末尾数据,最后一位插入下一组的第一位数据
压缩后的密钥和扩展后的分组异或后得到 48 位数据,将数据送入 S 盒,进行替换运算,替换运算是由 8 个不同的 S 盒完成的,每个 S 盒有 六位输入 四位输出,经过 S 盒替换后,就可以将 48 位数据替换为 32 位数据。
如何进行运算: 输入的六位数据的第一位和最后一位作为行数 H ,其余作为列数 L,在 S 盒中查找到 第 H 行 L 列对应的数据 Q, 就是输出的数据
S盒代替运算的32位输出按照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 |
然后 P 盒替换的结果和最初的 64 位分组的左半部分异或,然后 左、右部分交换,然后开始下一轮
末置换是初始置换的逆过程,DES最后一轮后,左、右两半部分并未进行交换,而是两部分合并形成一个分组做为末置换的输入。末置换规则如下表:
| 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 |

package com.example.frame_first;
import static java.lang.System.exit;
public class DES {
// IP 置换表
final static int[] IP_Substitution = {
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, 56, 48, 40, 32,
24, 16, 8, 0, 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 位秘钥
final static int[] Password_Substitution = {
56, 48, 40, 32, 24, 16, 8, 0, 57, 49,
41, 33, 25, 17, 9, 1, 58, 50, 42, 34,
26, 18, 10, 2, 59, 51, 43, 35, 62, 54,
46, 38, 30, 22, 14, 6, 61, 53, 45, 37,
29, 21, 13, 5, 60, 52, 44, 36, 28, 20,
12, 4, 27, 19, 11, 3};
// 子密钥的每一轮的移动数
final static int[] Password_Move = {1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1};
// 密钥压缩表,将 56位秘钥压缩为 48 位秘钥
final static int[] Password_compress = {
13, 16, 10, 23, 0, 4,
2, 27, 14, 5, 20, 9,
22, 18, 11, 3, 25, 7,
15, 6, 26, 19, 12, 1,
40, 51, 30, 36, 46, 54,
29, 39, 50, 44, 32, 47,
43, 48, 38, 55, 33, 52,
45, 41, 49, 35, 28, 31};
// 扩展置换
final static int[] Permutation_Extended = {
31, 0, 1, 2, 3, 4,
3, 4, 5, 6, 7, 8,
7, 8, 9, 10, 11,
12, 11, 12, 13,
14, 15, 16, 15, 16,
17, 18, 19, 20, 19, 20,
21, 22, 23, 24, 23, 24, 25,
26, 27, 28, 27, 28, 29, 30, 31, 0};
// S 盒
final static int[][][] S_BOX ={
{
{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}
},
{
{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}
},
{
{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}
},
{
{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}
},
{
{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}
},
{
{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}
},
{
{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}
},
{
{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}
}
};
final static int[] RT_P = {
15, 6, 19, 20, 28, 11,
27, 16, 0, 14, 22, 25,
4, 17, 30, 9, 1, 7, 23,
13, 31, 26, 2, 8, 18, 12,
29, 5, 21, 10, 3, 24 };
// 逆IP置换表
final static int[] RT_InverseIP = {
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,
32, 0, 40, 8, 48, 16, 56, 24};
/**
* 第一步: 执行 IP 置换
* @param Plaintext
* @return
*/
public static char[] Substitution_IP(String Plaintext){
char[] temp = new char[64];
for (int i = 0; i < 64; i++) {
temp[i] = Plaintext.charAt(IP_Substitution[i]);
}
return temp;
}
/**
* 第二步: 执行 密钥置换
* @param Key
* @return
*/
public static StringBuffer[] Password_Key(String Key){
if(Key.length() != 64) {
System.out.println("你规定的秘钥不是 64 位");
exit(0);
}
// 进行 64 位密钥置换为 56 位秘钥
char[] temp = new char[56];
char[] key = Key.toCharArray();
for (int i = 0; i < 56; i++) {
temp[i] = key[Password_Substitution[i]];
}
Key = String.valueOf(temp);
// 分为左右两个部分
StringBuffer temp_left = new StringBuffer(Key.substring(0,28));
StringBuffer temp_right = new StringBuffer(Key.substring(28,56));
StringBuffer[] Kid_Key = new StringBuffer[16]; // 这是子密钥表
for (int i = 0; i < 16; i++) {
int digit = Password_Move[i]; // 获取每一轮要移动的次数
StringBuffer Left_temp = new StringBuffer(temp_left);
StringBuffer Right_temp = new StringBuffer(temp_right);
// 进行移动
for (int j = 0; j < 28; j++) {
if(j-digit < 0){
temp_left.setCharAt(j,Left_temp.charAt(j-digit+28));
temp_right.setCharAt(j,Right_temp.charAt(j-digit+28));
}else{
temp_left.setCharAt(j,Left_temp.charAt(j-digit));
temp_right.setCharAt(j,Right_temp.charAt(j-digit));
}
}
StringBuffer string = new StringBuffer(temp_left);
StringBuffer input = string.append(temp_right);
char[] res = new char[48];
for (int j = 0; j < 48; j++) {
res[j] = input.charAt(Password_compress[j]);
}
Kid_Key[i] = new StringBuffer(String.valueOf(res));
}
return Kid_Key;
}
/**
* 扩展置换
* @param string
* @return
*/
public static StringBuffer Extended_permutation(String string){
StringBuffer string_temp = new StringBuffer();
for (int i = 0; i < 48; i++) {
string_temp.append(string.charAt(Permutation_Extended[i]));
}
return string_temp;
}
/**
* 进行 密钥和明文的异或
* @param Key
* @param Right_String
* @return
*/
public static StringBuffer XOR(String Key, String Right_String){
StringBuffer res = new StringBuffer(48);
for (int i = 0; i < Key.length(); i++) {
int temp = 0;
if(Key.charAt(i) != Right_String.charAt(i)){
temp = 1;
}
res.append(temp);
}
return res;
}
/**
* 将二进制转为十进制
* @param s
* @return
*/
public static int Char_Int(char... s){
int sum = 0;
String S = String.valueOf(s);
sum = Integer.parseInt(S,2);
return sum;
}
/**
* 进行 S 盒替换
* @param string
* @return
*/
public static StringBuffer S_P_Replace(String string){
StringBuffer res = new StringBuffer();
for (int i = 0; i < 8; i++) {
char[] temp = string.substring(i*6, (i + 1) *6).toCharArray();
int row = Char_Int(temp[0],temp[5]);
int col = Char_Int(temp[1],temp[2],temp[3],temp[4]);
String s = Integer.toBinaryString(S_BOX[i][row][col]);
StringBuffer stringBuffer = new StringBuffer(s);
while(4-stringBuffer.length()!=0){
stringBuffer.insert(0,'0');
}
res.append(stringBuffer);
}
StringBuffer temp = new StringBuffer();
for (int i = 0; i < 32; i++) {
temp.append(res.charAt(RT_P[i]));
}
res = temp;
return res;
}
/**
* 逆 IP 置换
* @param str
* @return
*/
public static StringBuffer ReverseIPReplacement(String str){
StringBuffer res = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
res.append(str.charAt(RT_InverseIP[i]));
}
return res;
}
/**
* 执行 F 函数,也就是加密函数
* @param Key
* @param Right_String
* @return
*/
public static StringBuffer F(StringBuffer[] Key,String Right_String,String Left_String){
for (int i = 0; i < 16; i++) {
String Right_String1;
StringBuffer res = new StringBuffer();
// 进行扩展置换
Right_String1 = Extended_permutation(Right_String).toString();
StringBuffer xor = XOR(String.valueOf(Key[i]), Right_String1);
// 进行 S 盒和1 P 盒替换
res = S_P_Replace(String.valueOf(xor));
StringBuffer temp = XOR(String.valueOf(res), Left_String);
Left_String = Right_String;
Right_String = String.valueOf(temp);
}
String res = Right_String + Left_String;
StringBuffer stringBuffer = ReverseIPReplacement(res);
return stringBuffer;
}
/**
* 这是解密函数
* @return
*/
public static StringBuffer Decryption(StringBuffer ciphertext, StringBuffer[] Key){
// 获取子秘钥
StringBuffer temp = new StringBuffer();
for (int i = 0; i < 8; i++) {
temp = Key[i];
Key[i] = Key[16 - i -1];
Key[16 - i - 1] = temp;
}
// IP 置换
String substitution = String.valueOf(Substitution_IP(String.valueOf(ciphertext)));
temp = F(Key, substitution.substring(32, 64), substitution.substring(0, 32));
return temp;
}
public static void main(String[] args) {
System.out.println("开始进行 DES 加解密");
// 明文
String Plaintext = "1110101100010110111001100100110011100101101010011010000111111001";
// 密钥
String key = "0000010110110010001011110100011001010110110001100001000101011111";
// 执行 IP 置换 -- 对象为 明文
String substitution = String.valueOf(Substitution_IP(Plaintext));
// 执行密钥置换 -- 对象为密钥
StringBuffer[] stringBuffers = Password_Key(key);
for (int i = 0; i < stringBuffers.length; i++) {
System.out.println(stringBuffers[i]);
}
StringBuffer S = F(stringBuffers,substitution.substring(32, 64), substitution.substring(0, 32) );
StringBuffer decryption = Decryption(S, stringBuffers);
System.out.println("明文为: " + Plaintext);
System.out.println("密钥为: " + key);
System.out.println("加密后的密文为: " + S);
System.out.println("解密后的明文为: " + decryption);
}
}