-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdes.dem
77 lines (77 loc) · 1.79 KB
/
des.dem
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
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
PROGRAM D7R13
C Driver for routine DES
DIMENSION IN(64),KEY(64),IOUT(64),ICMP(64)
INTEGER HEX2IN
CHARACTER HIN(17)*1,HKEY(17)*1,HOUT(17)*1,HCMP(17)*1,
* VERDCT*8,TEXT*60,TEXT2*6,IN2HEX*1
OPEN(5,FILE='DESTST.DAT',STATUS='OLD')
READ (5,'(A)') TEXT
WRITE(*,'(/1X,A)') TEXT
5 READ (5,'(A)') TEXT
WRITE(*,'(/1X,A,/)') TEXT
READ (5,*,ERR=99) NCIPHR
READ (5,'(A)') TEXT2
IF (TEXT2.EQ.'encode') IDIREC=0
IF (TEXT2.EQ.'decode') IDIREC=1
10 WRITE(*,'(1X,T9,A,T23,A,T38,A,T56,A)')
* 'Key','Plaintext','Expected Cipher','Actual Cipher'
MM=MIN(NCIPHR,16)
NCIPHR=NCIPHR-16
DO 16 M=1,MM
READ(5,'(51A1)') (HKEY(K),K=1,17),(HIN(K),K=1,17),
* (HCMP(K),K=1,17)
DO 12 I=1,16
J=I+1
IDUM=HEX2IN(HIN(J))
JDUM=HEX2IN(HKEY(J))
DO 11 K=1,4
L=4*I+1-K
IN(L)=MOD(IDUM,2)
IDUM=IDUM/2
KEY(L)=MOD(JDUM,2)
JDUM=JDUM/2
11 CONTINUE
12 CONTINUE
NEWKEY=1
CALL DES(IN,KEY,NEWKEY,IDIREC,IOUT)
HOUT(1)=' '
DO 14 I=1,16
JDUM=0
DO 13 J=1,4
JDUM=JDUM+(2**(4-J))*IOUT(4*(I-1)+J)
13 CONTINUE
HOUT(I+1)=IN2HEX(JDUM)
14 CONTINUE
VERDCT=' o.k.'
DO 15 I=1,17
IF (HCMP(I).NE.HOUT(I)) VERDCT=' wrong'
15 CONTINUE
WRITE(*,'(1X,68A1,A)') (HKEY(K),K=1,17),(HIN(K),K=1,17),
* (HCMP(K),K=1,17),(HOUT(K),K=1,17),VERDCT
16 CONTINUE
WRITE(*,'(/1X,A)') 'press RETURN to continue...'
READ(*,*)
IF (NCIPHR.LE.0) GOTO 5
GOTO 10
99 CLOSE(5)
END
INTEGER FUNCTION HEX2IN(CH)
C Converts character representing hexadecimal data to its integer
C value in a machine-independent way
CHARACTER CH*1
IF(CH.GE.'0'.AND.CH.LE.'9')THEN
HEX2IN=ICHAR(CH)-ICHAR('0')
ELSE
HEX2IN=ICHAR(CH)-ICHAR('A')+10
ENDIF
RETURN
END
CHARACTER*1 FUNCTION IN2HEX(I)
C Inverse of HEX2IN
IF(I.LE.9)THEN
IN2HEX=CHAR(I+ICHAR('0'))
ELSE
IN2HEX=CHAR(I-10+ICHAR('A'))
ENDIF
RETURN
END