How to implement DES algorithm in ASP (full)

Source: Internet
Author: User
Algorithm this is excerpted from Tsinghua BBS an article, foreign language, the younger brother to turn it into Chinese please master advice.
After the words of the semicolon (;) is the younger brother's translation, the Well number (#) after the little brother's feeling.


How to implement the
Data encryption Standard (DES)

A Step by Step tutorial
Version 1.2


The Data encryption Standard (DES) algorithm, adopted by the U.S.
Government in 1977, are a block cipher that transforms 64-bit data blocks
Under a 56-bit secret key, by means of permutation and substitution. It
is officially described in FIPS PUB 46. The DES algorithm is used for
Many applications within the Government and in the private sector.

This is a tutorial designed to being clear and compact, and to provide a
Newcomer to the DES and all of the necessary information to implement it
Himself, without has to track down printed works or wade through C
Source code. I Welcome any comments.
Matthew Fischer <mfischer@heinous.isca.uiowa.edu>

Above is the introduction, I will not turn over. ;)


Here's how to do it.

1 Process the key.
; Generate key

1.1 Get a 64-bit key from the user. (Every 8th bit is considered a
Parity bit. For a key to have correct parity, each byte should contain
An odd number of "1" bits.)
; Get a 64-bit key from the user. (For each 8-bit group, the 8th digit of each group is the check digit.) If the checksum
correctly, each byte should have a 1

1.2 Calculate the key schedule.
; Calculation key Table

1.2.1 Perform the following permutation on the 64-bit key. (The Parity
Bits are discarded, reducing the key to bits. Bit 1 of the permuted
Block is bit the original key, bit 2 is bit
Being bit 4 of the original key.)
; A 64-bit key is replaced with the following. (remove check bit, the actual length of the key is 56 bits.) After the replacement
The first is the 57th digit of the original key, the second is the original 49th digit, and the 56th bit is the original key's 4th bit. )
# odd permutations, which Big Brother can write a formula?
# It seems to be divided into two parts.
# for (j=57;j<64;j++)
# {
# for (i=j;i<0;i-=8)
# {
# if (k=28)
# break;
# c[k]=i;
# k++;
# }
# This is the top 28, don't know, right? Please correct me.


permuted Choice 1 (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

1.2.2 Split the permuted key into two halves. The ARE
Called C[0] and the last bits are called d[0].
To divide the replacement key into c[0] and d[0] two parts, each 28 bits.

1.2.3 Calculate the subkeys. Start with i = 1.
16 subkeys are computed, starting with the I=1.

1.2.3.1 perform one or two circular left shifts on both c[i-1] and
D[i-1] to get c[i] and d[i], respectively. The number of shifts per
Iteration are given in the table below.
, respectively to C[i-1] and d[i-1] to move left one to two bits of displacement operation, get c[i] and D[i]. Each
; The number of displacements is as follows:
# total 16 times

Iteration # 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Left shifts 1 1 2 2 2 2 2 2 1 2 2 2 2 2 2 1

1.2.3.2 permute the concatenation c[i]d[i] as indicated below. This
Would yield k[i], which is-bits long.
The following table, change the arrangement of c[i] and D[i], and get a 48-bit long k[i].
# Don't know:(
# Have you lost some bits?

permuted Choice 2 (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

1.2.3.3 Loop back to 1.2.3.1 until K[16] has been.
, repeat the process of 1.2.3.1 start, calculate 16 Word key.

2 Process a 64-bit data block.
; Processing a 64-bit block of data.

2.1 Get a 64-bit the data block. If the is shorter than bits, it
Should is padded as appropriate for the application.
; Gets a 64-bit block of data. If the data block is less than 64 digits, it complements 64 bits.
# Maybe it's a 0 fix.

2.2 Perform the following permutation on the "Data block."
The data block is replaced as follows.
# is divided into two parts, first even digit.
# simpler, the formula is not written.

Initial permutation (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

2.3 Split The block into two halves. The "a" bits are called l[0],
And the last bits are called r[0].
Divide the data blocks into l[0] and r[0] two parts.

2.4 Apply The subkeys to the data block. Start with i = 1.
The data block is encrypted with 16 subkeys, starting with the I=1.

2.4.1 Expand the 32-bit r[i-1] into-bits according to the
bit-selection function below.
; The 32-bit r[i-1] of the data block is extended with the following rules.
# can't write a formula. :(
Expansion (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


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.