Article View: borland.public.cppbuilder.nativeapi
Article #7619Re: Encrypting/Decrypting stringlist
From: roger
Date: Mon, 17 Jan 2005 13:18
Date: Mon, 17 Jan 2005 13:18
165 lines
7091 bytes
7091 bytes
Everything seemed fine using the below code to initialize the CSP and being
called with
CMyCrypto myCrypto((LPSTR)thekey.c_str(), CALG_RC4);
/////////////////////////////////////////////////////////
CMyCrypto::CMyCrypto(LPTSTR pszPassword, ALG_ID algorithm)
{
m_hProv = NULL;
m_hKey = NULL;
// Get handle to the default key container in default CSP
if (CryptAcquireContext(&m_hProv, // handle to CSP
NULL, // default container
MS_DEF_PROV, // default CSP
PROV_RSA_FULL,// provider type
0)) { // action
//// here i set CRYPT_NEWKEYSET ////
HCRYPTHASH hHash;
// Get handle to hash object
if (CryptCreateHash(m_hProv, // handle to CSP
CALG_MD5, // hashing algorithm
0, // non-keyed hash
0, // reserved, must be zero
&hHash)) { // hash object
// Hash the password
if (CryptHashData(hHash, // handle to hash object
(LPBYTE)pszPassword, // data buffer
lstrlen(pszPassword)*sizeof(TCHAR),
0)) { // flags
// Create the session key using the hashed password
CryptDeriveKey(m_hProv, // handle to CSP
algorithm, // encryption algorithm
hHash, // hashed data
CRYPT_EXPORTABLE | 0x00280000,
&m_hKey); // session key
}
CryptDestroyHash(hHash);
}
}
}
/////////////////////////////////////////////////////////////
However when i tried the same program on a newly installed version of XP the
CryptAcquireContext() return false.
I tried with the action set as CRYPT_NEWKEYSET rather than 0 and then
retried with it set to 0 again and it worked..
so i am under the impression that if the function fails then recall with
CRYPT_NEWKEYSET and then start over.. as just carrying on after using
CRYPT_NEWKEYSET did not work.
My main concern however that is if i do it this way is that one pc will be
sending crypted commands and files to another..will they choose different
providers and even though they both use the same keystr for dec/encryption
hows it going to decrypt correctly when using different providers or do i
have to make sure it uses CRYPT_NEWKEYSET to use the same provider...sorry
for the lack of knowledge here but its my virgin run at it..
"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message
news:41a385b5$1@newsgroups.borland.com...
>
> "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:
<41ebbb0b@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!newsprint.newsread.com!newsread.com!news-xfer.newsread.com!news-out2.kabelfoon.nl!83.128.0.10.MISMATCH!newsfeed.kabelfoon.nl!213.218.66.70.MISMATCH!feeder.enertel.nl!nntpfeed-01.ops.asmr-01.energis-idc.net!216.196.110.149.MISMATCH!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> <41a385b5$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: "roger" <rogersmail@ntlworld.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <41a37ce2$1@newsgroups.borland.com> <41a385b5$1@newsgroups.borland.com> Subject: Re: Encrypting/Decrypting stringlist Date: Mon, 17 Jan 2005 13:18:10 -0000 Lines: 167 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2720.3000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2739.300 NNTP-Posting-Host: 81.103.69.122 Message-ID: <41ebbb0b@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1105967883 81.103.69.122 (17 Jan 2005 05:18:03 -0800) Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!newsprint.newsread.com!newsread.com!news-xfer.newsread.com!news-out2.kabelfoon.nl!83.128.0.10.MISMATCH!newsfeed.kabelfoon.nl!213.218.66.70.MISMATCH!feeder.enertel.nl!nntpfeed-01.ops.asmr-01.energis-idc.net!216.196.110.149.MISMATCH!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