Ìåòîäû è ñðåäñòâà çàùèòû èíôîðìàöèè


Ìåõàíèçì äåéñòâèÿ S-áëîêîâ


Ïðåîáðàçîâàíèå, ñ ïîìîùüþ êîòîðîãî 48-ðàçðÿäíûé áëîê ïðåîáðàçóåòñÿ â 32-ðàçðÿäíûé, ñâîäèòñÿ ê âûáîðêå âîñüìè òåòðàä èç 8 òàáëèö (S-áëîêîâ) ðàçìåðîì 4 ´ 16 (òàáë. 18.7). Èç êàæäîãî S-áëîêà âûáèðàåòñÿ îäíà òåòðàäà. Äëÿ ýòîãî 48-ðàçðÿäíûé áëîê äåëèòñÿ ïîñëåäîâàòåëüíî íà 8 êîìáèíàöèé ïî 6 áèò êàæäàÿ. Ïåðâàÿ êîìáèíàöèÿ (ñëåâà) ÿâëÿåòñÿ âõîäîì â ïåðâûé S-áëîê, âòîðàÿ — âî âòîðîé è ò.ä. Ïðè ýòîì ïåðâûé è ïîñëåäíèé áèòû êîìáèíàöèè çàäàþò íîìåð ñòðîêè, à îñòàëüíûå 4 áèòà — íîìåð ñòîëáöà S-áëîêà, íà ïåðåñå÷åíèè êîòîðûõ ðàñïîëîæåíà ñîîòâåòñòâóþùàÿ òåòðàäà. Êîíêðåòíûå çíà÷åíèÿ Si (i = 1, …, 8) ïðåäñòàâëåíû â òàáë. 18.7.

Òàáëèöà 18.7. Òàáëèöû S -áëîêîâ äëÿ DES

S1

0

1

2

3



4

5

6

7

8

9

10

11

12

13

14

15

0

14

4

13

1

2

15

11

8

3

10

6

12

5

9

0

7

1

0

15

7

4

14

2

13

1

10

6

12

11

9

5

3

8

2

4

1

14

8

13

6

2

11

15

12

9

7

3

10

5

0

3

15

12

8

2

4

9

1

7

5

11

3

14

10

0

6

13

S2

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

0

15

1

8

14

6

11

3

4

9

7

2

13

12

0

5

10

1

3

13

4

7

15

2

8

14

12

0

1

10

6

9

11

5

2

0

14

7

11

10

4

13

1

5

8

12

6

9

3

2

15

3

13

8

10

1

3

15

4

2

11

6

7

12

0

5

14

9

S3

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

0

10

0

9

14

6

3

15

5

1

13

12

7

11

4

2

8

1

13

7

0

9

3

4

6

10

2

8

5

14

12

11

15

1

2

13

6

4

9

8

15

3

0

11

1

2

12

5

10

14

7

3

1

10

13

0

6

9

8

7

4

15

14

3

11

5

2

12

S4

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

0

7

13

14

3

0

6

9

10

1

2

8

5

11

12

4

15

1

13

8

11

5

6

15

0

3

4

7

2

12

1

10

14

9

2

10

6

9

0

12

11

7

13

15

1

3

14

5

2

8

4

3

3

15

0

6

10

1

13

8

9

4

5

11

12

7

2

14

S5

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

0

2

12

4

1

7

10

11

6

8

5

3

15

13

0

14

9

1

14

11

2

12

4

7

13

1

5

0

15

10

3

9

8

6

2

4

2

1

11

10

13

7

8

15

9

12

5

6

3

0

14

3

11

8

12

7

1

14

2

13

6

15

0

9

10

4

5

3

S6

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

0

12

1

10

15

9

2

6

8

0

13

3

4

14

7

5

11

1

10

15

4

2

7

12

9

5

6

1

13

14

0

11

3

8

2

9

14

15

5

2

8

12

3

7

0

4

10

1

13

11

6

3

4

3

2

12

9

5

15

10

11

14

1

7

6

0

8

13

<
Îêîí÷àíèå òàáëèöû 18.7

S7

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

0

4

11

2

14

15

0

8

13

3

32

9

7

5

10

6

1

1

13

0

11

7

4

9

1

10

14

3

5

12

2

15

8

6

2

1

4

11

13

12

3

7

14

10

15

6

8

0

5

9

2

3

6

11

13

8

1

4

10

7

9

5

0

15

14

2

3

12

S8

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

0

13

2

8

4

6

15

11

1

10

9

3

14

5

0

12

7

1

1

15

13

8

10

3

7

4

12

5

6

11

0

14

9

2

2

7

11

4

1

9

12

14

2

0

6

10

13

15

3

5

8

3

2

1

14

7

4

10

8

13

15

12

9

0

3

5

6

11

Ïðèìåð ðåàëèçàöèè àëãîðèòìà DES ïðåäñòàâëåí â ëèñòèíãàõ 18.3 è 18.4 (êîìïèëÿòîð — PowerBasic).

Ëèñòèíã 18.3. Ïðèìåð ðåàëèçàöèè

àëãîðèòìà DES íà ÿçûêå Basic äëÿ øèôðîâàíèÿ ôàéëîâ

$CPU 80386

$FLOAT NPX

$OPTIMIZE SPEED

$LIB ALL-

$OPTION CNTLBREAK ON

DECLARE FUNCTION MYBIN$ (n%)

DECLARE FUNCTION desalg$ (a$)

DECLARE SUB f (i%, a%(), x%())

DECLARE SUB transpose (datax%(), T%(), n%)

DECLARE SUB mrotate (keyx%())

DECLARE SUB stob (a$, mbits%())                

DECLARE SUB btos (mbits%(), a$)                

