Implement proxy traversal (3): One of the des Algorithms

Source: Internet
Author: User

  
I have been busy recently. I have a lot of things, I have to live a lot, and I have to pay a lower salary. There is no time to read books. I will move some of my previous technical blogs here. NTLM studied for a while at the end of last year and at the beginning of this year.
Example program. It involves many algorithms and has been checked online for a long time. (Below is the previous blog) recently I want to implement a network programming program through proxy, and summarize the relevant content. Many things come
Self-network communism should also give back to communism. Describes the implementation of the DES algorithm.

 

  
Use the DES algorithm in NTLM to generate LM-HASH and NTLM-HASH (as described later ). We will discuss the implementation of the DES algorithm. Des is Data
Encryption
The abbreviation of standard (Data Encryption Standard. It is an encryption algorithm developed by IBM, which was published by the US National Bureau of Standards in 1977 as a Data Encryption Standard for non-confidential departments, it has been active on the stage of international confidential communication and plays a very important role. The DES algorithm itself is called DEA (Data
Encryption althorithm), so des and DEA are actually the same stuff.

Des: 64-bit data source input, which is obtained through the 56bits key (obtained by removing the CRC check bit from 8 bytes, and the input can also be considered as 64 bits, at least in the program code ), generate a 64bits result. Des is a symmetric encryption method. There are three steps.

In the implementation process, the most depressing thing is that there are a lot of code and examples on the Internet, there are also some detection tools, such as crytotool1.2, But it is strange that they give different results, therefore, it is necessary to carefully understand the entire des process.

Step 1: Change the initialization sequence of the source and key (initail
Permutation)

By moving the bit sequence, a new source 'and key' are generated for subsequent algorithm computation.

A: Source Processing

8 bytes, a total of 64 bits. From 1 to 64, the column is arranged based on the left array below. After moving through the sequence, the right array is arranged as follows, in fact, the first new 8-bit is the 2nd of the Left array.
Column, in the ascending order. The seven groups of 8 bits are sorted from bottom to top By 4th, 6, 8, 1, 3, 5, and 7, respectively. In this way, I think it is very convenient to implement the chip, but the C programming language does not
Directly process by column, so you have to write code segments.
| 1 2 3
4 5 6 7 8 | 58 50 42
34 26 18 10 2 |

| 9 10 11 12 13 14 15
16 | 60 52 44
36 28 20 12 4 |

| 17 18 19 20 21 22 23
24 | 62 54 46
38 30 22 14 6 |

| 25 26 27 28 29 30 31
32 | 64 56 48
40 32 24 16 8 |

| 33 34 35 36 37 38 39 40 |-> | 57 49 41 33 25
17 9 1 |

| 41 42 43 44 45 46 47
48 | 59 51 43
35 27 19 11 3 |

| 49 50 51 52 53 54 55
56 | 61 53 45
37 29 21 13 5 |

| 57 58 59 60 61 62 63
64 | 63 55 47
39 31 23 15 7 | implementation code:


// Transformation sequence

Static int ip_data_seq [] = {


,


,


, 54,


64, 56, 48, 40, 32, 24, 16, 8,


,


59,51, 35,


,


};

// Because there are a large number of bit operations behind it, and the C program usually uses bytes as the unit, we separate the bits in bytes


// It is stored in 8 bytes to facilitate a large number of subsequent operations

Static void storebit (in unsigned char * data, in int data_len, out
Unsigned char * DST ){

Int I =
0;

For (I = 0;
I <data_len; I ++ ){


DST [I * 8] =
Getbit (data [I], 7 );


DST [I * 8 + 1]
= Getbit (data [I], 6 );


DST [I * 8 + 2]
= Getbit (data [I], 5 );


DST [I * 8 + 3]
= Getbit (data [I], 4 );


DST [I * 8 + 4]
= Getbit (data [I], 3 );


DST [I * 8 + 5]
= Getbit (data [I], 2 );


DST [I * 8 + 6]
= Getbit (data [I], 1 );


DST [I * 8 + 7]
= Getbit (data [I], 0 );

}

}
// This is the reverse operation of storebit.

Static void parsebit (in unsigned char * data, out unsigned char *
DST, in int dst_len ){

Int I =
0;

For (I = 0;
I <dst_len; I ++ ){


DST [I] =
Data [8 * I] * 0x80 +



Data [8 * I +
1] * 0x40 +



Data [8 * I +
2] * 0x20 +



Data [8 * I +
3] * 0x10 +



Data [8 * I +
4] * 0x8 +



Data [8 * I +
5] * 0x4 +



Data [8 * I +
6] * 0x2 +



Data [8 * I +
7];

}

}

// Shift operation function

Static void initail_permutation (in unsigned char * data, in int *
Schedule, in int num,








Out unsigned
Char * DST ){

Int I =
0;

Unsigned
Char * temp;

Temp =
(Unsigned char *) malloc (Num );

For (I = 0;
I <num; I ++ ){


Temp [I] =
Data [schedule [I]-1];

}


Memcpy (DST, temp, num );


Free (temp );

}

// Algorithm Main Function

Void algorithm_des (in unsigned char * SRC, in unsigned char *
Secrect,


Out unsigned char * DST ){

Unsigned
Char s [64], key [64], L [32], R [32], K [48], E [48];

Int I =
0;

// Step 1

Storebit (SRC, 8, S );



Initail_permutation (S, ip_data_seq, 64, S );

}

B: Key Processing

For the input 8-byte key, each byte removes its CRC check bit (8th bits), and then generates a new 56-bit key after a similar sequence shift. From this processing, we can also see that the DES algorithm is old and uses the concept of stream, while the CRC checkbit is used in the early stage of relatively old communication with a high error rate.

In this way, we need to pay attention to the order problems in our programs when communication is transmitted in a stream. If we use byte (unsinged
Char) to place an 8bit of information, then the first is the high byte. This is similar to the little_endian and big_endian situations we encounter in network programming. The method for moving a bit is as follows:
| 1 2 3
4 5 6
7
8 | 57 49 41
33 25 17
9 | 57 49 41
33 25 17 9 1 |


| 9 10 11 12 13 14 15
16 | 1 58 50
42 34 26 18 |
| 58 50 42 34 26 18 10 2 |

| 17 18 19 20 21 22 23
24 |
| 10 2 59 51 43 35
27 | 59 51 43
35 27 19 11 3 |

| 25 26 27 28 29 30 31
32 | 19
11 3 60 52 44
36 | 60 52 44
36
|

| 33 34 35 36 37 38 39 40 |-> | 63 55 47 39 31 23 15 |
-> | 63 55 47 39 31 23 15 7 |

| 41 42 43 44 45 46 47
48 | 7 62 54
46 38 30 22 |
| 62 54 46 38 30 22 14 6 |

| 49 50 51 52 53 54 55
56 |
| 14 6 61 53 45 37
29 | 61 53 45
37 29 21 13 5 |

| 57 58 58 60 61 62 63
64 | 21
13 5 28 20 12
4 | 28 20
12
4
|

The middle part is the same as the right part, but we store them in 7 bits or 8 bits. We hope to provide a regular processing through the right part.


Static int ip_key_seq [] = {


,


, 18,


, 27,


, 36,


, 55, 15,


, 22,


, 29,


, 13, 20, 12, 4 };

// Algorithm Main Function

Void algorithm_des (in unsigned char * SRC, in unsigned char *
Secrect,


Out unsigned char * DST ){

Unsigned
Char s [64], key [64], L [32], R [32], K [48], E [48];

Int I =
0;

// Step 1

Storebit (SRC, 8, S );



Storebit (secrect, 8, key );



Initail_permutation (S, ip_data_seq, 64, S );



Initail_permutation (Key, ip_key_seq, 56, key );

}

OK. Finally, the first step is completed, and s [64] and key [56] Are the inputs of the second step algorithm.

Related Links: My network communication articles

NTLM implementation:

  • Proxy traversal (16): NTLM proxy Traversal

  • Proxy traversal (15): NTLM Session Security

  • Implement proxy traversal (14): NTLM type3 message
  • Implement proxy traversal (13): NTLM type2 message
  • Implement proxy traversal (12): NTLM type1 message
  • Proxy traversal (11): NTLMv2 session response
  • Implement proxy traversal (10): NTLMv2 response
  • Implement proxy traversal (9): ntlmv1 response
  • Implement proxy traversal (8): NT-Hash implementation
  • Proxy traversal (7): md4 and MD5
  • Implement proxy traversal (6): LM-Hash implementation
  • Implement proxy traversal (5): DES algorithm 3
  • Implement proxy traversal (4): DES algorithm 2
  • Implement proxy traversal (3): One of the des Algorithms
  • Proxy traversal (2): base64 Algorithm
  • Proxy traversal (1): process and NTLM Algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.