RE: CryptDecrypt fails with NTE_BAD_DATA
- From: lelteto <lelteto@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 5 Jun 2006 09:19:02 -0700
As Sylvain explained you are completely misusing the crypto. Not only that,
but what you do is completely inefficient (and also dangerous - giving out
too much cleartext / ciphertext pairs).
This is how it SHOULD be done:
1. you have private key Kpr (recipient) and public key Kpu (sender) and also
the message M (arbitrary large)
2. The sender should generate a RANDOM SESSION KEY Ks for some agreead-upon
crypto (if both sides XP than AES would be fine; otherwise you can pick some
other crypto like DES3 or RC4) - use CryptGenKey
3. You ecnrypt the message with the SESSION Key iwth CryptEncrypt
M'=encr(M, Ks)
4. you encrypt the session key with the public key
Ks'=encr(Ks, Kpu)
(since Ks is relatively small you won't run into problem with too large data
vs. the modulus value; moreover you would use the CryptExportKey function
which does everything for your)
5. the sender sends M' and Ks' to the recipient
At the recipient side
1. decrypt the session key (use CryptImportKey)
2. decrypt the message with the session key using CryptDecrypt
M = decr(M', Ks)
I recommend you to read some crypto BOOKs...
Laszlo Elteto
SafeNet, Inc.
"yakov.b@xxxxxxxxx" wrote:
Hi,.
I use a RSA key of 1024 bits.
The encrypter has only the public blob and the decrypter has the
private blob.
The 2 machines are win xp pro sp2.
I encrypt the data in chunks of 117 bytes that become 128 bytes.
When CryptDecrypt fails, it is always NTE_BAD_DATA and it happens in
about 1% of all cases, but when it happens all the chunks of the data
are NTE_BAD_DATA.
I'm 100% sure that it is not some flaw in my program because all
messages before and after the failed message are okay and I've ran it
debug on good and bad messages and nothing changed.
Furthermore, when this error occurs the data is re-encrypted and then
it decrypts okay!
Of course the encrypted data is now different - it encrypts it
differently every time.
Now, has anyone ever seen this phenomena?
I've searched alot but could find only padding issues and such - this
is not the case as I don't have to do anything like that.
Another thing I just thought about right now - can I affect the
encryption by setting something in the extra 11 bytes? - they are
uninitialized now.. will zero them tommorow.
Thanks in advance,
Yakov
- Follow-Ups:
- References:
- CryptDecrypt fails with NTE_BAD_DATA
- From: yakov . b
- CryptDecrypt fails with NTE_BAD_DATA
- Prev by Date: How to receive request from HTTPS server
- Next by Date: Re: SSL session id
- Previous by thread: Re: CryptDecrypt fails with NTE_BAD_DATA
- Next by thread: Re: CryptDecrypt fails with NTE_BAD_DATA
- Index(es):
Relevant Pages
|