DECLARE SUB letbe (target%(), source%(), LAST%)

DECLARE SUB init (x() AS INTEGER, n%)          

DECLARE SUB sboxinit (b() AS INTEGER)           

DECLARE SUB xtob (a$, mbits%())                

DIM s(1 TO 8, 1 TO 64) AS shared INTEGER

Ïðîäîëæåíèå ëèñòèíãà 18.3

' Èíèöèàëèçàöèÿ

RESTORE InitialTrl

DIM InitialTr(1 TO 64) AS shared INTEGER



init InitialTr(), 64

RESTORE FinalTrl

DIM FinalTr( 1 TO 64) AS shared INTEGER

init FinalTr(), 64

RESTORE swappyl

DIM swappy(1 TO 64) AS shared INTEGER

init swappy(), 64

RESTORE KeyTr1l

DIM KeyTr1(1 TO 56) AS shared INTEGER

init KeyTr1(), 56

RESTORE KeyTr2l

DIM KeyTr2(1 TO 48) AS shared INTEGER

init KeyTr2(), 48

RESTORE etrl

DIM etr(1 TO 48) AS shared INTEGER

init etr(), 48

RESTORE ptrl

DIM ptr(1 TO 32) AS shared INTEGER

init ptr(), 32

sboxinit s()

RESTORE rotsl

DIM rots(1 TO 16) AS shared INTEGER

init rots(), 16

DIM XR(1 TO 56) AS shared INTEGER            

DIM EF(1 TO 64) AS shared INTEGER           

DIM ikeyf(1 TO 64) AS shared INTEGER        

DIM yf(1 TO 64) AS shared INTEGER

Ïðîäîëæåíèå ëèñòèíãà 18.3

DIM ades(1 TO 64) AS shared INTEGER         

DIM bdes(1 TO 64) AS shared INTEGER         

DIM xdes(1 TO 64) AS shared INTEGER         

DIM XT(1 TO 64)   AS shared INTEGER         

DIM P2(1 TO 64)   AS shared INTEGER         

' Ãëàâíàÿ ïðîãðàììà

main:                                   

CLS                                     

parm$ = ltrim$(rtrim$(COMMAND$))+" "

IF LEN(parm$) > 1 THEN

 Plainf$ = LTRIM$(RTRIM$(LEFT$(parm$, INSTR(parm$, " "))))

 PRINT "Èñõîäíûé ôàéë : "; Plainf$

ELSE

INPUT "Èñõîäíûé ôàéë : ", plainf$

END IF

if len(plainf$)=0 then

 print : print "ÑÁÎÉ: ââåäèòå èìÿ ôàéëà!"

 system

end if

OPEN plainf$ FOR RANDOM AS 1

lof1& = LOF(1)

IF lof1& = 0 THEN

 CLOSE #1

 KILL plainf$

 PRINT : PRINT "Ôàéë íå íàéäåí!";

 SYSTEM

ELSE

 IF lof1& > 9999999 THEN PRINT : PRINT "Èñõîäíûé ôàéë ñëèøêîì âåëèê!": SYSTEM

 CLOSE #1

 OPEN plainf$ for binary access read as #1

END IF

cipherf$ = ""

sp0% = 0: sp% = 0

