473,385 Members | 1,893 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,385 software developers and data experts.

cryptEncrypt never returns the same ciphertext twice

Hi everyone,

I want to encrypt and decrypt a message. I'm using RSA encryption/decryption with PRIVATEKEYBLOB, no hash, and everything works fine, ie I can see my plaintext.

I notices that cryptEncrypt in cryptoAPI never returns the same ciphertext, can anyone explain me why ?
Thanks a lot,

Othman

--------------------------------
From: othman taj

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>mDmY2PMFqE+kVmaJoX3vXA==</Id>
Nov 17 '05 #1
5 4728
this is because your Initilization vector and key are are getting generated
on every call that you make. after your first call you need to rember these
two values, so that you can use them again in a later call.
Nov 17 '05 #2
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 ?

"Aaron Fischer" wrote:
this is because your Initilization vector and key are are getting generated
on every call that you make. after your first call you need to rember these
two values, so that you can use them again in a later call.

Nov 17 '05 #3
<"=?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
Nov 17 '05 #4
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

Nov 17 '05 #5
Othman <Ot****@discussions.microsoft.com> wrote:
Here's my code. i couldn't make it shorter :)


That's not C# code. (It's not complete, either, but that's another
issue.) I assumed you were writing in C#, given the newsgroup. If
you're writing in C, I suggest you use a C newsgroup.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6

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

Similar topics

1
by: Martin Lucas-Smith | last post by:
I wrote the function below as part of a larger class. The fopen stage works, and, as according to the documentation at www.php.net/fopen that succesfully creates a new file. The fwrite stage...
3
by: Faustino Dina | last post by:
Hi, The following code is from an article published in Informit.com at http://www.informit.com/guides/content.asp?g=dotnet&seqNum=142. The problem is the author says it is not a good idea to...
5
by: Al Dykes | last post by:
I've got a voting script that's a little too simple. I can vote as as many times as I can click on the link in my browser. I'n not trying to write an official voting system but I would like to...
5
by: Jonathan | last post by:
Hi-- I have the following code: #include <stdio.h> char a,b; int main()
7
by: tshad | last post by:
I thought I understood how the SaveViewState is working and was trying to use this (as per some code I found) to detect refreshes. It seemed to be working but I found that the SaveViewState was...
3
by: Ed Sonneveld | last post by:
Hi, I have hosted my webservice at a hosting company and it has been working fine for 2 years now. The webservice is called by winforms clients over the internet, using the proxy class generated...
11
by: Rimpinths | last post by:
I'm new at developing user controls in C#, and one thing I've noticed right off the bat is that the constructor gets called twice -- once at design time, once at run time. In short, I'm trying...
16
by: Akhenaten | last post by:
I must be missing something rather obvious. I have the following snippet of code that echo's my result twice when it should be echoing just once (only one element in the array). What am I...
15
by: dhtml | last post by:
Title says it. If I use a for in loop on an HTML collection, I get length twice. <!DOCTYPE HTML> <html lang="en"> <head> <title>length twice</title> </head> <body> <form...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.