Archived Forum Post

Index of archived forum posts

Question:

Encrypt with Public Key Returns Zero Bytes

Jun 22 '16 at 10:02

I'm trying to understand why Encrypt stopped working. It seems it might be environmental because I know it was working, but then some time passed and when I want back to resume work on it, it no longer worked. The code hasn't changed and I don't remember any changes to the environment so I'm stumped.

The code is below It finds the cert fine, but when it calls Encrypt, it returns zero bytes when a byte array (b) is passed in. No exceptions are thrown. I'm using C#.

Cert c = store.FindCertBySubjectCN(subjectCN);  
if (c != null)  
{  
    crypt.SetEncryptCertificate(c);  
    byte[] encryptedBytes = crypt.Encrypt(b);    // returns 0 bytes  
}

Answer

Check the contents of the crypt.LastErrorText property after calling crypt.Encrypt(b);


Answer

Thanks for the reply. Here is the value from the crypt.LastErrorText property:

logged_on_user: DOR964979$
ncsp: Microsoft Base Cryptographic Provider v1.0
alg_name: RC2
alg_bits: 40
encryptOid: 1.2.840.113549.3.2
encryptData.1: Bad Length.
Failed to encrypt data

The code hasn't changed in ages and the cert is good. Why it suddenly doesn't work is baffling to me. I see that the error says Bad Length--but why? I think it has to be the environment but can't figure it out.