473,398 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

cpencrypt, in out parameters

hi
I'm trying to write CPEncrypt function. but i don't change the
content's of the pbData. First it contained the plaintext. But after
encryption i did not write the ciphertext value on it. Actually it
change in the CSP dll but pbData which is in the cryptencrypt function
didn't change. how could i write the value on pbData? when i used
"strcpy((char* )pbData,(char *)tempData);" the progmram send
error.(tempdata is carrying ciphertext value)
Mycode(smaple program which is using my own CSP) is below.
thanks.

if (RCRYPT_FAILED(CryptAcquireContext(&hProv, "selami",
"MyCSP ", PROV_RSA_FULL, CRYPT_NEWKEYSET)))
{
printf("FAILED CryptAcquireConext returned error %x\n",
GetLastError());
return(TRUE);
}
else
printf("\n CryptAcquireContext SUCCEED\n");
Nov 13 '05 #1
3 2510
On 16 Nov 2003 23:34:23 -0800, se****@uekae.tubitak.gov.tr (p_hose)
wrote in comp.lang.c:

Note that non-standard, third party libraries like CPEncrypt, whatever
the heck it is, and DLLs, which are a part of the Windows operating
system and not part of the C language, are off-topic here, but I did
spot at least one genuine C coding mistake.
hi
I'm trying to write CPEncrypt function. but i don't change the
content's of the pbData. First it contained the plaintext. But after
encryption i did not write the ciphertext value on it. Actually it
change in the CSP dll but pbData which is in the cryptencrypt function
didn't change. how could i write the value on pbData? when i used
"strcpy((char* )pbData,(char *)tempData);" the progmram send
error.(tempdata is carrying ciphertext value)
Mycode(smaple program which is using my own CSP) is below.
thanks.
[snip]
BYTE *outdata;
outdata=(BYTE *)malloc(100);
Don't cast the return value of malloc() in C. It is not necessary and
can prevent the compiler from warning you about a potentially serious
error if you neglected to include <stdlib.h> and don't have a
prototype in scope.

And always test the pointer value returned by malloc() for NULL. If
the allocation failed and you don't test it, you will be dereferencing
a null pointer and generating undefined behavior.

In any case, the call to malloc(), if it succeeded, returned a pointer
to the newly allocated space.
outdata=(unsigned char *)"selami uekae tubitak\0";
Now you immediately throw away that pointer by coping the address of a
string literal into it. You have just leaked the memory you
allocated.
DWORD len = strlen((const char *)outdata);
strlen() returns a value of type size_t. Why cast it to anything
else?
if(!CryptEncrypt(hKey, 0,TRUE,0,outdata,&len,(DWORD)100 ))
If the CryptEncrypt() function tries to modify the data passed to in
via the outdata pointer, you have undefined behavior by attempting to
modify a string literal.

[snip]
if i used below code instead of "outdata=(BYTE *)malloc(100)" and
encrypted the hash value the program work properly.
BYTE outdata[100];
outdata[0]=(char)"s";
outdata[1]=(char)"e";
outdata[2]=(char)"l";
outdata[3]=(char)"a";
outdata[4]=(char)"m";
outdata[5]=(char)"i";
outdata[6]=(char)"h";
outdata[7]=(char)"o";
outdata[8]=(char)"z";
outdata[9]=(char)"b";
outdata[10]=(char)"e";
outdata[11]=(char)"f";
outdata[12]=(char)"b";
outdata[13]=(char)"y";
outdata[14]='\0';


Here you are not losing the pointer returned by malloc(), and for sure
you are not attempting to modify a string literal.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #2
hi
sorry my problem is how could i change the content's of pbData in my
CSP dll? First it contained the plaintext. But after
encryption i did not write the ciphertext value on it.could you send
me code for this problem?
thanks.
BOOL WINAPI
CPEncrypt(
IN HCRYPTPROV hProv,
IN HCRYPTKEY hKey,
IN HCRYPTHASH hHash,
IN BOOL fFinal,
IN DWORD dwFlags,
IN OUT LPBYTE pbData,
IN OUT LPDWORD pcbDataLen,
IN DWORD cbBufLen)
{
char *tempData;
tempData=(PBYTE)malloc(strlen(outData));
..
..//pbData (plaintext) is encrypted
..//and ciphertext is written on tempData
..
printf("tempdata degeri %s %d \n",tempData,strlen((char *)tempData));
//in mysample program i can see these values
memcpy(pbData,tempData,strlen(tempData));//and in this line
//CSP send error 0x57 but if i //use hashed data for encryption
//the program is working properly

return true;
}
my sample program code
..
..
..
BYTE *outdata;
outdata=(BYTE *)malloc(100);
size_t size;
size=_msize(outdata);
outdata=(unsigned char*)"selami uekae tubitak";
DWORD len = strlen((const char *)outdata);
if(!CryptEncrypt(hKey, 0,TRUE,0,outdata,&len,(DWORD)size ))
printf("Error %x during CryptEncrypt!\n", GetLastError());
else printf("CryptEncrypt succeed\n");
Nov 13 '05 #3
p_hose wrote:

sorry my problem is how could i change the content's of pbData
in my CSP dll? First it contained the plaintext. But after
encryption i did not write the ciphertext value on it.could
you send me code for this problem?

.... snip ...

What part of Jack Kleins "off-topic" message was too complicated
for you to understand? Google for "reading for comprehension
classes".

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Zlatko Matić | last post by:
Let's assume that we have a database on some SQL server (let it be MS SQL Server) and that we want to execute some parameterized query as a pass.through query. How can we pass parameters to the...
1
by: p_hose | last post by:
hi I'm trying to write CPEncrypt function. but i don't change the content's of the pbData. First it contained the plaintext. But after encryption i did not write the ciphertext value on it....
3
by: Bill Cart | last post by:
The oldDb Command does not support Named Parameters. If I am reading them correctly, the docs say that the order of the parameter should match the order they are declared in the Stored Procedure....
2
by: Mark | last post by:
I created a test to check the execution time difference between executing a SQL Server stored procedured using explicit parameters versus not. In one case I created new SqlParameters in the code,...
4
by: Tim::.. | last post by:
Can someone tell me a better way or give me a link that shows a better way to create large numbers of SQL parameters... Example... A better way to write this code! <code> Sub...
1
by: Mikey G | last post by:
Hi, I created a simple VB.NET 2003 application through Visual Studio that connects to a MySQL database and loads a table into a Dataset, and then displays that table information in a DataGrid on a...
14
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To...
2
by: Hexman | last post by:
Hello All, Well I'm stumped once more. Need some help. Writing a simple select and update program using VB.Net 2005 and an Access DB. I'm using parameters in my update statement and when trying...
12
by: pamelafluente | last post by:
Hi guys, In the past I have used several time optional parameters in my function. But Now I am more inclined to think that they are more dangerous than useful, and probably better to be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.