DO

 sp0% = sp%

 sp% = INSTR(sp% + 1, plainf$, "\")

Ïðîäîëæåíèå ëèñòèíãà 18.3

LOOP WHILE sp% > 0

bplainf$ = RIGHT$(plainf$, LEN(plainf$) - (sp0%))

PRINT "Ñîõðàíÿåìîå èìÿ ôàéëà: "; bplainf$



pp% = INSTR(sp0% + 1, plainf$, ".")

IF pp% = 0 THEN

 dcipherf$ = plainf$ + ".DES"

ELSE

 dcipherf$ = LEFT$(plainf$, pp% - 1) + ".DES"

END IF

PRINT " Ïî óìîë÷àíèþ : "; dcipherf$

INPUT "Âûõîäíîé ôàéë : ", cipherf$

IF cipherf$ = "" THEN cipherf$ = dcipherf$

OPEN cipherf$ FOR RANDOM AS 2

IF LOF(2) > 0 THEN

 CLOSE #2

 PRINT : PRINT "Âûõîäíîé ôàéë óæå ñóùåñòâóåò!"

 SYSTEM

ELSE

 CLOSE #2

 OPEN cipherf$ FOR binary AS 2

END IF

PW$ = ""

LOCATE 9, 1

INPUT ; "     Ïàðîëü : ", PW$

IF (LEN(PW$) < 8) THEN PW$ = PW$ + STRING$(8 - LEN(PW$), 0)

IF len(pw$) = 16 then

   LOCATE 9, 8: PRINT "Ïàðîëü : "; STRING$(16, 15); STRING$(10, " ")

   PW$ = ucase$(PW$)       

   xtob PW$, P2()          

ELSE

   LOCATE 9, 8: PRINT "Ïàðîëü : "; STRING$(8, 15); STRING$(10, " ")

   PW$ = LEFT$(PW$, 8)

   stob PW$, P2()

end IF

Ïðîäîëæåíèå ëèñòèíãà 18.3

transpose P2(), KeyTr1(), 56    

ciphertekst$ = ""

blocks& = lof1& \ 256

w = RND(-INT(TIMER))

anything$ = ""

FOR i% = 1 TO 12

 anything$ = anything$ + CHR$(128 + INT(127 * RND(1)))

NEXT i%

header$ = "#" + LTRIM$(STR$(lof1&))

header$ = "DES" + LEFT$(anything$, 8 - LEN(header$)) + header$

header$ = header$ + RIGHT$(anything$, 12 - LEN(bplainf$)) +

          "#" + bplainf$

cheader$=desalg$(left$(header$,8))+desalg$(MID$(header$,9,8))+desalg$(right$(header$,8))

put$ #2, cheader$

LOCATE 10, 8:  PRINT ; "Øèôðîâàíèå:   0 %";

inblock$=space$(256)

FOR n& = 1 TO blocks&

 get$ #1,256,inblock$

 outblock$=""

  for b%=1 to 256 step 8

   outblock$ = outblock$+desalg$(mid$(inblock$,b%,8))

  next   

  Put$ #2, outblock$

  LOCATE 10, 19: PRINT ; USING "###"; (n& / blocks&) * 100;

NEXT n&

rest1 = lof1& MOD 256

rest2 = lof1& MOD 8

rest  = rest1-rest2

IF rest1 > 0 THEN



 outblock$=""

 get$ #1,rest1,inblock$

Ïðîäîëæåíèå ëèñòèíãà 18.3

 if rest2 > 0 then

  inblock$=inblock$+left$(anything$,(8-rest2))

 end if

 for b%=1 to len(inblock$) step 8

  outblock$ = outblock$+desalg$(mid$(inblock$,b%,8))

 next   

 Put$ #2, outblock$

END IF

CLOSE

LOCATE 10, 19: PRINT "100 %  çàâåðøåíî"

PRINT : PRINT "Øèôðîâàíèå ïî àëãîðèòìó DES çàâåðøåíî."

PRINT

SYSTEM

' Äàííûå è ôóíêöèè

InitialTrl:

DATA 58,50,42,34,26,18,10,02,60,52,44,36,28,20,12,04

DATA 62,54,46,38,30,22,14,06,64,56,48,40,32,24,16,08

DATA 57,49,41,33,25,17,09,01,59,51,43,35,27,19,11,03

DATA 61,53,45,37,29,21,13,05,63,55,47,39,31,23,15,07

FinalTrl:

DATA 40,08,48,16,56,24,64,32,39,07,47,15,55,23,63,31

DATA 38,06,46,14,54,22,62,30,37,05,45,13,53,21,61,29

DATA 36,04,44,12,52,20,60,28,35,03,43,11,51,19,59,27

DATA 34,02,42,10,50,18,58,26,33,01,41,09,49,17,57,25

swappyl:

DATA 33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48

DATA 49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64

DATA 01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16

DATA 17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32

KeyTr1l:

DATA 57,49,41,33,25,17,09,01,58,50,42,34,26,18,10,02

DATA 59,51,43,35,27,19,11,03,60,52,44,36

DATA 63,55,47,39,31,23,15,07,62,54,46,38,30,22,14,06

DATA 61,53,45,37,29,21,13,05,28,20,12,04

Ïðîäîëæåíèå ëèñòèíãà 18.3

KeyTr2l:

DATA 14,17,11,24,01,05,03,28,15,06,21,10,23,19,12,04

DATA 26,08,16,07,27,20,13,02,41,52,31,37,47,55,30,40

DATA 51,45,33,48,44,49,39,56,34,53,46,42,50,36,29,32

etrl:

DATA 32,01,02,03,04,05,04,05,06,07,08,09,08,09,10,11

DATA 12,13,12,13,14,15,16,17,16,17,18,19,20,21,20,21

DATA 22,23,24,25,24,25,26,27,28,29,28,29,30,31,32,01

ptrl:

DATA 16,07,20,21,29,12,28,17,01,15,23,26,05,18,31,10

DATA 02,08,24,14,32,27,03,09,19,13,30,06,22,11,04,25

sboxesl:

DATA 14,04,13,01,02,15,11,08,03,10,06,12,05,09,00,07

DATA 00,15,07,04,14,02,13,01,10,06,12,11,09,05,03,08

DATA 04,01,14,08,13,06,02,11,15,12,09,07,03,10,05,00

DATA 15,12,08,02,04,09,01,07,05,11,03,14,10,00,06,13



DATA 15,01,08,14,06,11,03,04,09,07,02,13,12,00,05,10

DATA 03,13,04,07,15,02,08,14,12,00,01,10,06,09,11,05

DATA 00,14,07,11,10,04,13,01,05,08,12,06,09,03,02,15

DATA 13,08,10,01,03,15,04,02,11,06,07,12,00,05,14,09

DATA 10,00,09,14,06,03,15,05,01,13,12,07,11,04,02,08

DATA 13,07,00,09,03,04,06,10,02,08,05,14,12,11,15,01

DATA 13,06,04,09,08,15,03,00,11,01,02,12,05,10,14,07

DATA 01,10,13,00,06,09,08,07,04,15,14,03,11,05,02,12

DATA 07,13,14,03,00,06,09,10,01,02,08,05,11,12,04,15

DATA 13,08,11,05,06,15,00,03,04,07,02,12,01,10,14,09

DATA 10,06,09,00,12,11,07,13,15,01,03,14,05,02,08,04

DATA 03,15,00,06,10,01,13,08,09,04,05,11,12,07,02,14

DATA 02,12,04,01,07,10,11,06,08,05,03,15,13,00,14,09

DATA 14,11,02,12,04,07,13,01,05,00,15,10,03,09,08,06

DATA 04,02,01,11,10,13,07,08,15,09,12,05,06,03,00,14

DATA 11,08,12,07,01,14,02,13,06,15,00,09,10,04,05,03

Ïðîäîëæåíèå ëèñòèíãà 18.3

DATA 12,01,10,15,09,02,06,08,00,13,03,04,14,07,05,11

DATA 10,15,04,02,07,12,09,05,06,01,13,14,00,11,03,08

DATA 09,14,15,05,02,08,12,03,07,00,04,10,01,13,11,06

DATA 04,03,02,12,09,05,15,10,11,14,01,07,06,00,08,13

DATA 04,11,02,14,15,00,08,13,03,12,09,07,05,10,06,01

DATA 13,00,11,07,04,09,01,10,14,03,05,12,02,15,08,06

DATA 01,04,11,13,12,03,07,14,10,15,06,08,00,05,09,02

DATA 06,11,13,08,01,04,10,07,09,05,00,15,14,02,03,12

DATA 13,02,08,04,06,15,11,01,10,09,03,14,05,00,12,07

DATA 01,15,13,08,10,03,07,04,12,05,06,11,00,14,09,02

DATA 07,11,04,01,09,12,14,02,00,06,10,13,15,03,05,08

DATA 02,01,14,07,04,10,08,13,15,12,09,00,03,05,06,11

rotsl:

DATA 1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1       

                                            SUB btos (mbits() AS INTEGER, a$)

a$ = ""

FOR i% = 1 TO 8

 w% = 0

 FOR j% = 1 TO 8

  w% = w% + ((mbits(((i% - 1) * 8) + j%)) * (2 ^ (8 - j%)))

 NEXT j%

 a$ = a$ + CHR$(w%)

NEXT i%

END SUB

FUNCTION desalg$ (a$)

 temp$ = ""

 stob a$, ades()

 transpose ades(), InitialTr(), 64      

 FOR i% = 1 TO 16                       



  letbe bdes(), ades(), 64               

  FOR j% = 1 TO 32

   ades(j%) = bdes(j% + 32)             

  NEXT j%

  f i%, ades(), xdes()                  

  FOR j% = 1 TO 32                      

   ades(j% + 32) = (bdes(j%) + xdes(j%)) MOD 2

Ïðîäîëæåíèå ëèñòèíãà 18.3

  NEXT j%

 NEXT i%

 transpose ades(), swappy(), 64         

 transpose ades(), FinalTr(), 64        

 btos ades(), temp$

 desalg$ = temp$

END FUNCTION

SUB f (i%, a() AS INTEGER, x() AS INTEGER)

h% = i%: letbe EF(), a(), 64

transpose EF(), etr(), 48                

FOR j% = 1 TO rots(h%)                   

 mrotate P2()                            

NEXT j%

letbe ikeyf(), P2(), 64: transpose ikeyf(), KeyTr2(), 48

FOR j% = 1 TO 48

 yf(j%) = (EF(j%) + ikeyf(j%)) MOD 2

NEXT j%

FOR k% = 1 TO 8

 k6% = 6 * k%: k4% = 4 * k%

 r% = (32 * yf(k6% - 5)) + (16 * yf(k6%)) + (8 * yf(k6% - 4)) + (4 * yf(k6% - 3)) + (2 * yf(k6% - 2)) + yf(k6% - 1) + 1

 x(k4% - 3) = (s(k%, r%) \ 8) MOD 2: x(k4% - 2) = (s(k%, r%) \ 4) MOD 2

 x(k4% - 1) = (s(k%, r%) \ 2) MOD 2: x(k4%) = s(k%, r%) MOD 2

NEXT k%

transpose x(), ptr(), 32

END SUB

SUB init (x() AS INTEGER, n%)

 FOR i% = 1 TO n%

  READ x(i%)

 NEXT i%

END SUB

SUB letbe (target() AS INTEGER, source() AS INTEGER, LAST%)

FOR i% = 1 TO LAST%

 target(i%) = source(i%)

NEXT i%

END SUB

Ïðîäîëæåíèå ëèñòèíãà 18.3

FUNCTION MYBIN$ (n%)

LOCAL ST$

p% = n%

ST$=""

FOR i% = 1 TO 8

 IF (p% MOD 2) THEN

  ST$ = "1" + ST$

 ELSE

  ST$ = "0" + ST$

 END IF

 p% = p% \ 2

NEXT i%

MYBIN$ = ST$

END FUNCTION

SUB mrotate (keyr() AS INTEGER)    

letbe XR(), keyr(), 56

FOR i% = 1 TO 55

 XR(i%) = XR(i% + 1)

NEXT i%

XR(28) = keyr(1): XR(56) = keyr(29)

letbe keyr(), XR(), 56

END SUB

SUB sboxinit (b() AS INTEGER)

RESTORE sboxesl

FOR i% = 1 TO 8

 FOR j% = 1 TO 64

  READ b(i%, j%)

 NEXT j%

NEXT i%

END SUB

SUB stob (a$, mbits() AS INTEGER)

FOR i% = 1 TO 8

 b$ = MYBIN$(ASC(MID$(a$, i%, 1)))

 FOR j% = 1 TO 8

  mbits(((i% - 1) * 8) + j%) = ASC(MID$(b$, j%, 1)) - 48



 NEXT j%

NEXT i%

END SUB

Ïðîäîëæåíèå ëèñòèíãà 18.3

SUB transpose (datax() AS INTEGER, T() AS INTEGER, n%)

letbe XT(), datax(), 64

FOR i% = 1 TO n%

 datax(i%) = XT(T(i%))

NEXT i%

END SUB

SUB xtob (a$, mbits() AS INTEGER)

LOCAL X$,NIBBLE$

FOR i% = 1 to 16

    X$ = MID$(a$,i%,1)

    SELECT CASE X$

    CASE "0"

     NIBBLE$ = "0000"

    CASE "1"

     NIBBLE$ = "0001"

    CASE "2"

     NIBBLE$ = "0010"

    CASE "3"

     NIBBLE$ = "0011"

    CASE "4"

     NIBBLE$ = "0100"

    CASE "5"

     NIBBLE$ = "0101"

    CASE "6"

     NIBBLE$ = "0110"

    CASE "7"

     NIBBLE$ = "0111"

    CASE "8"

     NIBBLE$ = "1000"

    CASE "9"

     NIBBLE$ = "1001"

    CASE "A"

     NIBBLE$ = "1010"

    CASE "B"

     NIBBLE$ = "1011"

    CASE "C"

     NIBBLE$ = "1100"

    CASE "D"

     NIBBLE$ = "1101"

    CASE "E"

Îêîí÷àíèå ëèñòèíãà 18.3

     NIBBLE$ = "1110"

    CASE "F"

     NIBBLE$ = "1111"

    CASE ELSE

     Print "Íå ÿâëÿåòñÿ 16-ðè÷íûì çíà÷åíèåì!"

     SYSTEM

    END SELECT

    FOR j% = 1 to 4

        mbits(((i% - 1) * 4) + j%) = ASC(MID$(NIBBLE$, j%, 1)) - 48

    NEXT j%

  NEXT i%

END SUB

Ëèñòèíã 18.4. Ïðèìåð ðåàëèçàöèè àëãîðèòìà

DES íà ÿçûêå Basic äëÿ ðàñøèôðîâûâàíèÿ ôàéëîâ

$CPU 80386

$FLOAT NPX

$OPTIMIZE SPEED

$LIB ALL-

$OPTION CNTLBREAK ON

DECLARE FUNCTION MYBIN$ (n%)

DECLARE FUNCTION desalg$ (a$)

DECLARE SUB f (i%, a%(), x%())

DECLARE SUB transpose (datax%(), T%(), n%)

DECLARE SUB mrotate (keyx%())                   

DECLARE SUB stob (a$, mbits%())

DECLARE SUB btos (mbits%(), a$)

DECLARE SUB letbe (target%(), source%(), last%)

DECLARE SUB init (x() AS INTEGER, n%)

DECLARE SUB sboxinit (b() AS INTEGER)

DECLARE SUB xtob (a$, mbits%())           



DIM s(1 TO 8, 1 TO 64) AS shared INTEGER

' Èíèöèàëèçàöèÿ

RESTORE InitialTrl

Ïðîäîëæåíèå ëèñòèíãà 18.4

DIM InitialTr(1 TO 64) AS shared INTEGER

init InitialTr(), 64

RESTORE FinalTrl

DIM FinalTr(1 TO 64) AS shared INTEGER

init FinalTr(), 64

RESTORE swappyl

DIM swappy(1 TO 64) AS shared INTEGER

init swappy(), 64

RESTORE KeyTr1l

DIM KeyTr1(1 TO 56) AS shared INTEGER

init KeyTr1(), 56

RESTORE KeyTr2l

DIM KeyTr2(1 TO 48) AS shared INTEGER

init KeyTr2(), 48

RESTORE etrl

DIM etr(1 TO 48) AS shared INTEGER

init etr(), 48

RESTORE ptrl

DIM ptr(1 TO 32) AS shared INTEGER

init ptr(), 32

sboxinit s()

RESTORE rotsl

DIM rots(1 TO 16) AS shared INTEGER

init rots(), 16

DIM XR(1 TO 56) AS shared INTEGER

DIM EF(1 TO 64) AS shared INTEGER

DIM ikeyf(1 TO 64) AS shared INTEGER

DIM yf(1 TO 64) AS shared INTEGER

DIM ades(1 TO 64) AS shared INTEGER

DIM bdes(1 TO 64) AS shared INTEGER

Ïðîäîëæåíèå ëèñòèíãà 18.4

DIM xdes(1 TO 64) AS shared INTEGER

DIM XT(1 TO 64)   AS shared INTEGER

DIM P2(1 TO 64) AS shared INTEGER

main:

CLS

parm$ = ltrim$(rtrim$(COMMAND$))+" "

IF LEN(parm$) > 1 THEN

 cipherf$ = LTRIM$(RTRIM$(LEFT$(parm$, INSTR(parm$, " "))))

 PRINT "Èìÿ  çàøèôðîâàííîãî ôàéëà : "; cipherf$

ELSE

 INPUT "Èìÿ ðàñøèôðîâàííîãî ôàéëà : ", cipherf$

END IF

if len(cipherf$)=0 then

 print : print "ÑÁÎÉ: ââåäèòå èìÿ ôàéëà!"

 system

end if

OPEN cipherf$ FOR RANDOM AS 1

lof1& = LOF(1)

IF lof1& = 0 THEN

 CLOSE #1

 KILL cipherf$

 PRINT : PRINT "Ôàéë íå íàéäåí!";

 SYSTEM

ELSE

 CLOSE #1

 OPEN cipherf$ for binary access read as #1

END IF

PW$ = ""

LOCATE 6, 1

INPUT "                    Ïàðîëü : ", PW$

IF (LEN(PW$) < 8) THEN PW$ = PW$ + STRING$(8 - LEN(PW$), 0)

IF len(pw$) = 16 then

   LOCATE 6, 1: PRINT "                    Ïàðîëü : ";

   STRING$(16, 15); STRING$(10, " ")

Ïðîäîëæåíèå ëèñòèíãà 18.4

    PW$ = ucase$(PW$)       



    xtob PW$, P2()          

ELSE

   LOCATE 6, 1: PRINT "                     Ïàðîëü : ";

   STRING$(8, 15); STRING$(10, " ")

   PW$ = LEFT$(PW$, 8)

   stob PW$, P2()

END IF

       PRINT "            Ïðîâåðêà ïàðîëÿ : ";

transpose P2(), KeyTr1(), 56

get$ #1,24,cheader$

header$ = desalg$(LEFT$(cheader$, 8))

IF NOT (LEFT$(header$, 3) = "DES") THEN

 PRINT "Íåâåðåí!": PRINT : PRINT "Íåïðàâèëüíûé ïàðîëü èëè ";

       cipherf$; " íå ÿâëÿåòñÿ çàøèôðîâàííûì ôàéëîì!"

 SYSTEM

ELSE

 PRINT "Âåðåí!"

END IF

PRINT "       Ïðîâåðêà äëèíû ôàéëà :";

header$ = header$ + desalg$(MID$(cheader$, 9, 8))

header$ = header$ + desalg$(RIGHT$(cheader$, 8))

pl% = INSTR(header$, "#")

le$ = MID$(header$, pl% + 1, (11 - pl%))

lf& = VAL(le$)

ev& = lf& + 24

IF (ev& MOD 8) THEN ev& = ev& + 8 - (ev& MOD 8)

rescue% = 0

IF (ev& <> lof1&) THEN

 PRINT "Íåâåðíà!! (âîçìîæíà ïîòåðÿ äàííûõ)"

 PRINT "     Äëèíà èñõîäíîãî ôàéëà :"; lf&

 PRINT "    Äëèíà óêàçàííîãî ôàéëà :"; lof1&

 PRINT "   Äëèíà ôàéëà äîëæíà áûòü :"; ev&

 INPUT ; "Ïîïûòàòüñÿ âîññòàíîâèòü? (y/n) : ", q$:

         IF (INSTR(q$, "N") OR (INSTR(q$, "n"))) THEN SYSTEM

 rescue% = 4: PRINT

ELSE

 PRINT lf&; ", Âåðíà!"

END IF

Ïðîäîëæåíèå ëèñòèíãà 18.4

pl% = INSTR(12, header$, "#")

oldplainf$ = RIGHT$(header$, 24 - pl%)

PRINT "        Èìÿ èñõîäíîãî ôàéëà : "; oldplainf$;

OPEN oldplainf$ FOR RANDOM AS 2

IF INSTR(oldplainf$, ".") THEN

 PRINT " ([*.*] ";

ELSE

 PRINT " ([*] ";

END IF

IF LOF(2) > 0 THEN PRINT "óæå åñòü â êàòàëîãå";

PRINT ")"

CLOSE #2

plainf$ = ""

INPUT "        Èìÿ âûõîäíîãî ôàéëà : ", plainf$

IF plainf$ = "" THEN plainf$ = oldplainf$

plainf$ = RTRIM$(LTRIM$(plainf$))



IF INSTR(plainf$, "*.") THEN

 IF INSTR(oldplainf$, ".") THEN

  plainf$ = LEFT$(plainf$, INSTR(plainf$, "*.") - 1) + LEFT$(oldplainf$, INSTR(oldplainf$, ".")) + RIGHT$(plainf$, LEN(plainf$) - INSTR(plainf$, "*.") - 1)

 ELSE

  plainf$ = LEFT$(plainf$, INSTR(plainf$, "*.") - 1) + oldplainf$ + RIGHT$(plainf$, LEN(plainf$) - INSTR(plainf$, "*."))

 END IF

END IF

IF (RIGHT$(plainf$, 1) = "*") THEN

 IF plainf$ = "*" THEN

  plainf$ = oldplainf$

 ELSE

  IF (MID$(plainf$, LEN(plainf$) - 1, 1) = ".") THEN

   plainf$ = LEFT$(plainf$, INSTR(plainf$, ".") - 1) +

             RIGHT$(oldplainf$, LEN(oldplainf$) -

             INSTR(oldplainf$, ".") + 1)

  ELSE

   plainf$ = LEFT$(plainf$, LEN(plainf$) - 1) + oldplainf$

  END IF

 END IF

END IF

IF RIGHT$(plainf$, 1) = "\" THEN plainf$ = plainf$ + oldplainf$

Ïðîäîëæåíèå ëèñòèíãà 18.4

OPEN plainf$ FOR RANDOM AS 2

IF LOF(2) > 0 THEN

 CLOSE #2

 PRINT : PRINT "Óæå åñòü ôàéë ñ òàêèì èìåíåì!"

 SYSTEM

ELSE

 CLOSE #2

 OPEN plainf$ FOR BINARY AS 2

END IF

plaintekst$ = ""

blocks& = (LOF(1) \ 8) - 3

LOCATE rescue% + 11, 21: PRINT ; "Ðàñøèôðîâûâàíèå :   0 %";

bigblocks&=(blocks&-1) \ 32

large$=space$(256)

FOR m& = 1 TO bigblocks&

  outblock$=""

  get$ #1,256,large$

  for o%=1 to 256 step 8

   outblock$=outblock$+desalg$(mid$(large$,o%,8))

  next

  put$ #2,outblock$

  LOCATE rescue% + 11, 32: PRINT ; USING "###"; (m& / (bigblocks&+1)) * 100;

next

FOR n& = (bigblocks&*32)+1 TO blocks& - 1

 GET$ #1,8,ciphertekst$

 plaintekst$ = desalg$(ciphertekst$)

 PUT$ #2, plaintekst$

 LOCATE rescue% + 11, 32: PRINT ; USING "###"; (n& / blocks&) * 100;

NEXT n&

get$ #1,8,ciphertekst$

if len(ciphertekst$) > 0 then

   plaintekst$ = desalg$(ciphertekst$)

   IF rescue% THEN

     last$ = plaintekst$



   ELSE

Ïðîäîëæåíèå ëèñòèíãà 18.4

     last$ = LEFT$(plaintekst$, lf& + 32 - LOF(1))

   END IF

   IF LEN(last$) > 0 THEN

      PUT$ #2, last$

   END IF

end if

CLOSE

LOCATE 11 + rescue%, 32: PRINT "100 %  ãîòîâî": PRINT

IF rescue% THEN

 PRINT " Ïîïûòàéòåñü äðóãèì ñïîñîáîì ðàñøèôðîâàòü ýòîò ôàéë."

ELSE

 PRINT "Ðàñøèôðîâûâàíèå ïî àëãîðèòìó DES çàâåðøåíî."

END IF

SYSTEM

' Äàííûå è ôóíêöèè

InitialTrl:

DATA 58,50,42,34,26,18,10,02,60,52,44,36,28,20,12,04

DATA 62,54,46,38,30,22,14,06,64,56,48,40,32,24,16,08

DATA 57,49,41,33,25,17,09,01,59,51,43,35,27,19,11,03

DATA 61,53,45,37,29,21,13,05,63,55,47,39,31,23,15,07

FinalTrl:

DATA 40,08,48,16,56,24,64,32,39,07,47,15,55,23,63,31

DATA 38,06,46,14,54,22,62,30,37,05,45,13,53,21,61,29

DATA 36,04,44,12,52,20,60,28,35,03,43,11,51,19,59,27

DATA 34,02,42,10,50,18,58,26,33,01,41,09,49,17,57,25

swappyl:

DATA 33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48

DATA 49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64

DATA 01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16

DATA 17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32

KeyTr1l:

DATA 57,49,41,33,25,17,09,01,58,50,42,34,26,18,10,02

DATA 59,51,43,35,27,19,11,03,60,52,44,36

DATA 63,55,47,39,31,23,15,07,62,54,46,38,30,22,14,06

DATA 61,53,45,37,29,21,13,05,28,20,12,04

Ïðîäîëæåíèå ëèñòèíãà 18.4

KeyTr2l:

DATA 14,17,11,24,01,05,03,28,15,06,21,10,23,19,12,04

DATA 26,08,16,07,27,20,13,02,41,52,31,37,47,55,30,40

DATA 51,45,33,48,44,49,39,56,34,53,46,42,50,36,29,32

etrl:

DATA 32,01,02,03,04,05,04,05,06,07,08,09,08,09,10,11

DATA 12,13,12,13,14,15,16,17,16,17,18,19,20,21,20,21

DATA 22,23,24,25,24,25,26,27,28,29,28,29,30,31,32,01

ptrl:

DATA 16,07,20,21,29,12,28,17,01,15,23,26,05,18,31,10

DATA 02,08,24,14,32,27,03,09,19,13,30,06,22,11,04,25

sboxesl:

DATA 14,04,13,01,02,15,11,08,03,10,06,12,05,09,00,07

DATA 00,15,07,04,14,02,13,01,10,06,12,11,09,05,03,08

DATA 04,01,14,08,13,06,02,11,15,12,09,07,03,10,05,00

DATA 15,12,08,02,04,09,01,07,05,11,03,14,10,00,06,13



DATA 15,01,08,14,06,11,03,04,09,07,02,13,12,00,05,10

DATA 03,13,04,07,15,02,08,14,12,00,01,10,06,09,11,05

DATA 00,14,07,11,10,04,13,01,05,08,12,06,09,03,02,15

DATA 13,08,10,01,03,15,04,02,11,06,07,12,00,05,14,09

DATA 10,00,09,14,06,03,15,05,01,13,12,07,11,04,02,08

DATA 13,07,00,09,03,04,06,10,02,08,05,14,12,11,15,01

DATA 13,06,04,09,08,15,03,00,11,01,02,12,05,10,14,07

DATA 01,10,13,00,06,09,08,07,04,15,14,03,11,05,02,12

DATA 07,13,14,03,00,06,09,10,01,02,08,05,11,12,04,15

DATA 13,08,11,05,06,15,00,03,04,07,02,12,01,10,14,09

DATA 10,06,09,00,12,11,07,13,15,01,03,14,05,02,08,04

DATA 03,15,00,06,10,01,13,08,09,04,05,11,12,07,02,14

DATA 02,12,04,01,07,10,11,06,08,05,03,15,13,00,14,09

DATA 14,11,02,12,04,07,13,01,05,00,15,10,03,09,08,06

DATA 04,02,01,11,10,13,07,08,15,09,12,05,06,03,00,14

DATA 11,08,12,07,01,14,02,13,06,15,00,09,10,04,05,03

Ïðîäîëæåíèå ëèñòèíãà 18.4

DATA 12,01,10,15,09,02,06,08,00,13,03,04,14,07,05,11

DATA 10,15,04,02,07,12,09,05,06,01,13,14,00,11,03,08

DATA 09,14,15,05,02,08,12,03,07,00,04,10,01,13,11,06

DATA 04,03,02,12,09,05,15,10,11,14,01,07,06,00,08,13

DATA 04,11,02,14,15,00,08,13,03,12,09,07,05,10,06,01

DATA 13,00,11,07,04,09,01,10,14,03,05,12,02,15,08,06

DATA 01,04,11,13,12,03,07,14,10,15,06,08,00,05,09,02

DATA 06,11,13,08,01,04,10,07,09,05,00,15,14,02,03,12

DATA 13,02,08,04,06,15,11,01,10,09,03,14,05,00,12,07

DATA 01,15,13,08,10,03,07,04,12,05,06,11,00,14,09,02

DATA 07,11,04,01,09,12,14,02,00,06,10,13,15,03,05,08

DATA 02,01,14,07,04,10,08,13,15,12,09,00,03,05,06,11

rotsl:

DATA 1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1

SUB btos (mbits() AS INTEGER, a$)

a$ = ""

FOR i% = 1 TO 8

 w% = 0

 FOR j% = 1 TO 8

  w% = w% + ((mbits(((i% - 1) * 8) + j%)) * (2 ^ (8 - j%)))

 NEXT j%

 a$ = a$ + CHR$(w%)

NEXT i%

END SUB

FUNCTION desalg$ (a$)

 temp$ = "": stob a$, ades()

 transpose ades(), InitialTr(), 64      

 transpose ades(), swappy(), 64         

 FOR i% = 16 TO 1 STEP -1

  letbe bdes(), ades(), 64



  f i%, bdes(), xdes()                  

  FOR j% = 1 TO 32

   ades(j%) = (bdes(j% + 32) + xdes(j%)) MOD 2

  NEXT j%

  FOR j% = 33 TO 64

   ades(j%) = bdes(j% - 32)

Ïðîäîëæåíèå ëèñòèíãà 18.4

  NEXT j%

 NEXT i%

 transpose ades(), FinalTr(), 64        

 btos ades(), temp$                

 desalg$ = temp$

END FUNCTION

SUB f (i%, a() AS INTEGER, x() AS INTEGER)

h% = i%: letbe EF(), a(), 64

transpose EF(), etr(), 48                

letbe ikeyf(), P2(), 64

transpose ikeyf(), KeyTr2(), 48

FOR j% = 1 TO rots(h%)                   

 mrotate P2()                            

NEXT j%                                  

                                           FOR j% = 1 TO 48

 yf(j%) = (EF(j%) + ikeyf(j%)) MOD 2

NEXT j%

FOR k% = 1 TO 8

 k6% = 6 * k%: k4% = 4 * k%

 r% = (32 * yf(k6% - 5)) + (16 * yf(k6%)) + (8 * yf(k6% - 4)) + (4 * yf(k6% - 3)) + (2 * yf(k6% - 2)) + yf(k6% - 1) + 1

 x(k4% - 3) = (s(k%, r%) \ 8) MOD 2: x(k4% - 2) = (s(k%, r%) \ 4) MOD 2

 x(k4% - 1) = (s(k%, r%) \ 2) MOD 2: x(k4%) = s(k%, r%) MOD 2

NEXT k%

transpose x(), ptr(), 32

END SUB

SUB init (x() AS INTEGER, n%)

 FOR i% = 1 TO n%

  READ x(i%)

 NEXT i%

END SUB

SUB letbe (target() AS INTEGER, source() AS INTEGER, last%)

FOR il% = 1 TO last%

 target(il%) = source(il%)

NEXT il%

END SUB

Ïðîäîëæåíèå ëèñòèíãà 18.4

FUNCTION MYBIN$ (n%)

STS$ = ""

p% = n%

 FOR i% = 1 TO 8

  IF (p% MOD 2) THEN

   ST$ = "1" + ST$

  ELSE

   ST$ = "0" + ST$

  END IF

  p% = p% \ 2

 NEXT i%

 MYBIN$ = ST$

END FUNCTION

SUB mrotate (keyr() AS INTEGER)    

letbe XR(), keyr(), 56

FOR ir% = 56 TO 2 STEP -1

 XR(ir%) = XR(ir% - 1)

NEXT ir%

XR(1) = keyr(28): XR(29) = keyr(56)

letbe keyr(), XR(), 56

END SUB

SUB sboxinit (b() AS INTEGER)

RESTORE sboxesl

FOR i% = 1 TO 8

 FOR j% = 1 TO 64

  READ b(i%, j%)

 NEXT j%

NEXT i%

END SUB

SUB stob (a$, mbits() AS INTEGER)

FOR i% = 1 TO 8

 b$ = MYBIN$(ASC(MID$(a$, i%, 1)))

 FOR j% = 1 TO 8

  mbits(((i% - 1) * 8) + j%) = ASC(MID$(b$, j%, 1)) - 48



 NEXT j%

NEXT i%

END SUB

Ïðîäîëæåíèå ëèñòèíãà 18.4

SUB transpose (datax() AS INTEGER, T() AS INTEGER, nt%)

letbe XT(), datax(), 64

FOR i% = 1 TO nt%

 datax(i%) = XT(T(i%))

NEXT i%

END SUB

SUB xtob (a$, mbits() AS INTEGER)

LOCAL X$,NIBBLE$

FOR i% = 1 to 16

    X$ = MID$(a$,i%,1)

    SELECT CASE X$

    CASE "0"

     NIBBLE$ = "0000"

    CASE "1"

     NIBBLE$ = "0001"

    CASE "2"

     NIBBLE$ = "0010"

    CASE "3"

     NIBBLE$ = "0011"

    CASE "4"

     NIBBLE$ = "0100"

    CASE "5"

     NIBBLE$ = "0101"

    CASE "6"

     NIBBLE$ = "0110"

    CASE "7"

     NIBBLE$ = "0111"

    CASE "8"

     NIBBLE$ = "1000"

    CASE "9"

     NIBBLE$ = "1001"

    CASE "A"

     NIBBLE$ = "1010"

    CASE "B"

     NIBBLE$ = "1011"

    CASE "C"

     NIBBLE$ = "1100"

    CASE "D"

     NIBBLE$ = "1101"

    CASE "E"

Îêîí÷àíèå ëèñòèíãà 18.4

     NIBBLE$ = "1110"

    CASE "F"

     NIBBLE$ = "1111"

    CASE ELSE

     Print "Íå ÿâëÿåòñÿ 16-ðè÷íûì çíà÷åíèåì!"

     SYSTEM

    END SELECT

    FOR j% = 1 to 4

        mbits(((i% - 1) * 4) + j%) = ASC(MID$(NIBBLE$, j%, 1)) - 48

    NEXT j%

  NEXT i%

END SUB


Ñîäåðæàíèå ðàçäåëà