开发者

Encryption: Testing that a string is properly decrypted?

开发者 https://www.devze.com 2023-02-12 22:37 出处:网络
Here\'s a theoretical one that not only applies to PHP, but probably to more languages. Let\'s say that I encrypt a string with the mcrypt library using and the AES-256 cipher. The string, encrypted,

Here's a theoretical one that not only applies to PHP, but probably to more languages.

Let's say that I encrypt a string with the mcrypt library using and the AES-256 cipher. The string, encrypted, would now look similar to þøÆ{”ò(ü´î开发者_StackOverflowÚÜÇW¹ËŸK­¯L‘rø?ª¶!JF£­º+Œ’Ú'‚.

If the encryption key would change between the events of decryption and encryption, the result of the decryption would obviously be worthless.

Since an encrypted string contains, at least to me, random chars, It wouldn't be easy to run some sort of test on it to ensure that it is in encrypted/decrypted state.

I've spent some time thinking. How can I test that a string has been properly decrypted?

What if I appended a small prefix to the original string before encrypting it in the first place, and then removed this prefix upon decryption. If this prefix wasn't found, it would be safe to say that the decryption has failed.

Would this be an appropriate way to handle this?


To test data integrity you want a Message Authentication Code (MAC).

There are a few stand-alone MAC algorithms, which look like a hash function with a key. The very standard MAC algorithm is HMAC (which uses a hash function).

Since you also encrypt the data, you will want to use an encryption mode with a builtin MAC; there are a few such modes, such as GCM or EAX. Those modes apply to a block cipher, usually the AES.

Adding a known prefix or suffix to the data before encryption is a homemade MAC. MACs are subtle and error prone. For instance, if you add a CRC32 and then encrypt with a stream cipher (or a block cipher in CTR mode), then you are reproducing one of the seven capital sins of WEP (see section 4 in particular, for the CRC32-as-MAC issue). Basically your integrity check is no longer resistant to active attacks; you are only detecting innocent mistakes, such as using the wrong key.

(Unfortunately, it seems that MCrypt does not support any combined encryption/MAC mode. PHP itself, when compiled with the --with-mhash option, provides the mhash() function which implements both raw hashing, and HMAC.)


How can I test that a string has been properly decrypted?

The "small prefix" idea should be fine; also the excellent idea by @CodeInChaos. Other than that, storing the string in some defined format (like serialize() or json_encode()) and failing to restore it (unserialize(), json_decode()) would be indication of a broken decryption as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消