CryptDecode with modifier CRYPT_ARCH_ZIP - How to use? - page 5

 
Mikalas:

Then state what you want correctly.

There is code at the beginning of your topic.

You are reading the entire archive and you are trying to decode it with the header!

Any ZIP archive created by a standard packer has a HEADER!

MQL5 unpacker does NOT skip header.

Therefore, you cannot unpack data.

So, explain:

Initially what do you NEED?

Let me try to explain one last time:

The yellow rectangle in the top window is the word "test" packed by CryptEncode. The yellow rectangle below is the word "test" packed by standard zip archiver. The question is why the rectangles are different, and how to make their content the same.

 
C-4:

I'll try to explain one last time:

The yellow rectangle in the top window is the word "test" packed with CryptEncode. The yellow rectangle at the bottom is the word "test" packed by the standard zip archiver. The question is why rectangles are different and how to make their content equal.

Because DIFFERENT compression methods are used

http://tools.ietf.org/html/rfc1951

 

there's something wrong with the archiving

I archive with one key, unarchive with another key and it's fine:

//+------------------------------------------------------------------+
//| ArrayToHex                                                       |
//+------------------------------------------------------------------+
string ArrayToHex(uchar &arr[],int count=-1)
  {
   string res="";
//--- проверка размера
   if(count<0 || count>ArraySize(arr))
      count=ArraySize(arr);
//--- преобразование в шестнадцатиричную строку
   for(int i=0; i<count; i++)
      res+=StringFormat("%.2X",arr[i]);
//---
   return(res);
  }
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   string text="sdfgghjdfggfghjghghjk,g";
   string keystr="asgfgfdfgfghjhjmhjmfhjmhjmhjmhjmhjhj";
   uchar src[],dst[],key[];
//--- подготовка ключа шифрования
   StringToCharArray(keystr,key);
//--- подготовка исходного массива src[]
   StringToCharArray(text,src);
//--- вывод исходных данных
   PrintFormat("Initial data: size=%d, string='%s'",ArraySize(src),CharArrayToString(src));
//--- шифрование массива src[] методом DES с 56-битным ключом key[]
   int res=CryptEncode(CRYPT_ARCH_ZIP,src,key,dst);
   keystr="asgfgfdfg";
   StringToCharArray(keystr,key);
//--- проверка результата шифрования
   if(res>0)
     {
      //--- вывод шифрованных данных
      PrintFormat("Encoded data: size=%d %s",res,ArrayToHex(dst));
      //--- расшифровка данных массива dst[] методом DES с 56-битным ключом key[]
      res=CryptDecode(CRYPT_ARCH_ZIP,dst,key,src);
      //--- проверка результата
      if(res>0)
        {
         //--- вывод дешифрованных данных
         PrintFormat("Decoded data: size=%d, string='%s'",ArraySize(src),CharArrayToString(src));
        }
      else
         Print("Ошибка в CryptDecode. Код ошибки=",GetLastError());
     }
   else
      Print("Ошибка в CryptEncode. Код ошибки=",GetLastError());
  }
 



or are the keys only used for encryption?

 
The algorithm has settings. Maybe it is about them. Then you need to find out what they are in MQL5.
 
It looks like the keys are only valid for encryption.
 
TheXpert:
The algorithm has settings. Maybe it is about them. Then you need to find out what they are in MQL5.

torture them or something?

They will say we are scoundrels who do not know how the archiving algorithm works )

 
TheXpert:
The algorithm has settings. Maybe it is about them. Then you need to find out what they are in MQL5.
The algorithm must certainly have some settings, at least, the compression ratio setting. The only place where you can pass these settings to CryptDecode is the key. If it is not sensitive to the key, then it is not using it. This is bad, because in this case you can neither uncompress third-party archive, nor pack your own archive for use in third-party archivers.
 
Mikalas:

Because DIFFERENT compression methods are used

http://tools.ietf.org/html/rfc1951

Michael, let's not play "Captain Obvious". We all know that zip is different and we all know what an archive header is.
 
sanyooooook:

should we torture them?

They will say that we are scoundrels who do not know how the archiving algorithm works)

Most likely another crude function where features are outlined but not yet implemented.
 

Unpack standard ZIP files!

(for now, only with one packed file in the archive, later I will make it for several (if needed) )

string file_name = "";
   uchar array[];
   uchar key[];
   uchar result[];
//---   
   int handle = FileOpen( "GAZR-6.15.zip", FILE_READ|FILE_BIN ); //Здесь имя Вашего файла и гдe лежит! 
   if ( handle != INVALID_HANDLE )
   {
     FileSeek( handle, 18, SEEK_SET );
     long pack_unp_size = FileReadLong( handle );
     long pack_size = ( pack_unp_size & 0xFFFFF );
     long unp_size = ( pack_unp_size >> 32 );
     long names_len = FileReadLong( handle ); 
     long fn_len = ( names_len & 0xFF );
     long extr_fild_len = ( ( names_len & 0xFF00 ) >> 8 );
     FileSeek( handle, 30, SEEK_SET );
     uint fn_res = FileReadArray( handle, array, 0, int( fn_len ) );
     if ( fn_res == uint( fn_len ) )
     {
       for ( int i = 0; i < int( fn_res ); i++ )
       {
         file_name += CharToString( array[i] );
       }
     }
     if ( extr_fild_len != 0 )
     {
       FileSeek( handle, 30 + fn_len + extr_fild_len, SEEK_SET );
     }
     else
     {
       FileSeek( handle, 30 + fn_len, SEEK_SET );
     }
//---
     ArrayResize( array, int( pack_size ) + 6 );
     
     uint ar_res = FileReadArray( handle, array, 2, int( pack_size ) );
     if ( ar_res == uint( pack_size ) ) 
     {
       array[0] = 120;
       array[1] = 94;
       array[int( pack_size ) + 2] = 193;
       array[int( pack_size ) + 3] = 12;
       array[int( pack_size ) + 4] = 31;
       array[int( pack_size ) + 5] = 159;
       ArrayResize( key, int( pack_size ) + 6 );
       for ( int i = 0; i < int( pack_size ) + 6; i++ ) key[i] = 0;
       ResetLastError();
       int dec_res = CryptDecode( CRYPT_ARCH_ZIP, array, key, result );
       if ( dec_res == int( unp_size ) )
       {
         Print( "Data unpack! File name = ", file_name );
       }
       else
       {
         Print( GetLastError() );
       }  
     }     
     FileClose( handle );
   }