Does CommonCrypto reliably know when my key is invalid when I decrypt a ciphertext?

StackOverflow https://stackoverflow.com/questions/7846384

  •  10-02-2021
  •  | 
  •  

سؤال

When I try to decrypt a cipher text using the wrong key, CCCrypt returns kCCDecodeError.

Question is, does it do so reliably (eg. am I guaranteed that if it returns success, my input key was the key used to encrypt the plain text, and am I also guaranteed that my output data is my original plain text) and how can it even know whether my key is correct or not?

As far as I understood crypto, the engine cannot predict whether the key is valid and should just give me random noise as output data and a successful return code.

هل كانت مفيدة؟

المحلول

If you specified PCKS7 padding (kCCOptionPKCS7Padding) then it can tell if you failed to decrypt it properly - most of the time. There is a chance that the random result of the wrong key could cause the last bits of the message to look like valid PKCS7 padding.

The only other thing it can detect is if your key is not a valid length at all.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top