/[eiffelstudio]/trunk/Src/library/encryption/des_encoder.e
ViewVC logotype

Contents of /trunk/Src/library/encryption/des_encoder.e

Parent Directory Parent Directory | Revision Log Revision Log


Revision 26532 - (show annotations)
Sat Jul 14 12:30:44 2001 UTC (18 years, 7 months ago) by manus
File size: 4454 byte(s)
Added copyright notice

1 indexing
2 description: "DES encryption and decryption";
3 product: EiffelLicense;
4 keywords: Des, Encryption, Decryption;
5 date: "$Date$";
6 revision: "$Revision$"
7
8 class
9 DES_ENCODER
10
11 inherit
12 EXCEPTIONS
13
14 creation
15 make, make_with_key
16
17 feature -- Initialization
18
19 make is
20 -- Prepare object for encryption
21 do
22 des_init (1)
23 set_key (secret_key);
24 end
25
26 make_with_key (key: STRING) is
27 -- Prepare object for encryption with `key'.
28 require
29 key_not_void: key /= Void
30 valid_key_count: key.count = 128
31 do
32 des_init (1)
33 set_key (key)
34 end
35
36 feature -- Disposal
37
38 terminate is
39 -- Dispose the allocated structures.
40 do
41 desdone
42 end
43
44 feature -- Encryption
45
46 encrypt (s: STRING): STRING is
47 -- Padding done if length not multiple of 8
48 require
49 string_not_void: s /= Void;
50 local
51 c_string: ANY;
52 pad: STRING;
53 do
54 Result := clone (s);
55 if (s.count \\ 8) /= 0 then
56 !! pad.make (8 - (s.count \\ 8));
57 pad.fill_character ('%U');
58 Result.append (pad);
59 end;
60 c_string := Result.to_c;
61 c_encrypt ($c_string, Result.count);
62 end;
63
64 decrypt (s: STRING): STRING is
65 -- Padding done if length not multiple of 8
66 require
67 string_not_void: s /= Void;
68 local
69 c_string: ANY;
70 pad: STRING;
71 do
72 Result := clone (s);
73 if (s.count \\ 8) /= 0 then
74 !! pad.make (8 - (s.count \\ 8));
75 pad.fill_character ('%U');
76 Result.append (pad);
77 end;
78 c_string := Result.to_c;
79 c_decrypt ($c_string, Result.count);
80 Result.prune_all ('%U');
81 Result.adapt_size;
82 end;
83
84 feature -- Status
85
86 des_init (mode: INTEGER) is
87 -- mode 0: standard Data Encryption Algorithm
88 -- mode 1: DEA without initial and final permutation for speed
89 -- mode 2: DEA without permutations and with 128-byte key
90 -- In modes 0 and 1, 8 key bytes are expected, with the low order
91 -- bit of each key byte ignored (parity is not checked). This
92 -- gives a 56-bit key.
93 -- In mode 2, 128 key bytes are expected; the high order 2 bits
94 -- of each byte are ignored, giving a 768 bit key.
95 require
96 mode_low_enough: mode <= 2;
97 mode_high_enough: mode >= 0;
98 do
99 if desinit (mode) < 0 then
100 raise ("Unable to initialize DES encryption.")
101 end;
102 end;
103
104 set_key (key: STRING) is
105 require
106 key_not_void: key /= Void;
107 local
108 c_string: ANY;
109 do
110 c_string := key.to_c;
111 setkey ($c_string);
112 end;
113
114 feature {NONE}
115
116 secret_key: STRING is
117 once
118 Result := "%/23/%/05/%/37/%/16/%/01/%/03/%/07/%/39/%/63/%/25/%
119 %%/37/%/56/%/23/%/19/%/54/%/63/%/21/%/53/%/32/%/40/%
120 %%/30/%/06/%/12/%/21/%/53/%/35/%/59/%/19/%/57/%/03/%
121 %%/44/%/10/%/12/%/01/%/25/%/35/%/16/%/19/%/21/%/02/%
122 %%/37/%/21/%/16/%/01/%/98/%/55/%/44/%/09/%/37/%/49/%
123 %%/11/%/62/%/09/%/46/%/32/%/45/%/15/%/53/%/46/%/25/%
124 %%/07/%/05/%/54/%/38/%/61/%/48/%/76/%/03/%/59/%/38/%
125 %%/35/%/24/%/38/%/39/%/28/%/02/%/53/%/10/%/40/%/19/%
126 %%/39/%/51/%/36/%/02/%/57/%/62/%/39/%/24/%/82/%/56/%
127 %%/09/%/01/%/31/%/19/%/20/%/49/%/25/%/29/%/49/%/09/%
128 %%/36/%/61/%/10/%/03/%/45/%/50/%/42/%/10/%/31/%/39/%
129 %%/50/%/02/%/30/%/17/%/17/%/49/%/63/%/10/%/58/%/38/%
130 %%/14/%/21/%/14/%/18/%/13/%/04/%/11/%/57/"
131 end
132
133 feature {NONE} -- Externals
134
135 c_decrypt (data: POINTER; size: INTEGER) is
136 external
137 "C | %"desop.h%""
138 alias
139 "des_decrypt"
140 end;
141
142 c_encrypt (data: POINTER; size: INTEGER) is
143 external
144 "C | %"desop.h%""
145 alias
146 "des_encrypt"
147 end;
148
149 desinit (mode: INTEGER): INTEGER is
150 external
151 "C | %"des.h%""
152 alias
153 "ise_desinit"
154 end;
155
156 desdone is
157 external
158 "C | %"des.h%""
159 alias
160 "ise_desdone"
161 end;
162
163 setkey (key: POINTER) is
164 external
165 "C | %"des.h%""
166 alias
167 "ise_setkey"
168 end;
169
170 end -- class DES_ENCODER
171
172
173 --|----------------------------------------------------------------
174 --| EiffelEncryption: library of reusable components for ISE Eiffel.
175 --| Copyright (C) 1986-2001 Interactive Software Engineering Inc.
176 --| All rights reserved. Duplication and distribution prohibited.
177 --| May be used only with ISE Eiffel, under terms of user license.
178 --| Contact ISE for any other use.
179 --|
180 --| Interactive Software Engineering Inc.
181 --| ISE Building
182 --| 360 Storke Road, Goleta, CA 93117 USA
183 --| Telephone 805-685-1006, Fax 805-685-6869
184 --| Electronic mail <info@eiffel.com>
185 --| Customer support: http://support.eiffel.com>
186 --| For latest info see award-winning pages: http://www.eiffel.com
187 --|----------------------------------------------------------------
188

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.23