17 rows Oct 24, 2019 zend-crypt Repository abandoned 2019-12-31. This repository has moved to.
![]()
Encrypt/decrypt using block ciphersZendCryptBlockCipher implements the encrypt-then-authenticate mode using to provide authentication.The symmetric cipher can be choose with a specific adapter that implements theZendCryptSymmetricSymmetricInterface. We support the standard algorithms of the extension. Theadapter that implements the Mcrypt is ZendCryptSymmetricMcrypt.In the following code we reported an example on how to use the BlockCipher class to encrypt-then-authenticate astring using the block cipher (with a key of 256 bit) and the HMAC algorithm (using the hashfunction). Use ZendCryptBlockCipher; $blockCipher = BlockCipher:: factory ( 'mcrypt', array ( 'algo' = 'aes' )); $blockCipher - setKey ( 'encryption key' ); $result = $blockCipher - encrypt ( 'this is a secret message' ); echo 'Encrypted text: $result n ';The BlockCipher is initialized using a factory method with the name of the cipher adapter to use (mcrypt) and theparameters to pass to the adapter (the AES algorithm). In order to encrypt a string we need to specify anencryption key and we used the setKey method for that scope. The encryption is provided by the encryptmethod.The output of the encryption is a string, encoded in Base64 (default), that contains the HMAC value, the IV vector,and the encrypted text.
The encryption mode used is the (with a random by default) and SHA256 as defaulthash algorithm of the HMAC.The Mcrypt adapter encrypts using the mechanism by default. You can specify a different paddingmethod using a special adapter for that (ZendCryptSymmetricPadding). The encryption and authentication keysused by the BlockCipher are generated with the algorithm, used as key derivation function from theuser’s key specified using the setKey method. NoteRecommendationIf you are not familiar with symmetric encryption techniques we strongly suggest to use the default values ofthe BlockCipher class.
![]()
The default values are: AES algorithm, CBC mode, HMAC with SHA256, PKCS#7 padding.To decrypt a string we can use the decrypt method. In order to successfully decrypt a string we have toconfigure the BlockCipher with the same parameters of the encryption.We can also initialize the BlockCipher manually without use the factory method. We can inject the symmetric cipheradapter directly to the constructor of the BlockCipher class. For instance, we can rewrite the previous example asfollow.
Edit this documentThe source code of this file is hosted on GitHub. Everyone canupdate and fix errors in this document with few clicks -no downloads needed.Login with your account.Go toon GitHub.Edit file contents using GitHub's text editor in your web browser.Fill in the Commit message text box at the end of the page telling whyyou did the changes.
Press Propose file change button next to it when done.On Send a pull request page you don't need to fill in text anymore. Justpress Send pull request button.Your changes are now queued for review under project's tab on GitHub.
Encrypt/decrypt using block ciphersZendCryptBlockCipher implements the encrypt-then-authenticate mode using to provide authentication.The symmetric cipher can be choose with a specific adapter that implements theZendCryptSymmetricSymmetricInterface. We support the standard algorithms of the extension. Theadapter that implements the Mcrypt is ZendCryptSymmetricMcrypt.In the following code we reported an example on how to use the BlockCipher class to encrypt-then-authenticate astring using the block cipher (with a key of 256 bit) and the HMAC algorithm (using the hashfunction).
Use ZendCryptBlockCipher; $blockCipher = BlockCipher:: factory ( 'mcrypt', array ( 'algo' = 'aes' )); $blockCipher - setKey ( 'encryption key' ); $result = $blockCipher - encrypt ( 'this is a secret message' ); echo 'Encrypted text: $result n ';The BlockCipher is initialized using a factory method with the name of the cipher adapter to use (mcrypt) and theparameters to pass to the adapter (the AES algorithm). In order to encrypt a string we need to specify anencryption key and we used the setKey method for that scope. The encryption is provided by the encryptmethod.The output of the encryption is a string, encoded in Base64 (default), that contains the HMAC value, the IV vector,and the encrypted text. The encryption mode used is the (with a random by default) and SHA256 as defaulthash algorithm of the HMAC.The Mcrypt adapter encrypts using the mechanism by default. You can specify a different paddingmethod using a special adapter for that (ZendCryptSymmetricPadding). The encryption and authentication keysused by the BlockCipher are generated with the algorithm, used as key derivation function from theuser’s key specified using the setKey method.
NoteRecommendationIf you are not familiar with symmetric encryption techniques we strongly suggest to use the default values ofthe BlockCipher class. The default values are: AES algorithm, CBC mode, HMAC with SHA256, PKCS#7 padding.To decrypt a string we can use the decrypt method.
In order to successfully decrypt a string we have toconfigure the BlockCipher with the same parameters of the encryption.We can also initialize the BlockCipher manually without use the factory method. We can inject the symmetric cipheradapter directly to the constructor of the BlockCipher class. For instance, we can rewrite the previous example asfollow. Edit this documentThe source code of this file is hosted on GitHub. Everyone canupdate and fix errors in this document with few clicks -no downloads needed.Login with your account.Go toon GitHub.Edit file contents using GitHub's text editor in your web browser.Fill in the Commit message text box at the end of the page telling whyyou did the changes.
Press Propose file change button next to it when done.On Send a pull request page you don't need to fill in text anymore. Justpress Send pull request button.Your changes are now queued for review under project's tab on GitHub.
![]() Comments are closed.
|
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
March 2023
Categories |