-
July 28th, 2015, 06:52 AM
#1
[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
-
July 28th, 2015, 07:08 AM
#2
Re: Encrypt/Decrypt Problem
Originally Posted by saraswathisrinath
If i process Encryption/Decryption in a same PC, working correctly.
but different PC, problem occurred.
What problem occurred?
Victor Nijegorodov
-
July 29th, 2015, 12:40 AM
#3
Re: Encrypt/Decrypt Problem
Originally Posted by VictorN
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
-
July 29th, 2015, 06:55 AM
#4
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.
-
July 29th, 2015, 08:16 AM
#5
Re: Encrypt/Decrypt Problem
Originally Posted by OReubens
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·!ÊJ¡8’ù,³¼®Ð`pX½6Î*Ⱥž„Õ\w˜¶nÉr«Ê*½mNaîsOìTÜ•b¤®*pÛ¸P®Œ|³¨Ô]Ç)¹Å*@TÐ *šiØž"ÐavÓAe%ªüîF¥çõ …Ž§1xšvÔÑ¥
–!}ʵä³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
-
July 29th, 2015, 10:48 AM
#6
Re: Encrypt/Decrypt Problem
Originally Posted by saraswathisrinath
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...
-
July 29th, 2015, 10:58 PM
#7
Re: Encrypt/Decrypt Problem
Originally Posted by VladimirF
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?
Originally Posted by VladimirF
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.
Originally Posted by OReubens
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
-
July 30th, 2015, 05:33 AM
#8
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.
Originally Posted by saraswathisrinath
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
-
July 30th, 2015, 09:38 AM
#9
Re: Encrypt/Decrypt Problem
Originally Posted by saraswathisrinath
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...
-
August 1st, 2015, 06:47 AM
#10
Re: Encrypt/Decrypt Problem
Originally Posted by VladimirF
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
-
August 5th, 2015, 01:59 AM
#11
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ÎãȾ*¥úl1wï¶iµr¯Ì*Ém=dƒ
-ÞCXšGï'ô©æ~›È.˜ša¶¼ÌzÝžc‹›òZa.«u‡ï¢ÇtU)6»&DEaî§*OZÂñ9…—¤;}öÓǘ¥æ+°°å±T©U™åý´ò¤ˆEux¿tOw0ýCp¤ãS¹^ºc<žü(%5“øö×€¸Hv“3‹^ºkã–°ŸvO1Sð*å$ñ{¯¿! 8ÜÁÅ㦔ý“¿|¼—ýÖÍ(ÊW5Ï
PDAý,Où*pG¯‹´Óú¾;j§+92‚‰è4C1Ø
T0‡o”nq?bBáÃü=vœÞQ1ÈšœÝéûYÏâëÎBYM½ÉŒp&09œ÷Å}A+êAæòõ0a9Ÿé¾ßþm“Âf*V8“·Ü}_×½ñÆé¶jüó¾ï1¢¡ý+¨Áû]O–A2Zc’Ñ0RiJsæbxµm!ƒiƒÅÏç¿AFJ&aw¡iǦæëÌ0!•\×*§À5©E’I¥
|YÁ:Æé\!Œï4*۴ܲMJ‰º0ï#+*¬Æ³êБÂìrj@(Wñ1'O̪@oývZ·%¾Á€y?YæP©h¤±7’%
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ÎãȾ*¥úl1wï¶iµr¯Ì*Ém=dƒ
-ÞCXšGï'ô©æ~›È.˜ša¶¼ÌzÝžc‹›òZa.«u‡ï¢ÇtU)6»&DEaî§*OZÂñ9…—¤;}öÓǘ¥æ+°°å±T©U™åý´ò¤ˆEux¿tOw0ýCp¤ãS¹^ºc<žü(%5“øö
Regards,
SaraswathiSrinath
-
August 5th, 2015, 06:57 AM
#12
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
-
August 5th, 2015, 07:08 AM
#13
Re: Encrypt/Decrypt Problem
Don't use CString. Use CByteArray instead.
Victor Nijegorodov
-
August 5th, 2015, 08:11 AM
#14
Re: Encrypt/Decrypt Problem
Originally Posted by VictorN
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
-
August 5th, 2015, 08:48 AM
#15
Re: Encrypt/Decrypt Problem
Originally Posted by saraswathisrinath
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|