-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathICRYPT
More file actions
executable file
·186 lines (185 loc) · 6.33 KB
/
ICRYPT
File metadata and controls
executable file
·186 lines (185 loc) · 6.33 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
PROGRAM ICRYPT
* Simple utility for strong encryption/decryption of an item.
* ********************************************************** *
* ********************************************************** *
* Use the IDEA encryption algorithm to encrypt or decrypt *
* an item in a file. *
* This program is really only intended to demontrate how the *
* encryption functions and the base64 encoding utility might *
* be used. *
* *
* Usage: ICRYPT filename itemname - encrypt *
* ICRYPT -R filename itemname - decrypt *
* *
* In both cases, you will be prompted for a pass-phrase. *
* *
* Note: For UniData, if you have trouble with storing the *
* base64 string in UniData DIR files, consider that it may *
* be translating the linefeeds into attributes marks 'for *
* you'. This can stuff things up. Look at the setting of *
* UDT.OPTIONS 95 - this is called: 'U_NO_TRANSLATE_NEWLINE'. *
* Set as appropriate. Also note that there is no real reason *
* to store the UniVerse and UniData encrypted data using *
* base64 strings, but as this is a contrived example program *
* to demonstrate various functions .... <g> (dmm) *
* ********************************************************** *
* ********************************************************** *
*
EQU SPCE TO CHAR(32), TRUE TO 1, FALSE TO 0
*
EQU ENCRYPT TO 0 ;* udt/uv
EQU DECRYPT TO 1 ;* udt/uv
EQU USE.BASE64 TO 1 ;* udt/uv
*r83 EQU ENCRYPT TO 2 ;* Encrypt to hex string!
*r83 EQU DECRYPT TO 3 ;* Decrypt from hex string!
*r83 EQU USE.BASE64 TO 0 ;* Data stored in hex, no need for base64!
*
*r83 CMDLINE = ERROR()
CMDLINE = @SENTENCE ;* udt/uv
*
*r83 DIM IK(52)
*
Z = INDEX(CMDLINE, SPCE, 1)
IF Z > 0 THEN
LOOP Z = Z + 1 WHILE CMDLINE[Z, 1] = SPCE DO REPEAT
CMDLINE = CMDLINE[Z, LEN(CMDLINE)]
END ELSE CMDLINE = ''
IF CMDLINE = '' THEN
CRT
CRT " Usage : ICRYPT [ -R ] [ DICT | DATA ] filename itemname"
CRT
CRT " -R : If present, the -R option indicates a decryption"
CRT " operation is required. Absence indicates encryption."
CRT
CRT " filename: Name of target file. May be prefixed by DICT modifier."
CRT
CRT " itemname: Name of item id within file 'filename' to be used in"
CRT " then encryption/decryption operation."
CRT
STOP
END
*
* Look for option flag!
*
CRYPT.MODE = ENCRYPT
IF CMDLINE[1, 1] = '-' THEN
CH = OCONV(CMDLINE[2, 1], 'MCU')
IF CH = 'R' THEN
CRYPT.MODE = DECRYPT
END ELSE
CRT 'Invalid option character in command line!'
STOP
END
Z = 2
LOOP Z = Z + 1 WHILE CMDLINE[Z, 1] = SPCE DO REPEAT
CMDLINE = CMDLINE[Z, LEN(CMDLINE)]
END
*
* Extract and validate file name!
*
DFLG = ''
FNAME = FIELD(CMDLINE, SPCE, 1)
IF FNAME = 'DICT' OR FNAME = 'DATA' THEN
IF FNAME = 'DICT' THEN DFLG = FNAME
Z = COL2() ;* Find next non-space!
LOOP Z = Z + 1 WHILE CMDLINE[Z, 1] = SPCE DO REPEAT
CMDLINE = CMDLINE[Z, LEN(CMDLINE)]
FNAME = FIELD(CMDLINE, SPCE, 1)
END
OPEN DFLG, FNAME TO F.FILE ELSE
IF DFLG > '' THEN FNAME = DFLG:SPCE:FNAME
CRT "Cannot open '":FNAME:"'"
STOP
END
*
Z = COL2() ;* Find next non-space!
LOOP Z = Z + 1 WHILE CMDLINE[Z, 1] = SPCE DO REPEAT
CMDLINE = CMDLINE[Z, LEN(CMDLINE)]
*
* Extract item name, or check for active list!
*
IF CMDLINE = '' THEN
IF NOT(SYSTEM(11)) THEN
CRT 'Item name ':
INPUT INAME
INAME = TRIM(INAME)
IF INAME = '' THEN STOP
END ELSE
READNEXT INAME ELSE STOP
END
END ELSE
INAME = TRIM(CMDLINE)
END
*
* Ok, lets do it!
*
*udt NOCONVERT ON ;* UniData only!!
*
READ REC FROM F.FILE, INAME ELSE
IF DFLG > '' THEN FNAME = DFLG:SPCE:FNAME
CRT "'":INAME:"' not found on file ":FNAME
STOP
END
*
* Prompt for destination item
*
CRT 'Destination item ':
INPUT DNAME
DNAME = TRIM(DNAME)
IF DNAME = '' THEN STOP
*
* Get password/phrase from user!
*
CRT "Pass phrase ":
INPUT PASSWD
IF PASSWD = "" THEN STOP
*
IF CRYPT.MODE = DECRYPT AND USE.BASE64 THEN
* If decrypting, convert from Base64 format first!
CRT 'Converting stored item from Base64 format ...'
CALL BASE64(1, REC, OUTBUF, ERR)
IF ERR THEN
CRT 'Error converting from Base64 format!'
STOP
END
REC = OUTBUF
END
*
IF CRYPT.MODE = DECRYPT THEN Z = 'De' ELSE Z = 'En'
CRT Z:'crypting data ...'
CALL CIPHER(CRYPT.MODE, PASSWD, REC, OUTBUF, ERR) ;* udt/uv
*r83 CALL CIPHER(CRYPT.MODE, PASSWD, REC, OUTBUF, ERR, MAT IK)
*
IF ERR THEN
IF CRYPT.MODE = DECRYPT AND ERR = 2 THEN
CRT 'Wrong pass-phrase specified for decryption!'
STOP
END
CRT 'Error code ':ERR:' - aborted!'
STOP
END
IF CRYPT.MODE = ENCRYPT AND USE.BASE64 THEN
* If encrypting, convert to Base64 before writing to disk!
CRT 'Converting encrypted data to Base64 format ...'
CALL BASE64(0, OUTBUF, REC, ERR)
IF ERR THEN
CRT 'Error converting encrypyted item to Base64 format!'
STOP
END
OUTBUF = REC
END
*
READ TEMP FROM F.FILE, DNAME THEN
CRT 'Overwrite? (Y/N) ':
INPUT REPLY
REPLY = OCONV(TRIM(REPLY), 'MCU')
OFLAG = (REPLY = 'Y')
END ELSE OFLAG = TRUE
IF OFLAG THEN
WRITE OUTBUF ON F.FILE, DNAME
END
*
*udt NOCONVERT OFF ;* UniData only!!
STOP
*
END