dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 28

Thread: [RESOLVED] Encrypt/Decrypt Problem

  1. #1
    Join Date
    Nov 2011
    Location
    India
    Posts
    309

    [RESOLVED] Encrypt/Decrypt Problem

    Hi,
    I was used the link - Encrypt/Decrypt a String

    I have a problem in my application.

    I was encrypt a string using the above link with a key from PC1.
    i like to decrypt the encrypted string(from PC1 - decrypt using the same key) from PC2.

    If i process Encryption/Decryption in a same PC, working correctly.
    but different PC, problem occurred.

    pls, clear me.
    Regards,

    SaraswathiSrinath

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,592

    Re: Encrypt/Decrypt Problem

    Quote Originally Posted by saraswathisrinath View Post
    If i process Encryption/Decryption in a same PC, working correctly.
    but different PC, problem occurred.
    What problem occurred?
    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2011
    Location
    India
    Posts
    309

    Re: Encrypt/Decrypt Problem

    Quote Originally Posted by VictorN View Post
    What problem occurred?
    I find my mistakes. But i don't know how to recover.
    Here i explain,

    1. I read my input and encrypt the string and store in a file.
    2. read the encrypt string from file and decrypt the file content and display it.
    Problem 1:
    I am reading the file, line by line and add the line in to a result string, finally i decrypt the result string.
    here i missed to add the "\n" for reading the file, line by line.
    Now i added the "\n" for every line except lost line.
    Probelm 1 solved.

    Problem 2:
    When i read the file, The file contains the symbol like ->.
    So reading is stopped when i reach the symbol ->.
    That is reading process thought the file end and come to decrypt process.
    So only i didn't get the full input.
    Note : my input string contains separators like Space, "\n", @, -, #.

    how can i solve this problem.
    Regards,

    SaraswathiSrinath

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Encrypt/Decrypt Problem

    Are you sure the encrypted data is in fact "a string" ?

    encrypted data isn't typically "a string" but is binary data. so you either need to change your code to handle binary data, or you need to encode the binary data into a string that can be transfered in a textfile. Something like binhex64 or uuencode or whatever, there's several ways to encode binary data into a string.

  5. #5
    Join Date
    Nov 2011
    Location
    India
    Posts
    309

    Re: Encrypt/Decrypt Problem

    Quote Originally Posted by OReubens View Post
    Are you sure the encrypted data is in fact "a string" ?

    encrypted data isn't typically "a string" but is binary data. so you either need to change your code to handle binary data, or you need to encode the binary data into a string that can be transfered in a textfile. Something like binhex64 or uuencode or whatever, there's several ways to encode binary data into a string.
    Example:
    Code:
    CString OPfileData = "8440:00:12:81,92:70:5102@sara@meera@2015:07:29,18:21:00:044@DB-64-69-42-9B-6D-9F-56-72-PR@#0:40b2b44cb36ghn@";
    
    const int size = 1024;
    
    TCHAR szRealInput[size] = _T("");
    TCHAR szEncryptInput[size] = _T("");
    TCHAR szDecryptedOutput[size] = _T("");
    
    _tcscpy(szRealInput, OPfileData);
    		
    EncryptString(szRealInput,szEncryptInput,Key);
    
    //in my project here i write the file , filewrite(CString file path, CString  szEncryptInput)
    
    TRACE("\nEncryptInput = %hs", szEncryptInput);
    
    /*EncryptInput = *i
    *b
    Ձnۿk*nC g!J8,`pX6ȺՍ\wnr*mNasOTb*p۸P|]ǝ)@T	i؞"avAe%F 1xvс
    !}ʵ8
    */
    
    //In my project here i read the file and store in a string. 
    //the copy in to szEncryptInput and starts decryption
    //szEncryptInput = ReadFile(CString filepath);
    
    DecryptString(szEncryptInput,szDecryptedOutput,Key);
    
    TRACE("\nszDecryptedOutput = %hs", szDecryptedOutput);
    
    /*//perfectly decrypted
    szDecryptedOutput = 8440:00:12:81,92:70:5102@sara@meera@2015:07:29,18:21:00:044@DB-64-69-42-9B-6D-9F-56-72-PR@#0:40b2b44cb36ghn@
    */
    But no data conversion process occur here. direct encryption / decryption working properly.
    encrypt the input , store in to a file. then read the file and my problem in - decryption the file input.
    here, TCHAR* & CString - both are contains the same encrypted data.
    Regards,

    SaraswathiSrinath

  6. #6
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Encrypt/Decrypt Problem

    Quote Originally Posted by saraswathisrinath View Post
    Code:
    //in my project here i write the file , filewrite(CString file path, CString  szEncryptInput)
    But no data conversion process occur here. direct encryption / decryption working properly.
    encrypt the input , store in to a file. then read the file and my problem in - decryption the file input.
    here, TCHAR* & CString - both are contains the same encrypted data.
    Apparently, the problem is in writing/reading the encrypted string to a file.
    What is filewrite()? And is there fileread()?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  7. #7
    Join Date
    Nov 2011
    Location
    India
    Posts
    309

    Re: Encrypt/Decrypt Problem

    Quote Originally Posted by VladimirF View Post
    Apparently, the problem is in writing/reading the encrypted string to a file.
    Exactly. when the encrypt symbol -> found the file reading thought the file end. so i can't read the full file.
    i don't know how to solve this problem?


    Quote Originally Posted by VladimirF View Post
    What is filewrite()? And is there fileread()?
    filewrite() And fileread() are two functions in my project writen by me.
    void filewrite(CString filename, CString writestr) - used to write the passing data into the file.
    CString fileRead(CString filename, CString readstr) - used to read the file by line by line and return the String data.

    Quote Originally Posted by OReubens View Post
    encode binary data into a string
    I will try to convert the encrypt TCHAR[] data into binary and then store these binary data into file.
    and read binary data from file and convert binary to CString/TCHAR[], after that i will try to decrypt.

    any suggestions or solutions, pls welcome.
    Regards,

    SaraswathiSrinath

  8. #8
    Join Date
    Nov 2011
    Location
    India
    Posts
    309

    Re: Encrypt/Decrypt Problem

    I found the symbol -> . This is ASCII control character named SUBstitute.
    When i reach this character, i quit to read the file.

    File reading is my problem. So i read the whole file content like below
    Code:
    CString Myclass::ReadControlCodesFromFile(CString filepath, CString strdata)
    {	
    	std::ifstream infile (filepath, std::ifstream::binary);
    	if (infile)
    	{
    		// get length of file:
    		infile.seekg (0, infile.end);
    		int length = infile.tellg();
    		infile.seekg (0, infile.beg);
    
    		char * buffer = new char [length];
    		infile.read(buffer,length);
    
    		strdata = CString(buffer);
    		strdata = strdata.Mid(0,length);//for avoid junk data 
    	
    		//TRACE( "\n\nbuffer = %s", buffer);
    		//TRACE( "\n\nstrdata = %s", strdata);
    		delete[] buffer;
    	}
    	return strdata;
    }
    this code is working. strdata returns the full file encrypt data. But, am i missing something in that file?
    becouse, when i pass the strdata in to decrypt process, the process failed due to wrong input string.

    Quote Originally Posted by saraswathisrinath View Post
    I find my mistakes. But i don't know how to recover.
    Problem 1:
    I am reading the file, line by line and add the line in to a result string, finally i decrypt the result string.
    here i missed to add the "\n" for reading the file, line by line.
    Now i added the "\n" for every line except lost line.Problem 1 solved.
    like the above problem, am i missing something while read the ASCII code?

    Any other way to read the ASCII codes.
    Regards,

    SaraswathiSrinath

  9. #9
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Encrypt/Decrypt Problem

    Quote Originally Posted by saraswathisrinath View Post
    I found the symbol -> . This is ASCII control character named SUBstitute.
    When i reach this character, i quit to read the file.
    SUB is 0x1A, or Ctrl+Z, which sometimes is used as an end-of-file mark.
    However, since you open your file as std::ifstream::binary, that should not matter.
    Are you sure that your string is terminated at that 0x1A character? Or may there be an embedded 0? Your could would fail in that case.
    Did you write your file as binary? Maybe it was truncated on write?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  10. #10
    Join Date
    Nov 2011
    Location
    India
    Posts
    309

    Re: Encrypt/Decrypt Problem

    Quote Originally Posted by VladimirF View Post
    SUB is 0x1A, or Ctrl+Z, which sometimes is used as an end-of-file mark.
    However, since you open your file as std::ifstream::binary, that should not matter.
    Are you sure that your string is terminated at that 0x1A character? Or may there be an embedded 0? Your could would fail in that case.
    Did you write your file as binary? Maybe it was truncated on write?
    Yes i write as binary like below and you are absolutely correct.
    I was checked, i try to write the encrypted data in to a file using fileWritechar method.
    The input(Encrypted data) also contains embedded 0 '/0'. So i failed to write the full content.
    Is any mistakes in my fileWritechar method?

    How can i write the file with out data loss?
    My problem is solved when i write the Encrypted data into a file in correct way.
    Code:
    void MyClass::fileWritechar(CString fPath, TCHAR *Data)
    {		
    	ofstream myfile(fPath, ios::out | ios::binary);
    	int i =0;	
    	while(Data[i] != '\0')	
    	{
    		myfile.put(Data[i]);				
    		i++;
    	}	
    	::AfxMessageBox("File Generated at " + fPath);
    	myfile.close();
    
    }
    Code:
    CString MyClass::ReadControlCodes(CString filepath, CString strdata)
    {	
    	std::ifstream infile (filepath, std::ifstream::binary);
    	if (infile)
    	{	// get length of file:
    		infile.seekg (0, infile.end);
    		int length = infile.tellg();
    		infile.seekg (0, infile.beg);
    		char * buffer = new char [length];		
    		infile.read(buffer,length);
    		strdata = buffer; 
    		strdata = strdata.Mid(0,length);//for avoid junk data 
    		delete[] buffer;
    	}
    	return strdata;
    }
    Regards,

    SaraswathiSrinath

  11. #11
    Join Date
    Nov 2011
    Location
    India
    Posts
    309

    Re: Encrypt/Decrypt Problem

    Hi
    can't append the char c into CString s1. When reach ASCII char = 215 its stop to append.

    pls help me.

    Code:
    CString MyClass::ReadControlCodesFromFile(CString filepath, CString strdata)
    {
        FILE *file;   
        int   fileSize;
        TCHAR *readBuffer;
      
        // Open the specified file in binary mode ("rb")
        file = fopen(filepath, "rb"); 
    
        // If the file wasn't opened successfully, print an error and close
        if(file == NULL)
        {
            perror("Failed to open file for read access.");
            return strdata;
        } 
    
        // Determine the file size
        fseek(file, 0L, SEEK_END);
        fileSize = ftell(file);
        rewind(file);
     
        // Allocate the read buffer
        readBuffer = (TCHAR *)malloc(fileSize); 
    
        // Read the file's data into the read buffer
        long read = fread(readBuffer, 1, fileSize, file);
         
    	char c;
    	int i = 0;			
    	TRACE("\n\nEncrypt Data From File\n");
    	for(int l = 0; l < fileSize; l++)
    	{
    		c = readBuffer[l];
    		i = static_cast<int>(c);			
    		TRACE("%c", c);	
    	}	
    	TRACE("\n\n");
    	
    
    	TRACE("\n\nMatch char = int Values \n");
    	CString s1 = "";
    		
    	for(int l = 0; l < fileSize; l++)
    	{
    		c = readBuffer[l];
    		i = static_cast<int>(c);	
    		if(i > 0)		TRACE("%c = %d ", c,i);	
    		else			TRACE("%c = %d ", c,256+i);
    		s1.AppendChar(i);
    	}	
    	TRACE("\n\n");	
    	::AfxMessageBox(s1);
    
       // Free allocated memory and close the file
        free(readBuffer);
        fclose(file);
       
        return strdata;
    }
    Output
    Perfectly print the full data here.
    Code:
    Encrypt Data From File
    *i	,`
    ځnۿkсfX b![B-vqU	7Ⱦl1wir*m=d
    -CXG'~.azݞcZa.utU)6&DEa*OZ9;}ǘ+TUEuxtOw0CpS^c<(%5׀Hv3^k㖰vO1S${! 8㦔|(W5
    PDA,O*pG;j+924C1
    T0onq?bB=vQ1ȚYBYMɌp&09}A+A0a9mf*V8}_׽j1+]OA2Zc0RiJsbxm!iAFJ&awiǦ0!\*5EI
    |Y:\!4۴ܲMJ0#+ƳБrj@(W1'O̪@ovZ%y?YPh7%
    Code:
    Match char = int Values 
     = 163  = 130 * = 42  = 19  = 183  = 217 i = 105 	 = 9 , = 44  = 192  = 157  = 245  = 20  = 210 ` = 96 
     = 13  = 218  = 129 n = 110  = 244  = 219  = 191 k = 107  = 209  = 129  = 183 f = 102 X = 88  = 230   = 32 b = 98  = 179  = 31 ! = 33  = 17  = 213 [ = 91  = 181 B = 66  = 127  = 236  = 149  = 243 - = 45  = 177  = 188  = 172  = 209 v = 118 q = 113 U = 85 	 = 9  = 190 7 = 55  = 206  = 6  = 227  = 200  = 190  = 157  = 138  = 165  = 250 l = 108 1 = 49 w = 119  = 239  = 182 i = 105  = 181 r = 114  = 175  = 204 * = 42  = 28  = 201 m = 109 = = 61 d = 100  = 131  = 3 
     = 13 - = 45  = 222 C = 67 X = 88  = 154 G = 71  = 239 ' = 39  = 22  = 244  = 169  = 230 ~ = 126  = 155  = 200 . = 46  = 152  = 154 a = 97  = 182  = 188  = 204 z = 122  = 221  = 158 c = 99  = 139  = 155  = 242 Z = 90 a = 97 . = 46  = 171 u = 117  = 135  = 239  = 5  = 162  = 199 t = 116  = 144 U = 85 ) = 41 6 = 54  = 187  = 19 & = 38 D = 68 E = 69 a = 97  = 238  = 167 * = 173 O = 79 Z = 90  = 194  = 241  = 141 9 = 57  = 133  = 151  = 164  = 2 ; = 59 } = 125  = 246  = 23  = 211  = 199  = 152  = 165  = 14  = 230 + = 43  = 5  = 15  = 176  = 176  = 229  = 2  = 177 T = 84  = 169 U = 85  = 153  = 12  = 229  = 17  = 25  = 253  = 31  = 180  = 242  = 164  = 136 E = 69 u = 117  = 26  = 22 x = 120  = 191 t = 116 O = 79 w = 119 0 = 48  = 253 C = 67 p = 112  = 164  = 227 S = 83  = 185 ^ = 94  = 186 c = 99 < = 60  = 158  = 252 ( = 40 % = 37 5 = 53  = 147  = 248  = 246  = 215  = 128  = 184  = 4 H = 72 v = 118  = 147 3 = 51  = 139 ^ = 94  = 2  = 186  = 7 k = 107  = 227  = 150  = 176  = 18  = 159 v = 118 O = 79 1 = 49  = 2 S = 83  = 240  = 157  = 138  = 229 $ = 36  = 241 { = 123  = 175  = 191 ! = 33   = 32 8 = 56  = 220  = 193  = 197  = 227  = 166  = 148  = 19  = 253  = 147  = 191 | = 124  = 188  = 6  = 151  = 253  = 214  = 205 ( = 40  = 202 W = 87 5 = 53  = 207 
     = 10 P = 80 D = 68  = 25 A = 65  = 253 , = 44 O = 79  = 249 * = 42 p = 112 G = 71  = 175  = 139  = 180  = 211  = 26  = 250  = 190 ; = 59 j = 106  = 167 + = 43 9 = 57  = 24 2 = 50  = 130  = 137  = 232 4 = 52  = 144 C = 67 1 = 49  = 216 
     = 13 T = 84 0 = 48  = 135 o = 111  = 148 n = 110 q = 113 ? = 63 b = 98 B = 66  = 225  = 195  = 252 = = 61 v = 118  = 156  = 222 Q = 81 1 = 49  = 200  = 154  = 156  = 221  = 233  = 6  = 17  = 251 Y = 89  = 26  = 207  = 29  = 226  = 235  = 206 B = 66 Y = 89 M = 77  = 189  = 201  = 140 p = 112 & = 38 0 = 48 9 = 57  = 156  = 247  = 197 } = 125 A = 65 + = 43  = 234 A = 65  = 230  = 242  = 245 0 = 48 a = 97  = 26 9 = 57  = 159  = 233  = 190  = 223  = 254 m = 109  = 147  = 194 f = 102 * = 173 V = 86 8 = 56  = 147  = 183  = 220 } = 125 _ = 95  = 215  = 189  = 241  = 198  = 233  = 182 j = 106  = 252  = 243  = 190  = 239 1 = 49  = 162  = 22  = 161  = 253  = 30 + = 43  = 168  = 193  = 251 ] = 93 O = 79  = 150 A = 65 2 = 50 Z = 90 c = 99  = 2  = 27  = 11  = 146  = 209 0 = 48 R = 82 i = 105 J = 74 s = 115  = 230 b = 98 x = 120  = 181 m = 109 ! = 33  = 8  = 131 i = 105  = 131  = 197  = 207  = 231  = 191 A = 65 F = 70 J = 74 & = 38 a = 97 w = 119  = 161 i = 105  = 199  = 166  = 18  = 230  = 235  = 204 0 = 48 ! = 33  = 149  = 19 \ = 92  = 215 * = 160  = 167  = 192  = 30  = 14 5 = 53  = 16  = 169 E = 69  = 146 I = 73  = 165  = 157 
     = 10 | = 124  = 141 Y = 89  = 193  = 19  = 144 : = 58  = 198  = 233 \ = 92 ! = 33  = 140  = 239 4 = 52  = 237  = 219  = 180  = 19  = 25  = 28  = 25  = 220  = 178  = 5 M = 77 J = 74  = 17  = 137  = 186 0 = 48  = 239 # = 35 + = 43  = 224  = 172  = 23  = 198  = 179  = 26  = 234  = 30  = 208  = 145  = 194  = 236 r = 114 j = 106 @ = 64 ( = 40 W = 87  = 241 1 = 49 ' = 39 O = 79  = 144  = 2  = 204  = 170  = 26 @ = 64 o = 111  = 253 v = 118 Z = 90  = 183 % = 37  = 190  = 193  = 6  = 128 y = 121 ? = 63 Y = 89  = 230 P = 80  = 169 h = 104  = 164  = 177  = 4 7 = 55  = 146 % = 37
    Wrongly print the data here -
    Code:
    s1 :
    *i	,`
    ځnۿkсfX b![B-vqU	7Ⱦl1wir*m=d
    -CXG'~.azݞcZa.utU)6&DEa*OZ9;}ǘ+TUEuxtOw0CpS^c<(%5
    Regards,

    SaraswathiSrinath

  12. #12
    Join Date
    Nov 2011
    Location
    India
    Posts
    309

    Re: Encrypt/Decrypt Problem

    The above problem also solved.

    I wrote the entire encrypted data in to a file as binary mode with out failed.
    Then started to Read that file as binary mode and stored into TCHAR *readBuffer.
    Code:
    //Read the file's data into read buffer
    fread(readBuffer, 1, filesize, file);
    This TCHAR *readBuffer data converted into a CString strdata.
    I faced a problems.
    While data conversion, the readBuffer contains NULL data.
    CString doesn't store null, so DecryptString() methord faild due to NULL data not stored in the input. that is filedata != CString.
    How can i store NULL and pass it to the DecryptString() methord ?

    Code:
            for(int l = 0; l < fileSize; l++)
    	{
    		c = readBuffer[l];
    		i = static_cast<int>(c);
    		
    		if(i >= 0)
    		{			
    			if(i == 0)   s1.Format("\0");	//NULL Data not Store here			
    			else	           s1.Format("%c",i);		    
    		}
    		else	
    		{			
    			s1.Format("%c", 256+i);			
    		}		
    		if( i != 256)
    		{	
    			strdata.Append(s1);
    			szEncryptInput[l] = c;// Null data Not stored here
    		}		
    	}		
    	TRACE("\nstrdata :\n%s", strdata);
    
            DecryptString((TCHAR*)strdata.GetString(),szDecryptedOutput, Key);
    	strdata = szDecryptedOutput;	
    	::AfxMessageBox("DecryptString"+ strdata);
    Regards,

    SaraswathiSrinath

  13. #13
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,592

    Re: Encrypt/Decrypt Problem

    Don't use CString. Use CByteArray instead.
    Victor Nijegorodov

  14. #14
    Join Date
    Nov 2011
    Location
    India
    Posts
    309

    Re: Encrypt/Decrypt Problem

    Quote Originally Posted by VictorN View Post
    Don't use CString. Use CByteArray instead.
    Read file like,
    Code:
    CByetArray * buff = (CByteArray *)malloc(filesize);
    fread(buff, 1, filesize, file);
    while calling decryption, Error found - str_misaligned from strlem.asm.
    Code:
    DecryptString((TCHAR*)buff ->GetData(),szDecryptedOutput, Key);
    How can i check(print) the data from CByteArray ?

    How can i pass CByteArray to the decrypt method?
    Code:
    BOOL DecryptString(TCHAR* szEncryptPwd,TCHAR* szPassword,TCHAR *szKey)
    Regards,

    SaraswathiSrinath

  15. #15
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,592

    Re: Encrypt/Decrypt Problem

    Quote Originally Posted by saraswathisrinath View Post
    Read file like,
    Code:
    CByetArray * buff = (CByteArray *)malloc(filesize);
    fread(buff, 1, filesize, file);
    It is wrong! You have to use CByteArray::SetSize and CByteArray::GetData methods.
    Look at MSDN!
    Victor Nijegorodov

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)