Advertisement
Naught

ALiVE_fnc_crypt

May 20th, 2014
2,427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.91 KB | None | 0 0
  1. /*
  2.     Function: ALiVE_fnc_crypt
  3.     Author(s): Naught
  4.     Version: 1.0
  5.     Description:
  6.         Encrypts or decrypts a string with a specified encryption key.
  7.     Parameters:
  8.         0 - Decrypt (0) or encrypt (1) [number]
  9.         1 - Encryption method name [string]
  10.         2 - Encrypted or plain data [string]
  11.         3 - Encryption key [string]
  12.     Returns:
  13.         Encrypted or decrypted data or nothing on failure [string:nil]
  14.     Note(s):
  15.         1. Current encryption method names:
  16.             - "rc4" // Rivest Cipher 4 Stream Encryption Algorithm
  17.     License:
  18.         Copyright (c) 2014 Dylan Plecki. All Right Reserved.
  19.         Licensed under Creative Commons (CC BY-NC 4.0).
  20.         http://creativecommons.org/licenses/by-nc/4.0/
  21. */
  22.  
  23. // Settings
  24. // #define DEBUG_MODE_FULL
  25.  
  26. // Constants
  27. #define MAX_CHAR_SIZE 8 // bits
  28. #define CHAR_ZERO_REP 256
  29.  
  30. private ["_flow", "_method", "_key"];
  31. _flow = _this select 0;
  32. _method = _this select 1;
  33. _key = _this select 3;
  34.  
  35. #ifdef DEBUG_MODE_FULL
  36.     private ["_startTime"];
  37.     _startTime = diag_tickTime;
  38. #endif
  39.  
  40. private ["_fnc_intToBin"];
  41. _fnc_intToBin = {
  42.     private ["_int", "_bin", "_pwr", "_bool"];
  43.     _int = _this select 0;
  44.     _bin = if ((count _this) > 1) then {_this select 1} else {[]};
  45.    
  46.     for "_i" from (MAX_CHAR_SIZE - 1) to 0 step (-1) do
  47.     {
  48.         _pwr = 2^(_i);
  49.         _bool = _pwr <= _int;
  50.        
  51.         _bin set [(count _bin), _bool];
  52.        
  53.         if (_bool) then {_int = _int - _pwr};
  54.     };
  55.    
  56.     _bin
  57. };
  58.  
  59. private ["_bin"];
  60. _bin = [];
  61.  
  62. // Convert string to UTF-8 binary
  63. { // count (faster than forEach)
  64.     [(if (_x == CHAR_ZERO_REP) then {0} else {_x}), _bin] call _fnc_intToBin;
  65.     false;
  66. } count toArray(_this select 2);
  67.  
  68. #ifdef DEBUG_MODE_FULL
  69.     diag_log text format["[%1] ALiVE_fnc_crypt <info>: Trace: Line #%2. Benchmark: %3 sec.", round(diag_tickTime), __LINE__, (diag_tickTime - _startTime)];
  70. #endif
  71.  
  72. // Encrypt & decrypt methods
  73. switch (_method) do
  74. {
  75.     case "rc4":
  76.     {
  77.         _key = toArray(_key);
  78.        
  79.         private ["_keyLen", "_state", "_temp", "_j"];
  80.         _keyLen = count _key;
  81.         _state = [];
  82.         _temp = 0;
  83.         _j = 0;
  84.        
  85.         // Key-Scheduling Algorithm
  86.         for "_i" from 0 to 255 do {_state set [_i,_i]};
  87.         for "_i" from 0 to 255 do
  88.         {
  89.             _temp = _state select _i;
  90.            
  91.             _j = (_j + _temp + (_key select (_i mod _keyLen))) mod 256;
  92.            
  93.             _state set [_i, (_state select _j)];
  94.             _state set [_j, _temp];
  95.         };
  96.        
  97.         #ifdef DEBUG_MODE_FULL
  98.             diag_log text format["[%1] ALiVE_fnc_crypt <info>: Trace: Line #%2. Benchmark: %3 sec.", round(diag_tickTime), __LINE__, (diag_tickTime - _startTime)];
  99.         #endif
  100.        
  101.         private ["_temp1", "_temp2", "_rand", "_i", "_mod", "_rbit"];
  102.         _temp1 = 0;
  103.         _temp2 = 0;
  104.         _rand = [];
  105.         _i = 0;
  106.         _j = 0;
  107.        
  108.         // Pseudo-Random Generation Algorithm
  109.         { // forEach
  110.             _mod = _forEachIndex % MAX_CHAR_SIZE;
  111.            
  112.             if (_mod == 0) then
  113.             {
  114.                 _i = (_i + 1) mod 256;
  115.                 _j = (_j + (_state select _i)) mod 256;
  116.                
  117.                 _temp1 = _state select _i;
  118.                 _temp2 = _state select _j;
  119.                
  120.                 _state set [_i, _temp2];
  121.                 _state set [_j, _temp1];
  122.                
  123.                 _rand = [(_state select ((_temp1 + _temp2) mod 256))] call _fnc_intToBin;
  124.             };
  125.            
  126.             _rbit = _rand select _mod;
  127.             _bin set [_forEachIndex, (_x && !_rbit) || {!_x && _rbit}]; // XOR
  128.            
  129.         } forEach _bin;
  130.        
  131.         #ifdef DEBUG_MODE_FULL
  132.             diag_log text format["[%1] ALiVE_fnc_crypt <info>: Trace: Line #%2. Benchmark: %3 sec.", round(diag_tickTime), __LINE__, (diag_tickTime - _startTime)];
  133.         #endif
  134.     };
  135. };
  136.  
  137. private ["_dec", "_buf", "_mod"];
  138. _dec = 0;
  139. _buf = [];
  140.  
  141. // Convert binary array to UTF-8 string
  142. { // forEach
  143.     _mod = _forEachIndex % MAX_CHAR_SIZE;
  144.    
  145.     if (_x) then {_dec = _dec + 2^((MAX_CHAR_SIZE - 1) - _mod)};
  146.    
  147.     if (_mod == 7) then
  148.     {
  149.         if (_dec == 0) then {_dec = CHAR_ZERO_REP};
  150.         _buf set [(count _buf), _dec];
  151.         _dec = 0;
  152.     };
  153. } forEach _bin;
  154.  
  155. #ifdef DEBUG_MODE_FULL
  156.     diag_log text format["[%1] ALiVE_fnc_crypt <info>: Processed: %2 bits. Benchmark: %3 sec.", round(diag_tickTime), (count _bin), (diag_tickTime - _startTime)];
  157. #endif
  158.  
  159. toString(_buf)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement