ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 10.06.2021
Просмотров: 3559
Скачиваний: 3
Стандарт
шифрования
данных
DES
401
Продолжение
листинга
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$, "\")
402
Глава
18.
Криптографическая
защита
Продолжение
листинга
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
Стандарт
шифрования
данных
DES
403
Продолжение
листинга
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))+d
esalg$(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$
404
Глава
18.
Криптографическая
защита
Продолжение
листинга
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
Стандарт
шифрования
данных
DES
405
Продолжение
листинга
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