Here's my code. i couldn't make it shorter :)
As you can see I fill a private key blob with my previously generated RSA
keys. I write this keyBLOB to a file. Then I crypt a msg with the keyBLOB and
decrypt it. the result of encryption is always different.
Thanks you for help
bool WriteKeyToFile()
{
HCRYPTKEY hKey = 0;
HCRYPTPROV hProv = 0;
PBYTE pbKeyBlob = NULL;
DWORD mod_size = 128;
DWORD publicExponent = 65537;
unsigned char *modulus = new unsigned char[128];
unsigned char *prime1 = new unsigned char[64];
unsigned char *prime2 = new unsigned char[64];
unsigned char *exponent1 =new unsigned char[64];
unsigned char *exponent2 = new unsigned char[64];
unsigned char *coefficient = new unsigned char[64];
unsigned char *privateExponent = new unsigned char[128];
pbKeyBlob = (unsigned char*)malloc(sizeof(PUBLICKEYSTRUC) +
sizeof(RSAPUBKEY) + mod_size + (mod_size/2 * 7));
unsigned char *pKeybuf;
pKeybuf = (pbKeyBlob) + sizeof(PUBLICKEYSTRUC) + sizeof(RSAPUBKEY) ;
RSAPUBKEY *pRsaPubKey = 0;
BLOBHEADER *blobHeader;
blobHeader = (PUBLICKEYSTRUC*)pbKeyBlob;
blobHeader->bType = PRIVATEKEYBLOB;
blobHeader->bVersion = 0x02;
blobHeader->reserved = 0;
blobHeader->aiKeyAlg = CALG_RSA_KEYX;
pRsaPubKey = (RSAPUBKEY*)(((unsigned char*)pbKeyBlob) +
sizeof(PUBLICKEYSTRUC));
pRsaPubKey->magic = 0x32415352;
pRsaPubKey->bitlen = mod_size * 8;
pRsaPubKey->pubexp = publicExponent;
FILE *fKeys = fopen("\\KEYS", "rb");
unsigned char* bufferKeys = new unsigned char[576];
fread(bufferKeys, 1,576 , fKeys);
memcpy(modulus, bufferKeys, 128);
bufferKeys += 128;
memcpy(prime1,bufferKeys,64);
bufferKeys += 64;
memcpy(prime2,bufferKeys,64);
bufferKeys += 64;
memcpy(exponent1,bufferKeys,64);
bufferKeys += 64;
memcpy(exponent2,bufferKeys,64);
bufferKeys += 64;
memcpy(coefficient,bufferKeys,64);
bufferKeys += 64;
memcpy(privateExponent,bufferKeys, 128);
bufferKeys += 128;
memcpy(pKeybuf, modulus, 128);
bufferKeys += 128;
memcpy(pKeybuf,prime1,64);
bufferKeys += 64;
memcpy(pKeybuf,prime2,64);
bufferKeys += 64;
memcpy(pKeybuf,exponent1,64);
bufferKeys += 64;
memcpy(pKeybuf,exponent2,64);
bufferKeys += 64;
memcpy(pKeybuf,coefficient,64);
bufferKeys += 64;
memcpy(pKeybuf,privateExponent, 128);
bufferKeys += 128;
DWORD dwBlobLen = sizeof(PUBLICKEYSTRUC) + sizeof(RSAPUBKEY) + mod_size +
(mod_size/2 * 7);
if(!CryptAcquireContext(&hProv, MS_ENHANCED_PROV , NULL, PROV_RSA_FULL, 0))
return false;
if(!CryptImportKey(hProv, pbKeyBlob,
sizeof(PUBLICKEYSTRUC)+sizeof(RSAPUBKEY)+mod_size+ (mod_size/2 * 7), 0,0,
&hKey))
return false;
FILE *fIn = fopen("\\BLOBKey", "wb+");
if (fIn == NULL)
return false;
fwrite(pbKeyBlob, 1, dwBlobLen, fIn);
if((hKey != 0) && (hProv != 0))
{
if(!CryptDestroyKey(hKey))
return false;
if(!CryptReleaseContext(hProv, 0))
return false;
}
fclose(fIn);
return true;
}
bool CryptDecrypt()
{
HCRYPTPROV hProv;
HCRYPTKEY hKey;
DWORD dwBlobLen = 596; // length of PRIVATEKEYBLOB
BYTE *pbKeyBlob = NULL;
BYTE[128] = "123456789012345678901234";
if ((pbKeyBlob = (PBYTE)malloc(dwBlobLen))==NULL)
return false;
FILE *fIn = fopen("\\BLOBKey", "rb");
fread(pbKeyBlob, 1,dwBlobLen , fIn);
if(!CryptAcquireContext(&hProv, MS_ENHANCED_PROV, NULL, PROV_RSA_FULL, 0))
return false;
if(!CryptImportKey(hProv, pbKeyBlob, dwBlobLen, 0, 0, &hKey))
return false;
dwDataLen = 24;
if(!CryptEncrypt(hKey, 0,TRUE, 0, AESKey, &dwDataLen, (DWORD)sizeof(AESKey)))
return false;
if (!CryptDecrypt(hKey, 0, TRUE, 0, AESKey, &dwDataLen))
return false;
if((hKey != 0) && (hProv != 0))
{
if(!CryptDestroyKey(hKey))
return false;
if(!CryptReleaseContext(hProv, 0))
return false;
}
fclose(fIn);
fclose(fIn1);
return false;
}
"Jon Skeet [C# MVP]" wrote:
<"=?Utf-8?B?b3RobWFuIHRhag==?=" <othman
ta*@discussions.microsoft.com>> wrote: In Fact, I'm using always the same key, ie I import my key and crypt with
this one. Also the initialization vector isn't related to the asymetric
encryption scheme (I'm using RSA). So why cryptEncrypt returns different
ciphertexts with the same key used, each time ?
Could you post a short but complete program which demonstrates the
problem?
See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too