Click here to Skip to main content
15,921,837 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: double problem Pin
Sirrius23-Sep-03 13:18
Sirrius23-Sep-03 13:18 
GeneralRe: double problem Pin
David Crow23-Sep-03 16:58
David Crow23-Sep-03 16:58 
GeneralRe: double problem Pin
Sirrius23-Sep-03 18:19
Sirrius23-Sep-03 18:19 
GeneralOrdered Linked List Pin
Sirrius23-Sep-03 10:01
Sirrius23-Sep-03 10:01 
GeneralRe: Ordered Linked List Pin
Joaquín M López Muñoz23-Sep-03 11:00
Joaquín M López Muñoz23-Sep-03 11:00 
GeneralRe: Ordered Linked List Pin
David Crow23-Sep-03 11:20
David Crow23-Sep-03 11:20 
GeneralRe: Ordered Linked List Pin
Sirrius23-Sep-03 13:20
Sirrius23-Sep-03 13:20 
GeneralRC2 encryption interoperability between VC++ and VB.NET Pin
sheelag23-Sep-03 8:49
sheelag23-Sep-03 8:49 
Hi Everyone,

I have two applications (one developed in VC++ 6.0 and the other developed in VB.NET )which communicate to each other in secure manner.) I am using RC2 encryption to encrypt messages between the two applications. Both of them use the same key , IV , padding and the same cipher mode (ECB). I want to encrypt messages in one application and decrypt in the other application. But I am not able to do that. I am getting bad data exception when i Encrypt in one application and try to decrypt the message in another application.

I am pasting the code below from both the applications.

VC++ 6.0
GetSessionKey {
CryptAcquireContext( &hProv, NULL,
MS_ENHANCED_PROV , PROV_RSA_FULL, 0 );

CryptGetUserKey(hProv, AT_KEYEXCHANGE ,&hPublicKey);
BYTE tKey[] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
DWORD tLen = 16 ;
DWORD dtLen = 16 ;
fResult = CryptEncrypt(hPublicKey ,0,TRUE ,0 , NULL , &tLen , dtLen );
BYTE *tKey1 = new BYTE [tLen] ;
memset(tKey1 ,'\0', tLen) ;
memcpy(tKey1 , tKey , dtLen) ;
BOOL fResult = CryptEncrypt(hPublicKey ,0,TRUE ,0 , tKey1 , &dtLen , tLen );

fResult = CryptGenKey(hProv, CALG_RC2, CRYPT_EXPORTABLE, &hTempKey);
dwSize = sizeof(DWORD);
DWORD dwBlobLen ;
fResult = CryptGetKeyParam(hTempKey, KP_KEYLEN, (LPBYTE)&dwProvSessionKeySize,
&dwSize, 0);

CryptExportKey( hTempKey, hPublicKey, SIMPLEBLOB, 0, NULL, &dwBlobLen);
pbKeyBlob = (BYTE*)malloc(dwBlobLen);
//--------------------------------------------------------------------
// Export the key into a simple key BLOB.

fResult = CryptExportKey(hTempKey, hPublicKey, SIMPLEBLOB, pbKeyBlob, &dwBlobLen);

int tlen = sizeof(ALG_ID) + sizeof(BLOBHEADER) ;
pbPtr = pbKeyBlob;

pbPtr =pbKeyBlob + sizeof(ALG_ID) + sizeof(BLOBHEADER) ;
//copy the key in the blob
for (n = 0 ; n < dwProvSessionKeySize ; n++)
{
pbPtr[n] = tKey1 [n];
}
if (hTempKey) CryptDestroyKey(hTempKey);
DWORD dErr = GetLastError() ;
fResult = CryptImportKey(hProv, pbKeyBlob , dwBlobLen,
hPublicKey, CRYPT_EXPORTABLE, &m_hcryptSessionKey);
dErr = GetLastError() ;
BYTE ivarray[] = {1,1,1,1,1,1,1,1};
fResult = CryptSetKeyParam(m_hcryptSessionKey , KP_IV , ivarray ,0) ;
DWORD dwMode = CRYPT_MODE_ECB;
fResult =CryptSetKeyParam(m_hcryptSessionKey, KP_MODE, (BYTE*)&dwMode, 0) ;
DWORD dwCount = 0 ;
dwMode = PKCS5_PADDING ;
fResult =CryptSetKeyParam(m_hcryptSessionKey , KP_PADDING , (BYTE*)&dwMode , 0 );

}
int CEncryption::EncryptData(LPSTR inStr , DWORD &dwLen , CString & outStr)
{
// convert string to bytes
DWORD dataLen = dwLen ;
if (dwLen == 0) return 0 ;
if (inStr == NULL) return 0 ;
BOOL bResult = CryptEncrypt(m_hcryptSessionKey ,0,TRUE , 0, NULL , &dwLen , dataLen) ;

BYTE *encTxt = new BYTE [dwLen] ;
memset(encTxt ,'\0' ,dwLen) ;
memcpy (encTxt ,inStr , dataLen) ;

//dwLen = dataLen ;

bResult = CryptEncrypt(m_hcryptSessionKey ,0,TRUE , 0, encTxt , &dataLen , dwLen) ;
delete encTxt;
return bResult;
}

int CEncryption::DecryptData(LPSTR inStr , DWORD &dLen , CString & outStr)
{
DWORD dataLen = 0 ;
BYTE *outBytes = NULL ;
__try
{

outBytes = new BYTE [dLen ];
memset(outBytes ,'\0' ,Len);
dataLen = Len ;
BOOL bResult = CryptDecrypt(m_hcryptSessionKey ,0,TRUE , 0, outBytes , &dataLen) ;
DWORD dErr = GetLastError() ;
outBytes[dataLen] = '\0' ;
outStr = (char *)outBytes ;
}
return TRUE;

}

vb.net code

Dim iv As Byte() = {1, 1, 1, 1, 1, 1, 1, 1}
Dim key As Byte() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
dim rc2CSP As new RC2CryptoServiceProvider
rc2CSP.iv = iv
rc2CSP.key = key
// encryption
Dim encrypted() As Byte
Dim toEncrypt() As Byte

Console.WriteLine("Effective key size is {0} bits.", rc2CSP.EffectiveKeySize)
Dim keysize As Integer = rc2CSP.EffectiveKeySize

'Create a new key and initialization vector.

'Get an encryptor.
Dim encryptor As ICryptoTransform = rc2CSP.CreateEncryptor(key, iv)

'Encrypt the data.
Dim msEncrypt As New MemoryStream
Dim csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)

'Convert the data to a byte array.
'toEncrypt = textConverter.GetBytes(InnerString)
toEncrypt = plainText

'Write all data to the crypto stream and flush it.
csEncrypt.Write(toEncrypt, 0, toEncrypt.Length)
csEncrypt.FlushFinalBlock()

'Get encrypted array of bytes.
encrypted = msEncrypt.ToArray()



' Destruct classes
csEncrypt.Close()
csEncrypt = Nothing
msEncrypt.Flush()
msEncrypt.Close()
msEncrypt = Nothing

// decryption

Dim decryptor As ICryptoTransform = rc2CSP.CreateDecryptor(key, iv)

'Now decrypt the previously encrypted message using the decryptor
' obtained in the above step.
Dim msDecrypt As New MemoryStream(cipherText)
Dim csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)
Dim fromEncrypt As Byte()

fromEncrypt = New Byte(cipherText.Length) {}

'Read the data out of the crypto stream.
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length)


csDecrypt.Close()
csDecrypt = Nothing
msDecrypt.Close()
msDecrypt = Nothing

Any help in pointing me in the right direction will be highly appreciated
GeneralCDaoDatabase and CDaoRecordset for MS Access 2000 Pin
DionChen23-Sep-03 8:00
DionChen23-Sep-03 8:00 
GeneralRe: CDaoDatabase and CDaoRecordset for MS Access 2000 Pin
JWood23-Sep-03 9:57
JWood23-Sep-03 9:57 
GeneralRe: CDaoDatabase and CDaoRecordset for MS Access 2000 Pin
RChin23-Sep-03 10:14
RChin23-Sep-03 10:14 
Generalcreating menus in VC++ dialog application Pin
karteek23-Sep-03 7:39
karteek23-Sep-03 7:39 
GeneralRe: creating menus in VC++ dialog application Pin
David Crow23-Sep-03 8:55
David Crow23-Sep-03 8:55 
QuestionGetClientRect with initial window wrong? Pin
Harco23-Sep-03 6:42
Harco23-Sep-03 6:42 
AnswerRe: GetClientRect with initial window wrong? Pin
cje23-Sep-03 9:00
cje23-Sep-03 9:00 
GeneralMFC ODBC Consumer Wizard Crashes Pin
Matt Gates23-Sep-03 6:22
Matt Gates23-Sep-03 6:22 
General[Message Deleted] Pin
imdx8023-Sep-03 6:05
imdx8023-Sep-03 6:05 
GeneralRe: Iterating Connection Names, Pin
Mike Dimmick23-Sep-03 7:32
Mike Dimmick23-Sep-03 7:32 
Generalwin95 global memory Pin
umarcool23-Sep-03 5:24
umarcool23-Sep-03 5:24 
Generalmenu Pin
sardinka23-Sep-03 4:31
sardinka23-Sep-03 4:31 
GeneralRe: menu Pin
RobJones23-Sep-03 4:56
RobJones23-Sep-03 4:56 
GeneralRe: menu Pin
sardinka23-Sep-03 5:15
sardinka23-Sep-03 5:15 
GeneralRe: menu Pin
RobJones23-Sep-03 5:56
RobJones23-Sep-03 5:56 
GeneralRe: menu Pin
sardinka23-Sep-03 6:34
sardinka23-Sep-03 6:34 
GeneralRe: menu Pin
David Crow23-Sep-03 7:06
David Crow23-Sep-03 7:06 

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.