RE: CryptDecrypt fails with NTE_BAD_DATA



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


.



Relevant Pages

  • RE: NTE_BAD_DATA
    ... They are NOT used DIRECTLY to encrypt / decrypt data; ... you should generate a RANDOM SESSION KEY and select a SYMMETRIC ENCRYPTION ... // imported from a BLOB read in from the source file or having ...
    (microsoft.public.platformsdk.security)
  • Re: Need simple lib for asymetric encryption
    ... I would like to encrypt some data with key1 and decrypt it again with ... Public key crypto is abysmally slow. ... symmetric crypto to actually encrypt the data. ... your security. ...
    (sci.crypt)
  • RE: CryptImportKey from windows services says NTE_BAD_DATA
    ... If you want to encrypt / decrypt between SYSTEM (that's the 'user' for your ... > this session key to a file.while retrieving, ... > I tried above flow with 2 applications. ...
    (microsoft.public.platformsdk.security)
  • Re: Need simple lib for asymetric encryption
    ... I would like to encrypt some data with key1 and decrypt it again with ... Public key crypto is abysmally slow. ... symmetric crypto to actually encrypt the data. ...
    (sci.crypt)
  • RE: CrpytAPI, Decryption using the Public Key
    ... using the generated key pair's private part. ... You should NOT encrypt / decrypt your data with the RSA key pair. ... wrap this session key using the RSA public key: ...
    (microsoft.public.platformsdk.security)