Click here to Skip to main content
15,897,032 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: 'delete' operator Pin
Maxwell Chen21-Apr-06 7:50
Maxwell Chen21-Apr-06 7:50 
AnswerRe: 'delete' operator Pin
David Crow21-Apr-06 9:12
David Crow21-Apr-06 9:12 
QuestionProblem with CSocket Listen function Pin
jpyp21-Apr-06 7:28
jpyp21-Apr-06 7:28 
AnswerRe: Problem with CSocket Listen function Pin
basementman21-Apr-06 8:35
basementman21-Apr-06 8:35 
GeneralRe: Problem with CSocket Listen function Pin
jpyp21-Apr-06 9:39
jpyp21-Apr-06 9:39 
QuestionHow to use Spy++? Pin
XXKKFF21-Apr-06 6:46
XXKKFF21-Apr-06 6:46 
AnswerRe: How to use Spy++? Pin
Eric Dahlvang21-Apr-06 6:52
Eric Dahlvang21-Apr-06 6:52 
QuestionVerify a message signed from javacard by smartcard in Visual C++ Pin
Malacitano21-Apr-06 6:34
Malacitano21-Apr-06 6:34 
I have a smartcard Cyberflex Access e-gate and I have developed for it an applet that receives an encrypted text (encrypted by a visual c++ application, using CryptoAPI), decrypts the text and returns the decrypted text signed. The problem is that I don’t get visual c++ application verifies signed text.

On card I have the following code:

byte apduBuffer[] = apdu.getBuffer(); 		
short byteRead = (short)(apdu.setIncomingAndReceive());

// First, we decrypt msg --------------------------------------    
cipherRSA = Cipher.getInstance(Cipher.ALG_RSA_PKCS1, false);
cipherRSA.init(privateKey, Cipher.MODE_DECRYPT);
cipherRSA.doFinal(apduBuffer,(short)APDUDATA, byteRead, apduBuffer, (short)0);
  
// Second, we sign only the first byte of decrypted msg ------------------------------
cipherRSA.init(privateKey, Cipher.MODE_ENCRYPT);
 
short outbytes; 	    
outbytes = cipherRSA.doFinal(apduBuffer,(short)APDUDATA, (short) 1, apduBuffer, (short)0);
    	
// Finally, we send results ------------------------------------
apdu.setOutgoing(); 
// indicate the number of bytes in the data field
apdu.setOutgoingLength((short)outbytes); 
// at offset 0 send 128 byte of data in the buffer
apdu.sendBytes((short) 0, (short) outbytes);


On visual c++ application I have the following code:

pbContent = (BYTE*) "Security is our business..."; // The message
cbContent = strlen((char *)pbContent)+1; // Size of message
cbEncryptedBlob = cbContent;


if(!CryptAcquireContext(
		&hProv, // Address for handle to be returned.
		NULL, // Use the current user's logon name.
		MS_DEF_PROV,
		PROV_RSA_FULL, // Need to both encrypt and sign.
		NULL)) {
	MessageBox("CryptAcquireContext failed.", "Error", MB_OK);
}


	
if (!CryptImportKey (hProv, rsaKeyBlob, sizeof(rsaKeyBlob), 0,0, &hMiKey)) 
{
	MessageBox("Public key import failed.", "Error", MB_OK);
	exit(1); // Kaboooommmmmmm!
}

CryptEncrypt(hMiKey, 0, TRUE, 0, NULL, &cbEncryptedBlob, cbContent); // we ask size

	
if(pbEncryptedBlob = (BYTE*)malloc(cbEncryptedBlob))
{
	MessageBox("Memory has been allocated for the encrypted BLOB.", "Info", MB_OK);
	memset(pbEncryptedBlob, '\0', cbEncryptedBlob);
	memcpy(pbEncryptedBlob, pbContent, cbContent); 
}
else
{
	MessageBox("Memory allocation error while encrypting.", "ERROR", MB_OK);
}

if(!CryptEncrypt(hMiKey, NULL, TRUE, NULL, pbEncryptedBlob, &cbContent, cbEncryptedBlob)) {
	MessageBox("CryptEncrypt failed.", "Error", MB_OK);
} else {
	char* EncryptedString = new char[(cbEncryptedBlob * 2) +1];
	ByteToStr(cbEncryptedBlob, pbEncryptedBlob, EncryptedString);
	wsprintf(informacion,"The size of encrypted message is %d bytes.\n%s",cbEncryptedBlob,EncryptedString);
	MessageBox(informacion, "Info", MB_OK);
}


Now I send pbEncryptedBlob (reversed because of little-big endian issue) to the card and I receive a BYTE[128]. That array is what I try to verify with the public key visual c++ application has read from card at the beginning.

if(!CryptDecrypt(hMyKey, NULL, TRUE, NULL, message, &lengthMsg)) {

	LPVOID lpMsgBuf;
	DWORD dw = GetLastError(); 

	FormatMessage(
		FORMAT_MESSAGE_ALLOCATE_BUFFER | 
		FORMAT_MESSAGE_FROM_SYSTEM,
		NULL,
		dw,
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		(LPTSTR) &lpMsgBuf,
		0, NULL );

	wsprintf(informacion, "failed with error %d: %s", dw, lpMsgBuf); 
	MessageBox(informacion, "Error", MB_OK);

	LocalFree(lpMsgBuf);
}


