ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 10.06.2021
Просмотров: 3555
Скачиваний: 3
406
Глава
18.
Криптографическая
защита
Продолжение
листинга
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
Стандарт
шифрования
данных
DES
407
Продолжение
листинга
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
408
Глава
18.
Криптографическая
защита
Продолжение
листинга
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
Стандарт
шифрования
данных
DES
409
Продолжение
листинга
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"
410
Глава
18.
Криптографическая
защита
Окончание
листинга
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