Skip to content

Instantly share code, notes, and snippets.

@adolfoabegg
Last active January 12, 2018 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adolfoabegg/51f075e4eb80b5869ccfa1102c9ac52f to your computer and use it in GitHub Desktop.
Save adolfoabegg/51f075e4eb80b5869ccfa1102c9ac52f to your computer and use it in GitHub Desktop.
Vendo's encryption and decryption methods.
<?php
namespace Vendo;
/**
* Usage:
* <?php
* include '/path/to/vendo_encryption_decryption_methods.php';
*
* $plainTextUserPassword = 'the plain text password goes here';
* $vendoSharedSecret = 'the shared secret';
*
* //encrypting the password:
* $encryptedPasswordBinaryData = \Vendo\Encryption::aes128Encrypt($plainTextUserPassword, $vendoSharedSecret);
* $encryptedPassword = bin2hex($encryptedPasswordBinaryData);
* //pass $encryptedPassword to Vendo
*
*
* //In case you need to decrypt the password:
* $encryptedPasswordBinaryData = pack('H*', $encryptedPassword);
* $decryptedPassword = \Vendo\Encryption::aes128Decrypt($encryptedPasswordBinaryData, $vendoSharedSecret);
* //$decryptedPassword contains the plain text password
* ?>
*
*/
class Encryption
{
const METHOD = 'AES-128-ECB';
/**
* Encrypts a string using AES128.
* This method generates exactly the same output as MySQL's AES_ENCRYPT() function.
*
* @param string $uncryptedData
* @param string $key The key to use in the encryption.
* @return string The result is binary!
*/
public static function aes128Encrypt($uncryptedData, $key)
{
$ivSize = openssl_cipher_iv_length(self::METHOD);
$iv = openssl_random_pseudo_bytes($ivSize);
$encryptedData = openssl_encrypt($uncryptedData, self::METHOD, $key, OPENSSL_RAW_DATA, $iv);
return $encryptedData;
}
/**
* Decrypts a string that was encrypted using AES128
* This method generates exactly the same output as MySQL's AES_DECRYPT() function.
*
* @param string $encryptedData
* @param string $key
* @return string
*/
public static function aes128Decrypt($encryptedData, $key)
{
$ivSize = openssl_cipher_iv_length(self::METHOD);
$iv = openssl_random_pseudo_bytes($ivSize);
$decryptedData = openssl_decrypt($encryptedData, self::METHOD, $key, OPENSSL_RAW_DATA, $iv);
return $decryptedData;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment