4029 error while decrypt

 

Dear friends,

I am trying to decode the encrypted data that is stored in a file.  While doing so, I am getting 4029 ERR_ARRAY_INVALID.  I tried to solve it,  but not able to do so.  Can any one find the problem in this code. 

if(FileIsExist("Mytext.txt",0)){
      ResetLastError();
      int filehandle=FileOpen("Mytext",FILE_READ|FILE_TXT);

         if(filehandle!=INVALID_HANDLE){
         str=FileReadString(filehandle);
         //--- print the string

         Print(" String from file : ",str," || StringLen(str) : ",StringLen(str));
         //--- prepare key
         StringToCharArray(keystr,key);
         StringToCharArray(str,dst);

          res=CryptDecode(CRYPT_DES,dst,key,src);

         Print("res : ",res);
         if(res>0){
            //--- print decoded data
            string source=CharArrayToString(src);
            PrintFormat("Decoded data: size=%d, string='%s' ",ArraySize(src),CharArrayToString(src));
         }else
            Print("Error code=",GetLastError());
         
         FileClose(filehandle);   
         

         }

 }

 
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. You don't show your array declaration, nor indicate which line has the error. There are no mind readers here.
 
vkarthikeyenv:

Dear friends,

I am trying to decode the encrypted data that is stored in a file.  While doing so, I am getting 4029 ERR_ARRAY_INVALID.  I tried to solve it,  but not able to do so.  Can any one find the problem in this code. 

if(FileIsExist("Mytext.txt",0)){
      ResetLastError();
      int filehandle=FileOpen("Mytext",FILE_READ|FILE_TXT);

         if(filehandle!=INVALID_HANDLE){
         str=FileReadString(filehandle);
         //--- print the string

         Print(" String from file : ",str," || StringLen(str) : ",StringLen(str));
         //--- prepare key
         StringToCharArray(keystr,key);
         StringToCharArray(str,dst);

          res=CryptDecode(CRYPT_DES,dst,key,src);

         Print("res : ",res);
         if(res>0){
            //--- print decoded data
            string source=CharArrayToString(src);
            PrintFormat("Decoded data: size=%d, string='%s' ",ArraySize(src),CharArrayToString(src));
         }else
            Print("Error code=",GetLastError());
         
         FileClose(filehandle);   
         

         }

 }

 
uchar src[],dst[],key[];
uchar src[],dst[],key[];
if(FileIsExist("Mytext.txt",0)){
      ResetLastError();
      int filehandle=FileOpen("Mytext",FILE_READ|FILE_TXT);

         if(filehandle!=INVALID_HANDLE){
         str=FileReadString(filehandle);
         //--- print the string

         Print(" String from file : ",str," || StringLen(str) : ",StringLen(str));
         //--- prepare key
         StringToCharArray(keystr,key);
         StringToCharArray(str,dst);
          res=CryptDecode(CRYPT_DES,dst,key,src);

         Print("res : ",res);
         if(res>0){
            //--- print decoded data
            string source=CharArrayToString(src);
            PrintFormat("Decoded data: size=%d, string='%s' ",ArraySize(src),CharArrayToString(src));
         }else
            Print("Error code=",GetLastError());
         
         FileClose(filehandle);   
         
         }

 }
 
Sorry friend,  I have updated it.  Thanks 
 
vkarthikeyenv:
Sorry friend,  I have updated it.  Thanks 

It may not be the only problem - you have not shown us the encryption code - but using FILE_TXT with the encrypted data is going to be unreliable. For example, if the encrypted data contains a 0x0D character (\r), then FileReadString will only return the data up to that point; the reading of the file will be truncated.

You should write and read the encrypted file using FILE_BIN, not FILE_TXT.

For example: 

void OnStart()
{
   // Name of file to write and read; raw data to be encrypted, plus encryption cipher 
   // and key of appropriate length (with the key converted from a string to a byte array)
   string strDataFileName = "test.dat"; // Name of file to write
   string strOriginalData = "This is a test"; // Data to encrypt
   ENUM_CRYPT_METHOD cipher = CRYPT_AES256; // Encryption method
   string strKey = "01234567890123456789012345678901"; // Key for encryption
   uchar arrKey[];
   StringToCharArray(strKey, arrKey);
   
   // Encrypt the data to a file; read the data back again and decrypt it;
   // and check that the decrypted result matches the original   
   if (EncryptToFile(cipher, arrKey, strOriginalData, strDataFileName)) {

      string strDecryptedResult;
      if (DecryptFromFile(cipher, arrKey, strDataFileName, strDecryptedResult)) {

         if (strDecryptedResult == strOriginalData) {
            Print("Succeeded; decrypted data matches original!");
         } else {
            Print("FAILED: Decrypted data does not match original!");
         }
      
      } else {
         // Error in decryption; already logged
      }
   } else {
      // Error in encryption; already logged
   }
}

// Encrypts the data to the specified filename, returning true/false
bool EncryptToFile(ENUM_CRYPT_METHOD cipher, uchar & Key[], string strUnencryptedData, string strDataFileName)
{
   // Convert the unencrypted string to a byte array
   uchar arrUnencryptedData[];
   StringToCharArray(strUnencryptedData, arrUnencryptedData);
   
   // Encrypt the raw data
   uchar arrEncryptedData[];
   int szEnc = CryptEncode(cipher, arrUnencryptedData, Key, arrEncryptedData);
   if (szEnc <= 0) {Print("CryptEncode failed");return false;}
   
   // Write the encrypted data to a file
   int fWrite = FileOpen(strDataFileName, FILE_WRITE | FILE_BIN);
   if (fWrite == INVALID_HANDLE) {Print("Unable to open file for writing");return false;}
   FileWriteArray(fWrite, arrEncryptedData);
   FileClose(fWrite);

   return true;
}

// Decrypts the data from the specified filename, returning true/false.
// If successful (i.e. true), the decrypted data is passed back by reference in strResult
bool DecryptFromFile(ENUM_CRYPT_METHOD cipher, uchar & Key[], string strDataFileName, string & strResult)
{
   strResult = "";

   // Read the encrypted data back from the file 
   uchar arrFileData[];
   int fRead = FileOpen(strDataFileName, FILE_READ | FILE_BIN);
   if (fRead == INVALID_HANDLE) {Print("Unable to open file for reading");return false;}
   FileReadArray(fRead, arrFileData);
   FileClose(fRead);
   
   // Decrypt the data 
   uchar arrDecryptedData[];
   int szDec = CryptDecode(cipher, arrFileData, Key, arrDecryptedData);
   if (szDec <= 0) {Print("CryptDecode failed");return false;}
   
   // Convert the decrypted data back from an array to a string, and pass back by reference
   strResult = CharArrayToString(arrDecryptedData);
   return true;
}
 
jjc:

It may not be the only problem - you have not shown us the encryption code - but using FILE_TXT with the encrypted data is going to be unreliable. For example, if the encrypted data contains a 0x0D character (\r), then FileReadString will only return the data up to that point; the reading of the file will be truncated.

You should write and read the encrypted file using FILE_BIN, not FILE_TXT.

For example: 

 

Thank you jjc for identifying the mistake.  

I made 2 mistake

  1. I used FILE_TXT 
  2.  While writing to file I used FileWriteArray(),  but while retrieving data from file I have used FileReadString().  Thus I got error 4029.
Now I have corrected it.  Thank you for your post.