Article View: borland.public.cppbuilder.nativeapi
Article #7630Re: Encrypting/Decrypting stringlist
From: roger
Date: Tue, 18 Jan 2005 06:20
Date: Tue, 18 Jan 2005 06:20
201 lines
8410 bytes
8410 bytes
Ok i seemed to have narrowed it down... using the below as my normal user
works fine but if i log off and logon as guest it fails to the
ShowMessage("Crypto Error #101");
so i am assuming its only admin rights allowed to create a new context...
any suggestions how to get out of this as i am going to be sending encrypted
files from one pc to another and need both to use the same methods for
decrypting..
m_hProv = NULL;
m_hKey = NULL;
// Get handle to the default key container in default CSP
if (!CryptAcquireContext(&m_hProv,"container",MS_DEF_PROV,PROV_RSA_FULL,
0))
if(!CryptAcquireContext(&m_hProv,"container",MS_DEF_PROV,PROV_RSA_FULL,
CRYPT_NEWKEYSET))
if(!CryptAcquireContext(&m_hProv,"container",MS_DEF_PROV,PROV_RSA_FULL,
CRYPT_MACHINE_KEYSET|CRYPT_NEWKEYSET))
{
ShowMessage("Crypto Error #101");
return;
}
thanks in advance..
"roger" <rogersmail@ntlworld.com> wrote in message
news:41ebbb0b@newsgroups.borland.com...
> 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:
<41ecaabd@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!news2.euro.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> <41ebbb0b@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> <41ebbb0b@newsgroups.borland.com> Subject: Re: Encrypting/Decrypting stringlist Date: Tue, 18 Jan 2005 06:20:56 -0000 Lines: 203 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: <41ecaabd@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1106029245 81.103.69.122 (17 Jan 2005 22:20:45 -0800) Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!news2.euro.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