PHP uses OpenSSL for RSA Long Data Encryption (117) decryption (128) and DES encryption decryption

Source: Internet
Author: User
Tags decrypt

PHP uses OpenSSL for RSA encryption, if the plaintext to be encrypted is too long error, workaround: Encrypt the time 117 characters to encrypt once, and then all the cipher stitching into a cipher text, the decryption time needs 128 characters to decrypt, and then splicing into data.

Encryption:

/** *     Encryption     * @param $originalData     * @return string|void     *    //*function Encrypt ($originalData) {        if (Openssl_private_encrypt ($originalData, $encryptData, $this->rsaprivatekey)) {        if (openssl_public_ Encrypt ($originalData, $encryptData, $this->rsapublickey)) {            return Base64_encode ($encryptData);        } else {            return false;        }    } *    /function Encrypt ($originalData) {        $crypto = ';        foreach (Str_split ($originalData, 117) as $chunk) {            openssl_public_encrypt ($chunk, $encryptData, $this, Rsapublickey);            $crypto. = $encryptData;        }        Return Base64_encode ($crypto);    }

Decrypt:

/**     * Private key decryption     * @param $encryptData *    //*function Decrypt ($encryptData) {        //if (openssl_public_ Decrypt (Base64_decode ($encryptData), $decryptData, $this->rsapublickey)) {        if (Openssl_private_decrypt ( Base64_decode ($encryptData), $decryptData, $this->rsaprivatekey)) {            return $decryptData;        } else {            return false;        }    } *    /function Decrypt ($encryptData) {        $crypto = ';        foreach (Base64_decode ($encryptData), str_split) as $chunk) {            openssl_private_decrypt ($chunk, $decryptData, $this->rsaprivatekey);            $crypto. = $decryptData;        }        return $crypto;    }

DES Encryption and decryption:

function Encrypt ($encrypt, $key) {$passcrypt = Mcrypt_encrypt (Mcrypt_des, $key, $encrypt, MCRYPT_MODE_CBC, $key); $ encode = Base64_encode ($passcrypt); return $encode;} function Decrypt ($decrypt, $key) {$decoded = Base64_decode ($decrypt); $decrypted = Mcrypt_decrypt (Mcrypt_des, $key, $ Decoded, MCRYPT_MODE_ECB, $key); return $decrypted;}

AES Encryption Decryption

Create random initial vectors for CBC mode $iv_size = Mcrypt_get_iv_size (mcrypt_rijndael_128, MCRYPT_MODE_CBC); $iv = Mcrypt_create_iv ($iv _ Size, Mcrypt_rand), function Aesencrypt ($encrypt, $key, $iv) {$passcrypt = Mcrypt_encrypt (mcrypt_rijndael_128, $key, $ Encrypt, MCRYPT_MODE_CBC, $IV);//append the initial vector to the ciphertext for decryption using $passcrypt = $iv. $passcrypt;//base64 encoding of ciphertext $encode = Base64_encode ($passcrypt); return $encode;} function Aesdecrypt ($decrypt, $key, $iv _size) {$decoded = Base64_decode ($decrypt);//initial vector size, can be mcrypt_get_iv_size () To obtain $iv_dec = substr ($decoded, 0, $iv _size);//Gets the ciphertext except the initial vector $decoded = substr ($decoded, $iv _size);//may need to be removed from the end of the clear text 0$ decrypted = Mcrypt_decrypt (mcrypt_rijndael_128, $key, $decoded, MCRYPT_MODE_ECB, $iv _dec); return $decrypted;}

  

PHP uses OpenSSL for RSA Long Data Encryption (117) decryption (128) and DES encryption decryption

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.