Vinay
Vinay

Reputation: 63

PHP data encryption and decryption

I am trying to encrypt data using php and insert into mysql. Encryption and insert actions working properly but decryption does not return actual string. Please see my code below for encryption

public function encryptText($text,$customer_id)
    {
        $key = $customer_id;
        $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB);
        return $crypttext;
    }

For decryption

public function decryptText($ctext,$customer_id)
    {
            $key = $customer_id;
            $text = mcrypt_decrypt(MCRYPT_RIJNDAEL_256,$key,$ctext,MCRYPT_MODE_ECB);
            return $text;
    }

Please help me to solve this issue

Upvotes: 2

Views: 2790

Answers (2)

Jon
Jon

Reputation: 437694

The most likely problem is that you are not using the correct key to decrypt the encrypted data. Your code shows a number of issues really look into:

  • The key should ideally be a binary string. What are the exact contents of $customer_id? Even if that is a string, it should be exactly either 128, 192, or 256 bits long. It doesn't look like it is.
  • Even if the key were technically acceptable, using a customer id as a key does not really offer any security at all.
  • The 256 in MCRYPT_RIJNDAEL_256 does not specify the encryption strength but the block size. In almost all cases you should use MCRYPT_RIJNDAEL_128 instead -- in fact doing this is the same as AES. MCRYPT_RIJNDAEL_256 is not AES.

Upvotes: 1

Petah
Petah

Reputation: 46060

These functions will take any PHP object and encrypt/decrypt them:

Encrypt JSON object Rijndael ECB base 64 encode

function ejor2eb($object, $key) {
    // Encode the object
    $object = json_encode($object, JSON_FORCE_OBJECT);

    // Add length onto the encoded object so we can remove the excess padding added by AES
    $object = strlen($object) . ':' . $object;

    // Encrypt the string
    $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
    $result = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $object, MCRYPT_MODE_ECB, $iv);

    // Return the URL encoded string, with the encryption type attached
    return 'jor2eu:' . base64_encode($result);
}

Decrypt JSON object Rijndael ECB base 64 decode

function djor2eb($string, $key, $default = false) {
    // Remove the encryption type, and decode the string
    $binary = base64_decode(substr($string, 7));
    if (!$binary) {
        return $default;
    }

    // Decrypt the string
    $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
    $result = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $binary, MCRYPT_MODE_ECB, $iv);

    // Remove encrption padding
    $tokens = null;
    preg_match('/^([0-9]+):/i', $result, $tokens);
    if (sizeof($tokens) !== 2) {
        return $default;
    }
    $result = substr($result, strlen($tokens[1]) + 1, $tokens[1]);

    // Decode the ecoded object
    $object = json_decode($result);

    return $object;
}

Upvotes: 0

Related Questions