求用VB写的DES加密的源代码

zt371 2002-04-04 08:31:39
我现在正在做一个程序,现在需要用到这个。那位好心给我发一个,谢谢。
zt371@163.com
...全文
58 点赞 收藏 9
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
zt371 2002-04-04
谢谢楼上的诸位。我正在看。
还有啊,怎么把字符串和二进制字符串相互转换呢?
回复
leus 2002-04-04
收藏
回复
rivershan 2002-04-04
2.4.4.3 用s密箱里的值s[j][ m][ n]替换b[j]。8个s密箱如下所示:
--------
s-boxes1
binary d1d6 => 00 01 10 11
\/ d2..d5 \/ dec 0 1 2 3
0000 0 14 0 4 15
0001 1 4 15 1 12
0010 2 13 7 14 8
0011 3 1 4 8 2
0100 4 2 14 13 4
0101 5 15 2 6 9
0110 6 11 13 2 1
0111 7 8 1 11 7
1000 8 3 10 15 5
1001 9 10 6 12 11
1010 10 6 12 9 3
1011 11 12 11 7 14
1100 12 5 9 3 10
1101 13 9 5 10 0
1110 14 0 3 5 6
1111 15 7 8 0 13
--------
s-boxes2
binary d1d6 => 00 01 10 11
\/ d2..d5 \/ dec 0 1 2 3
0000 0 15 3 0 13
0001 1 1 13 14 8
0010 2 8 4 7 10
0011 3 14 7 11 1
0100 4 6 15 10 3
0101 5 11 2 4 15
0110 6 3 8 13 4
0111 7 4 14 1 2
1000 8 9 12 5 11
1001 9 7 0 8 6
1010 10 2 1 12 7
1011 11 13 10 6 12
1100 12 12 6 9 0
1101 13 0 9 3 5
1110 14 5 11 2 14
1111 15 10 5 15 9
--------
s-boxes3
binary d1d6 => 00 01 10 11
\/ d2..d5 \/ dec 0 1 2 3
0000 0 10 13 13 1
0001 1 0 7 6 10
0010 2 9 0 4 13
0011 3 14 9 9 0
0100 4 6 3 8 6
0101 5 3 4 15 9
0110 6 15 6 3 8
0111 7 5 10 0 7
1000 8 1 2 11 4
1001 9 13 8 1 15
1010 10 12 5 2 14
1011 11 7 14 12 3
1100 12 11 12 5 11
1101 13 4 11 10 5
1110 14 2 15 14 2
1111 15 8 1 7 12
--------
s-boxes4
binary d1d6 => 00 01 10 11
\/ d2..d5 \/ dec 0 1 2 3
0000 0 7 13 10 3
0001 1 13 8 6 15
0010 2 14 11 9 0
0011 3 3 5 0 6
0100 4 0 6 12 10
0101 5 6 15 11 1
0110 6 9 0 7 13
0111 7 10 3 13 8
1000 8 1 4 15 9
1001 9 2 7 1 4
1010 10 8 2 3 5
1011 11 5 12 14 11
1100 12 11 1 5 12
1101 13 12 10 2 7
1110 14 4 14 8 2
1111 15 15 9 4 14
--------
s-boxes5
binary d1d6 => 00 01 10 11
\/ d2..d5 \/ dec 0 1 2 3
0000 0 2 14 4 11
0001 1 12 11 2 8
0010 2 4 2 1 12
0011 3 1 12 11 7
0100 4 7 4 10 1
0101 5 10 7 13 14
0110 6 11 13 7 2
0111 7 6 1 8 13
1000 8 8 5 15 6
1001 9 5 0 9 15
1010 10 3 15 12 0
1011 11 15 10 5 9
1100 12 13 3 6 10
1101 13 0 9 3 4
1110 14 14 8 0 5
1111 15 9 6 14 3
--------
s-boxes6
binary d1d6 => 00 01 10 11
\/ d2..d5 \/ dec 0 1 2 3
0000 0 12 10 9 4
0001 1 1 15 14 3
0010 2 10 4 15 2
0011 3 15 2 5 12
0100 4 9 7 2 9
0101 5 2 12 8 5
0110 6 6 9 12 15
0111 7 8 5 3 10
1000 8 0 6 7 11
1001 9 13 1 0 14
1010 10 3 13 4 1
1011 11 4 14 10 7
1100 12 14 0 1 6
1101 13 7 11 13 0
1110 14 5 3 11 8
1111 15 11 8 6 13
--------
s-boxes7
binary d1d6 => 00 01 10 11
\/ d2..d5 \/ dec 0 1 2 3
0000 0 4 13 1 6
0001 1 11 0 4 11
0010 2 2 11 11 13
0011 3 14 7 13 8
0100 4 15 4 12 1
0101 5 0 9 3 4
0110 6 8 1 7 10
0111 7 13 10 14 7
1000 8 3 14 10 9
1001 9 12 3 15 5
1010 10 9 5 6 0
1011 11 7 12 8 15
1100 12 5 2 0 14
1101 13 10 15 5 2
1110 14 6 8 9 3
1111 15 1 6 2 12
--------
s-boxes8
binary d1d6 => 00 01 10 11
\/ d2..d5 \/ dec 0 1 2 3
0000 0 13 1 7 2
0001 1 2 15 11 1
0010 2 8 13 4 14
0011 3 4 8 1 7
0100 4 6 10 9 4
0101 5 15 3 12 10
0110 6 11 7 14 8
0111 7 1 4 2 13
1000 8 10 12 0 15
1001 9 9 5 6 12
1010 10 3 6 10 9
1011 11 14 11 13 0
1100 12 5 0 15 3
1101 13 0 14 3 5
1110 14 12 9 5 6
1111 15 7 2 8 11
2.4.4.4 返回2.4.4.1直至8个数据块都被替换。
2.4.5 把b[1]至b[8] 顺序串联起来得到一个32位数。对这个数做如下变换:
bit goes to bit bit goes to bit
16 1 2 17
7 2 8 18
20 3 24 19
21 4 14 20
29 5 32 21
12 6 27 22
28 7 3 23
17 8 9 24
1 9 19 25
15 10 13 26
23 11 30 27
26 12 6 28
5 13 22 29
18 14 11 30
31 15 4 31
10 16 25 32
2.4.6 把得到的结果与l[i-1]作异或运算。把计算结果賦给r[i]。
2.4.7 把r[i-1]的值賦给l[i]。
2.4.8 从2.4.1循环执行,直到k[16]也被用到。
2.5 把r[16]和l[16] 顺序串联起来得到一个64位数。对这个数实施2.2变换的
逆变换。
以上就是des算法如何加密一段64位数据块。解密时用同样的过程,只需把1
6个子密钥的顺续颠倒过来,应用的顺序为k[16],k[15],k[14],。。。。k[1]。
回复
rivershan 2002-04-04
经典加密算法在VB中的实现--DES
一、算法实现
1.处理密钥:
1.1 从用户处获得64位密钥.(每第8位为校验位,为使密钥有正确的奇偶校验,每
个密钥要有奇数个”1”位.(本文如未特指,均指二进制位)
1.2 具体过程:
1.2.1对密钥实施变换,使得变换以后的密钥的各个位与原密钥位对应关系如
下表所示:
表一为忽略校验位以后情况
1 2 3 4 5 6 7 8 9 10 11 12
13 14 15 16 17 18 19 20 21 22 23 24
25 26 27 28
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
29 30 31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50 51 52
53 54 55 56
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 把变换后的密钥等分成两部分,前28位记为c[0], 后28位记为d[0].
1.2.3 计算子密钥(共16个), 从i=1开始。
1.2.3.1 分别对c[i-1],d[i-1]作循环左移来生成c[i],d[i].(共16次)。每次循环左移位数如下表所示:
循环次数 1 2 3 4 5 6 7 8 9 10 11 1
2 13 14 15 16
左移位数 1 1 2 2 2 2 2 2 1 2 2
2 2 2 2 1
1.2.3.2 串联c[i],d[i],得到一个56位数,然后对此数作如下变换以产生48位子密钥k[i]。
变换过程如下:
1 2 3 4 5 6 7 8 9 10 11 12
13 14 15 16 17 18 19 20 21 22 23 24

14 17 11 24 1 5 3 28 15 6 21 10
23 19 12 4 26 8 16 7 27 20 13 2

25 26 27 28 29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45 46 47 48

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 按以上方法计算出16个子密钥。
2.对64位数据块的处理:
2.1 把数据分成64位的数据块,不够64位的以适当的方式填补。
2.2对数据块作变换。
bit goes to bit bit goes to bit
58 1 57 33
50 2 49 34
42 3 41 35
34 4 33 36
26 5 25 37
18 6 17 38
10 7 9 39
2 8 1 40
60 9 59 41
52 10 51 42
44 11 43 43
36 12 35 44
28 13 27 45
20 14 19 46
12 15 11 47
4 16 3 48
62 17 61 49
54 18 53 50
46 19 45 51
38 20 37 52
30 21 29 53
22 22 21 54
14 23 13 55
6 24 5 56
64 25 63 57
56 26 55 58
48 27 47 59
40 28 39 60
32 29 31 61
24 30 23 62
16 31 15 63
8 32 7 64
2.3 将变换后的数据块等分成前后两部分,前32位记为l[0],后32位记为r[0]。
2.4 用16个子密钥对数据加密。
2.4.1 根据下面的扩冲函数e,扩展32位的成48位
bit goes to bit bit goes to bit bit goes to bit bit
goes to bit
32 1 8 13 16 25 24
37
1 2 9 14 17 26 25
38
2 3 10 15 18 27 26
39
3 4 11 16 19 28 27
40
4 5 12 17 20 29 28
41
5 6 13 18 21 30 29
42
4 7 12 19 20 31 28
43
5 8 13 20 21 32 29
44
6 9 14 21 22 33 30
45
7 10 15 22 23 34 31
46
8 11 16 23 24 35 32
47
9 12 17 24 25 36 1
48
2.4.2 用e{r[i-1]}与k[i]作异或运算。
2.4.3 把所得的48位数分成8个6位数。1-6位为b[1],7-12位为b[2],……43-4
8位为b[8]。
2.4.4 用s密箱里的值替换b[j]。从j=1开始。s密箱里的值为4位数,共8个s密

2.4.4.1 取出b[j]的第1和第6位串联起来成一个2位数,记为m.。m即是s密箱
里用来替换b[j]的数所在的列数。
2.4.4.2 取出b[j]的第2至第5位串联起来成一个4位数,记为n。n即是s密箱里
用来替换b[j]的数所在的行数。
回复
TechnoFantasy 2002-04-04
U.P
我有Delphi实现的加密和解密代码。
回复
jamex 2002-04-04
DES算法实现过程分析

http://www.gspcc.com/jshlt/ShowAnnounce.asp?boardID=4&RootID=323&ID=323

http://www.gspcc.com/jshlt/ShowAnnounce.asp?boardID=4&RootID=324&ID=324
回复
jamex 2002-04-04
2.4.4.1 Take the 1st and 6th bits of B[j] together as a 2-bit value
(call it m) indicating the row in S[j] to look in for the substitution.

2.4.4.2 Take the 2nd through 5th bits of B[j] together as a 4-bit
value (call it n) indicating the column in S[j] to find the substitution.

2.4.4.3 Replace B[j] with S[j][m][n].

Substitution Box 1 (S[1])

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

S[2]

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

S[3]

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

S[4]

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

S[5]

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

S[6]

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

S[7]

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

S[8]

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

2.4.4.4 Loop back to 2.4.4.1 until all 8 blocks have been replaced.

2.4.5 Permute the concatenation of B[1] through B[8] as indicated below.

Permutation 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

2.4.6 Exclusive-or the resulting value with L[i-1]. Thus, all together,
your R[i] = L[i-1] xor P(S[1](B[1])...S[8](B[8])), where B[j] is a 6-bit
block of E(R[i-1]) xor K[i]. (The function for R[i] is written as, R[i] =
L[i-1] xor f(R[i-1], K[i]).)

2.4.7 L[i] = R[i-1].

2.4.8 Loop back to 2.4.1 until K[16] has been applied.

2.5 Perform the following permutation on the block R[16]L[16].

Final Permutation (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


This has been a description of how to use the DES algorithm to encrypt
one 64-bit block. To decrypt, use the same process, but just use the keys
K[i] in reverse order. That is, instead of applying K[1] for the first
iteration, apply K[16], and then K[15] for the second, on down to K[1].

Summaries:

Key schedule:
C[0]D[0] = PC1(key)
for 1 <= i <= 16
C[i] = LS[i](C[i-1])
D[i] = LS[i](D[i-1])
K[i] = PC2(C[i]D[i])

Encipherment:
L[0]R[0] = IP(plain block)
for 1 <= i <= 16
L[i] = R[i-1]
R[i] = L[i-1] xor f(R[i-1], K[i])
cipher block = FP(R[16]L[16])

Decipherment:
R[16]L[16] = IP(cipher block)
for 1 <= i <= 16
R[i-1] = L[i]
L[i-1] = R[i] xor f(L[i], K[i])
plain block = FP(L[0]R[0])


To encrypt or decrypt more than 64 bits there are four official modes
(defined in FIPS PUB 81). One is to go through the above-described
process for each block in succession. This is called Electronic Codebook
(ECB) mode. A stronger method is to exclusive-or each plaintext block
with the preceding ciphertext block prior to encryption. (The first
block is exclusive-or'ed with a secret 64-bit initialization vector
(IV).) This is called Cipher Block Chaining (CBC) mode. The other two
modes are Output Feedback (OFB) and Cipher Feedback (CFB).

When it comes to padding the data block, there are several options. One
is to simply append zeros. Two suggested by FIPS PUB 81 are, if the data
is binary data, fill up the block with bits that are the opposite of the
last bit of data, or, if the data is ASCII data, fill up the block with
random bytes and put the ASCII character for the number of pad bytes in
the last byte of the block. Another technique is to pad the block with
random bytes and in the last 3 bits store the original number of data bytes.

The DES algorithm can also be used to calculate checksums up to 64 bits
long (see FIPS PUB 113). If the number of data bits to be checksummed is
not a multiple of 64, the last data block should be padded with zeros. If
the data is ASCII data, the first bit of each byte should be set to 0.
The data is then encrypted in CBC mode with IV = 0. The leftmost n bits
(where 16 <= n <= 64, and n is a multiple of 8) of the final ciphertext
block are an n-bit checksum.

回复
jamex 2002-04-04
DES加密法

问题描述:

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, is 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 be clear and compact, and to provide a
newcomer to the DES with all the necessary information to implement it
himself, without having to track down printed works or wade through C
source code. I welcome any comments.
Matthew Fischer <mfischer@heinous.isca.uiowa.edu>


Here's how to do it, step by step:

1 Process the 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.)

1.2 Calculate the key schedule.

1.2.1 Perform the following permutation on the 64-bit key. (The parity
bits are discarded, reducing the key to 56 bits. Bit 1 of the permuted
block is bit 57 of the original key, bit 2 is bit 49, and so on with bit
56 being bit 4 of the original key.)

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 first 28 bits are
called C[0] and the last 28 bits are called D[0].

1.2.3 Calculate the 16 subkeys. Start with 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.

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
will yield K[i], which is 48 bits long.

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 calculated.

2 Process a 64-bit data block.

2.1 Get a 64-bit data block. If the block is shorter than 64 bits, it
should be padded as appropriate for the application.

2.2 Perform the following permutation on the data block.

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 first 32 bits are called L[0],
and the last 32 bits are called R[0].

2.4 Apply the 16 subkeys to the data block. Start with i = 1.

2.4.1 Expand the 32-bit R[i-1] into 48 bits according to the
bit-selection function below.

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

2.4.2 Exclusive-or E(R[i-1]) with K[i].

2.4.3 Break E(R[i-1]) xor K[i] into eight 6-bit blocks. Bits 1-6 are
B[1], bits 7-12 are B[2], and so on with bits 43-48 being B[8].

2.4.4 Substitute the values found in the S-boxes for all B[j]. Start
with j = 1. All values in the S-boxes should be considered 4 bits wide.

回复
gump2000 2002-04-04
这里有例子代码
http://www.21code.com/codebase/?pos=down&id=981
回复
发帖
VB基础类
创建于2007-09-28

7476

社区成员

VB 基础类
申请成为版主
帖子事件
创建了帖子
2002-04-04 08:31
社区公告
暂无公告