Article View: borland.public.cppbuilder.nativeapi
Article #8122Re: Encrypting/Decrypting stringlist
From: Remy Lebeau (TeamB)
Date: Tue, 23 Nov 2004 18:49
Date: Tue, 23 Nov 2004 18:49
97 lines
3911 bytes
3911 bytes
"roger" <rogersmail@ntlworld.com> wrote in message
news:41a37ce2$1@newsgroups.borland.com...
> dwDataLen = stringsin->Strings[a].Length();
> dwEncLen = dwDataLen;
> dwDataLen = stringsin->Strings[a].Length();
The second assignment to dwDataLen is redundant. It already has the value
of Length().
> szData = new CHAR[dwEncLen + 1];
You are allocating a buffer...
> szData = NULL;
Yet you are then nullifying the pointer withoug actually freeing it.
> delete[] szData;
szData is NULL by this point.
> if (bEncrypt) {
> if (myCrypto.Encrypt((BYTE*)szData,&dwDataLen,dwDataLen,true))
> stringsout->Add(StrPas(szData));
> } else {
> if (myCrypto.Decrypt((BYTE*)szData,&dwDataLen, true))
> stringsout->Add(StrPas(szData));
> }
You don't need to use StrPas(). It is an obsolete function. Just pass the
char* pointer directly, it will be converted to AnsiString automatically for
you:
stringsout->Add(szData);
Also, for your encryption, what does the actual encrypted data look like?
Are there are '\0' characters in it? There most likely is. That would
account for the incomplete data being passed back to the TStringList, since
you are not taking '\0' characters into account at all.
Try this code instead (untested):
void __fastcall TForm1::CryptStringList(TStrings* stringsin, TStrings*
stringsout, const AnsiString &thekey)
{
DWORD dwDataLen, dwEncLen;
AnsiString asData;
bool bOk;
CMyCrypto myCrypto(thekey.c_str(), CALG_RC4);
stringsout->BeginUpdate();
try
{
for(int a = 0; a < stringsin->Count; ++a)
{
dwDataLen = stringsin->Strings[a].Length();
dwEncLen = dwDataLen;
bOk = !bEncrypt;
if( !bOk )
bOk = myCrypto.Encrypt(NULL, &dwEncLen, 0, true);
if( bOk )
{
if( dwEncLen < dwDataLen )
asData.SetLength(dwDataLen);
else
asData.SetLength(dwEncLen);
strncpy(asData.c_str(), stringsin->Strings[a].c_str(),
dwDataLen);
if( bEncrypt )
bOk = myCrypto.Encrypt((BYTE*)szData.c_str(),
&dwDataLen, asData.Length(), true);
else
bOk = myCrypto.Decrypt((BYTE*)szData.c_str(),
&dwDataLen, true);
if( bOk )
{
asData.SetLength(dwDataLen);
stringsout->Add(asData);
}
}
}
}
__finally {
stringsout->EndUpdate();
}
}
Gambit
Technical Details
Message-ID:
<41a385b5$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news4.google.com!news.glorb.com!zen.net.uk!dedekind.zen.co.uk!npeer.de.kpn-eurorings.net!border2.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<41a37ce2$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,8141b40d2daa221d X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <41a37ce2$1@newsgroups.borland.com> Subject: Re: Encrypting/Decrypting stringlist Date: Tue, 23 Nov 2004 10:49:22 -0800 Lines: 99 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 68.69.17.41 Message-ID: <41a385b5$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1101235637 68.69.17.41 (23 Nov 2004 10:47:17 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news4.google.com!news.glorb.com!zen.net.uk!dedekind.zen.co.uk!npeer.de.kpn-eurorings.net!border2.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail