|
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
|
|
|
|
|
Hi, All:
I have a MFC application developed five years ago and it used CDaoDatabase and CDaoRecordset. It works fine with MS Access 97 DB. After we converted the DB to MS Access 2000. The Application fails on the DB open. I use the following to open the DB:
CDAODatabase pDB = new CDaoDatabase();
try
{
pDB->Open( "MyDBFilewithFullPath", FALSE, FALSE, _T("") );
}
I wonder if there is any easy way to make this work with MS Access 2000. Will using ODBC fix the problem?
Thanks in advance.
Dion
|
|
|
|
|
Hi,
I'm using
RECT r;
GetClientRect(hWnd,&r);
CreateWindow(WC_EDIT,etc,etc,r.right,r.bottom....);
to get the client area of my window and putting an edit control in there which should fit precisely. I noticed that the returned value for the first window created by the application is wrong. The height is 10 pixels short(about the same height as a titlebar).
When used with any subsequently created windows the value is correct. Does anybody know what this is about? Maybe a bug?
|
|
|
|