I get “Bad key” error here.
hMyKey comes from importing this byte array:

BYTE rsaKeyBlob[] = {
	// BLOBHEADER
		0x06,		// PUBLICKEYBLOB
		0x02,		// CUR_BLOB_VERSION
		0x00,0x00,	// reserved
		0x00,0xa4,0x00,0x00, // CALG_RSA_KEYX
	// RSAPUBKEY
		0x52,0x53,0x41,0x31, // "RSA1" (public key)
		0x00,0x04,0x00,0x00, // bitlen = 1024
		0x01,0x00,0x01,0x00, // pubExp = 65537
	// inverted modulus
		0x95,0x04,0x59,0xEC,0x00,0x2D,0x45,0x93,
		0x4F,0x78,0x9A,0x3C,0x3F,0x60,0xDD,0xC4,
		0xE9,0x8D,0x0E,0xFE,0xEA,0x61,0xC7,0x77,
		0x8E,0x60,0x42,0x00,0xFA,0xE8,0x58,0xDD,
		0x44,0x2F,0xFF,0x88,0x7A,0xB1,0xED,0xB9,
		0x1A,0xDC,0x02,0x88,0x83,0xDD,0x8A,0xB4,
		0x23,0xEA,0x5D,0x3F,0x0E,0x16,0x8C,0x0B,
		0xFE,0x5C,0xD6,0x49,0x7D,0xEE,0xBF,0xDF,
		0x52,0xCF,0x0A,0x66,0x09,0xCF,0x44,0xA0,
		0xB5,0x26,0x8B,0x84,0xFA,0x3E,0xFE,0x37,
		0x8D,0x85,0xCE,0x30,0xCC,0xD9,0xC1,0x29,
		0x86,0x77,0x96,0x11,0xA4,0xF7,0xE0,0xD2,
		0x6E,0x1B,0xA9,0xE5,0x66,0xC0,0x5E,0xC7,
		0x5E,0x5E,0x50,0x0C,0xB1,0xB5,0xAC,0x4C,
		0x3A,0x8D,0x11,0x25,0x23,0xBC,0x2A,0x19,
		0x22,0x77,0x0B,0xCF,0x47,0xFF,0x9B,0xAC
   };


Can I decrypt a message encrypted with the private key using public key in visual c++?

Do I have to use any special function to do it or I can do it like I do it in java (if I encrypt with private key, everyone can decrypt with associate public key)?
QuestionChange the backgroound color for a CListCtrl control Pin
masnu21-Apr-06 6:16
masnu21-Apr-06 6:16 
AnswerRe: Change the backgroound color for a CListCtrl control Pin
Naveen21-Apr-06 21:15
Naveen21-Apr-06 21:15 
QuestionCComboBox - DropList Style- Horizontal Scrollbar? Pin
jayart21-Apr-06 6:08
jayart21-Apr-06 6:08 
AnswerRe: CComboBox - DropList Style- Horizontal Scrollbar? Pin
David Crow21-Apr-06 6:21
David Crow21-Apr-06 6:21 
Question[Message Deleted] Pin
sherton21-Apr-06 4:38
sherton21-Apr-06 4:38 
AnswerRe: System () in vc++ 6.0 Pin
toxcct21-Apr-06 4:52
toxcct21-Apr-06 4:52 
GeneralRe: System () in vc++ 6.0 Pin
sherton22-Apr-06 0:39
sherton22-Apr-06 0:39 
GeneralRe: System () in vc++ 6.0 Pin
super_ttd22-Apr-06 6:24
super_ttd22-Apr-06 6:24 
AnswerRe: System () in vc++ 6.0 Pin
David Crow21-Apr-06 4:54
David Crow21-Apr-06 4:54 
AnswerRe: System () in vc++ 6.0 Pin
Aqueel21-Apr-06 5:23
Aqueel21-Apr-06 5:23 
QuestionShortDate in MFC Pin
QuickDeveloper21-Apr-06 4:33
QuickDeveloper21-Apr-06 4:33 
AnswerRe: ShortDate in MFC Pin
James R. Twine21-Apr-06 4:50
James R. Twine21-Apr-06 4:50 
AnswerRe: ShortDate in MFC Pin
Roger Stoltz21-Apr-06 4:55
Roger Stoltz21-Apr-06 4:55 
Questionlistview position after inserting an item Pin
karfi21-Apr-06 3:57
karfi21-Apr-06 3:57 
QuestionDisabling Wireless while on network Pin
lynchspawn21-Apr-06 3:33
lynchspawn21-Apr-06 3:33 
QuestionInformation on cells of a CListctrl Pin
Sandeep A.C21-Apr-06 3:22
Sandeep A.C21-Apr-06 3:22 
AnswerRe: Information on cells of a CListctrl Pin
Naveen21-Apr-06 3:36
Naveen21-Apr-06 3:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.