RC4 Decrypt Macro

ATTENTION : Ce contenu est DÉSACTIVÉ. Il est invisible pour les visiteurs.
Niveau de difficulté
Débutant
Publié le :
This macro, `sas©_sec_rc4decrypt`, takes a file path and a password as input. It first stores the password in a temporary file and then computes its SHA256 hash. It prepares the RC4 key array in hexadecimal format. The macro then proceeds to decrypt the specified input file using the RC4 algorithm. It includes checks for the existence of the input encrypted file and for name conflicts with the output file. The decryption process involves initializing the S-box and key, and then iteratively XORing the input bytes with the RC4 keystream. The decrypted content is written to a new file with the same name as the input file.
Analyse des données

Type : EXTERNE


The macro processes an input file provided as an argument and uses a password for decryption. The input file is expected to be an RC4 encrypted file.

1 Bloc de code
MACRO SETUP
Explication :
Initializes macro variables, creates a temporary file to hold the password, computes the SHA256 hash of the password using the assumed %sdk_secure_hash_sha256 macro, and prepares the RC4 key array in hexadecimal format.
Copié !
1/*************************************************
2* Copyright(c) 2015 coco, All Rights Reserved.
3* @html/SAS Help Center_ isAuthorized Action.html Daniel YU
4* @code_sas_json_prod_multi/dsc_cdm_version_update_de.json 1.0
5*
6*************************************************/
7 
8%macro sas_sec_rc4decrypt(file, password);
9 filename _rc4_ temp;
10 
11 DATA _null_;
12 LENGTH pwd $256;
13 file _rc4_;
14 pwd=compress("&password.");
15 put pwd;
16 RUN;
17 
18 %local password pwdarray;
19 %sdk_secure_hash_sha256(%sysfunc(pathname(_rc4_)), password);
20 filename _rc4_ clear;
21 
22 /* %put &password.;*/
23 
24 %let pwdarray=%str();
25 
26 %DO i=1 %to 64 %BY 2;
27 %let pwdarray=&pwdarray. 0%substr(&password., &i., 2)x;
28 %END;
29 %local num;
30 %let num=16386;
31 
2 Bloc de code
DATA _NULL_
Explication :
Performs file existence checks. Initializes the RC4 S-box and key array. Implements the RC4 key schedule and decryption loop, reading input byte by byte from the encrypted file, generating keystream bytes, XORing them with input bytes, and writing the decrypted bytes to the output file. It also closes the macro definition.
Copié !
1 DATA _null_;
2 IF filename('_rc4', "&file..rc4") THEN
3 DO;
4 put 'error: 找不到加密文件!';
5 return;
6 END;
7 
8 IF fileexist("&file.") THEN
9 DO;
10 put 'error: 有重名文件存在,解码过程中止!';
11 return;
12 END;
13 ELSE
14 rc=filename('rc4_', "&file.");
15 array s[256];
16 array key[32] (&pwdarray.);
17 
18 DO i=0 to 255;
19 s[i+1]=i;
20 END;
21 j=0;
22 
23 DO i=0 to 255;
24 j=mod(j+s[i+1]+key[mod(i, 32)+1], 256);
25 temp=s[i+1];
26 s[i+1]=s[j+1];
27 s[j+1]=temp;
28 END;
29 
30 /* put s[*];*/
31 /* put key[*] hex2.;*/
32 LENGTH byte $&num.;
33 iid=fopen('_rc4', 'i', &num., 'b');
34 oid=fopen('rc4_', 'o', &num., 'b');
35 n=0;
36 i=0;
37 j=0;
38 
39 DO while(fread(iid)=0);
40 LENGTH=frlen(iid);
41 n+LENGTH;
42 rc=fget(iid, byte, LENGTH);
43 k=0;
44 
45 DO while(k<LENGTH);
46 k+1;
47 i=mod(i+1, 256);
48 j=mod(j+s[i+1], 256);
49 temp=s[i+1];
50 s[i+1]=s[j+1];
51 s[j+1]=temp;
52 substr(byte, k, 1)=byte(bxor(rank(char(byte, k)),
53 s[mod(s[i+1]+s[j+1], 256)+1]));
54 END;
55 
56 IF LENGTH<&num. THEN
57 rc=fput(oid, substr(byte, 1, LENGTH));
58 ELSE
59 rc=fput(oid, byte);
60 rc=fwrite(oid);
61 END;
62 rc=fclose(iid);
63 rc=filename('_rc4');
64 rc=fclose(oid);
65 rc=filename('rc4_');
66 RUN;
67 
68%mend;
69 
Ce matériel est fourni "tel quel" par We Are Cas. Il n'y a aucune garantie, expresse ou implicite, quant à la qualité marchande ou à l'adéquation à un usage particulier concernant le matériel ou le code contenu dans les présentes. We Are Cas n'est pas responsable des erreurs dans ce matériel tel qu'il existe maintenant ou existera, et We Are Cas ne fournit pas de support technique pour celui-ci.
Informations de Copyright : Copyright(c) 2015 coco, All Rights Reserved.