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 | /************************************************* |
| 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 |
| 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 |