forked from larshp/abapNTLM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzcl_arc4.clas.abap
284 lines (204 loc) · 5.91 KB
/
zcl_arc4.clas.abap
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
class ZCL_ARC4 definition
public
final
create public .
public section.
*"* public components of class ZCL_ARC4
*"* do not include other source files here!!!
class-methods DECRYPT
importing
!IV_KEY type STRING
!IV_CIPHERTEXT type XSTRING
returning
value(RV_PLAINTEXT) type STRING
raising
CX_STATIC_CHECK .
class-methods DECRYPT_HEX
importing
!IV_KEY type XSTRING
!IV_CIPHERTEXT type XSTRING
returning
value(RV_PLAINTEXT) type XSTRING
raising
CX_STATIC_CHECK .
class-methods ENCRYPT
importing
!IV_KEY type STRING
!IV_PLAINTEXT type STRING
returning
value(RV_CIPHERTEXT) type XSTRING
raising
CX_STATIC_CHECK .
class-methods ENCRYPT_HEX
importing
!IV_KEY type XSTRING
!IV_PLAINTEXT type XSTRING
returning
value(RV_CIPHERTEXT) type XSTRING
raising
CX_STATIC_CHECK .
protected section.
*"* protected components of class ZCL_ARC4
*"* do not include other source files here!!!
types:
ty_s TYPE x LENGTH 256 .
class-methods KEYSTREAM
importing
!IV_KEY type XSTRING
!IV_LENGTH type I
returning
value(RV_KEYSTREAM) type XSTRING
raising
CX_STATIC_CHECK .
class-methods KSA
importing
!IV_XKEY type XSTRING
returning
value(RV_S) type TY_S .
class-methods PRGA
importing
!IV_S type TY_S
!IV_LENGTH type I
returning
value(RV_K) type XSTRING .
class-methods TO_STRING
importing
!IV_XSTRING type XSTRING
returning
value(RV_STRING) type STRING
raising
CX_STATIC_CHECK .
class-methods TO_XSTRING
importing
!IV_STRING type STRING
returning
value(RV_XSTRING) type XSTRING
raising
CX_STATIC_CHECK .
class-methods XOR
importing
!IV_VAL1 type XSTRING
!IV_VAL2 type XSTRING
returning
value(RV_RES) type XSTRING .
private section.
*"* private components of class ZCL_ARC4
*"* do not include other source files here!!!
ENDCLASS.
CLASS ZCL_ARC4 IMPLEMENTATION.
METHOD decrypt.
DATA: lv_xstr TYPE xstring.
lv_xstr = decrypt_hex(
iv_key = to_xstring( iv_key )
iv_ciphertext = iv_ciphertext ).
rv_plaintext = to_string( lv_xstr ).
ENDMETHOD.
METHOD decrypt_hex.
rv_plaintext = encrypt_hex(
iv_key = iv_key
iv_plaintext = iv_ciphertext ).
ENDMETHOD.
METHOD encrypt.
* The MIT License (MIT)
*
* Copyright (c) 2015 Lars Hvam
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
rv_ciphertext = encrypt_hex(
iv_key = to_xstring( iv_key )
iv_plaintext = to_xstring( iv_plaintext ) ).
ENDMETHOD.
METHOD encrypt_hex.
DATA: lv_k TYPE xstring.
lv_k = keystream(
iv_key = iv_key
iv_length = xstrlen( iv_plaintext ) ).
rv_ciphertext = xor(
iv_val1 = lv_k
iv_val2 = iv_plaintext ).
ENDMETHOD.
METHOD keystream.
DATA: lv_s TYPE ty_s.
lv_s = ksa( iv_key ).
rv_keystream = prga( iv_s = lv_s
iv_length = iv_length ).
ENDMETHOD.
METHOD ksa.
DATA: lv_offset TYPE i,
lv_j TYPE i,
lv_i TYPE i,
lv_x TYPE x.
DO 256 TIMES.
lv_offset = sy-index - 1.
rv_s+lv_offset(1) = lv_offset.
ENDDO.
WHILE lv_i < 256.
lv_offset = lv_i MOD xstrlen( iv_xkey ).
lv_j = ( lv_j + rv_s+lv_i(1) + iv_xkey+lv_offset(1) ) MOD 256.
lv_x = rv_s+lv_i(1).
rv_s+lv_i(1) = rv_s+lv_j(1).
rv_s+lv_j(1) = lv_x.
lv_i = lv_i + 1.
ENDWHILE.
ENDMETHOD.
METHOD prga.
DATA: lv_x TYPE x,
lv_j TYPE i,
lv_i TYPE i,
lv_s LIKE iv_s,
lv_offset TYPE i.
lv_s = iv_s.
DO iv_length TIMES.
lv_i = ( lv_i + 1 ) MOD 256.
lv_j = ( lv_j + lv_s+lv_i(1) ) MOD 256.
lv_x = lv_s+lv_i(1).
lv_s+lv_i(1) = lv_s+lv_j(1).
lv_s+lv_j(1) = lv_x.
lv_offset = ( lv_s+lv_i(1) + lv_s+lv_j(1) ) MOD 256.
lv_x = lv_s+lv_offset(1).
CONCATENATE rv_k lv_x INTO rv_k IN BYTE MODE.
ENDDO.
ENDMETHOD.
METHOD to_string.
DATA: lv_len TYPE i,
lo_obj TYPE REF TO cl_abap_conv_in_ce.
lo_obj = cl_abap_conv_in_ce=>create(
input = iv_xstring
encoding = 'UTF-8' ).
lv_len = xstrlen( iv_xstring ).
lo_obj->read( EXPORTING n = lv_len
IMPORTING data = rv_string ).
ENDMETHOD.
METHOD to_xstring.
DATA: lo_obj TYPE REF TO cl_abap_conv_out_ce.
lo_obj = cl_abap_conv_out_ce=>create( encoding = 'UTF-8' ).
lo_obj->convert( EXPORTING data = iv_string
IMPORTING buffer = rv_xstring ).
ENDMETHOD.
METHOD xor.
DATA: lv_x TYPE x,
lv_offset TYPE i.
ASSERT xstrlen( iv_val1 ) = xstrlen( iv_val2 ).
DO xstrlen( iv_val1 ) TIMES.
lv_offset = sy-index - 1.
lv_x = iv_val1+lv_offset(1) BIT-XOR iv_val2+lv_offset(1).
CONCATENATE rv_res lv_x INTO rv_res IN BYTE MODE.
ENDDO.
ENDMETHOD.
ENDCLASS.