Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8459943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:28:08+00:00 2026-06-10T13:28:08+00:00

I want to use openssl_public_encrypt to encrypt data and store it in a MySQL

  • 0

I want to use openssl_public_encrypt to encrypt data and store it in a MySQL database. Then, when I need the data, I want to SELECT it from the MySQL database and use openssl_private_decrypt to decrypt it. After it is encrypted I am using base64 to encode the data for MySQL and again to decode the data before decrypting it.

Here is the code I am using to encrypt the data and INSERT it in the database:

<?php
    require_once('default.inc.php');
    mysql_connect_selectdb($host,$username,$password,$database);
    function encrypt($string) {
    $publickey = '-----BEGIN PUBLIC KEY-----
    PUBLIC KEY HERE
    -----END PUBLIC KEY-----';
        openssl_public_encrypt($string,$encryptedstring,$publickey);
        return $encryptedstring;
    }
$number = base64_encode(encrypt('1234567890123456'));
$qry = "UPDATE table SET number='$number' WHERE id='120006'";
$result = mysql_query($qry);
if (!$result) {
    die(mysql_error());
}
?>

The default.inc.php file sets error reporting to E_ALL and also defines the mysql connect variables and the mysql_connect_selectdb() function. It inserts into the database fine and there is no output to the screen.

Here is the code I am using to SELECT it from the database and decrypt it:

<?php
require_once('default.inc.php');
mysql_connect_selectdb($host,$username,$password,$database);
$key = '-----BEGIN RSA PRIVATE KEY-----
PRIVATE KEY HERE
-----END RSA PRIVATE KEY-----';
function decrypt($string,$privatekey) {
    openssl_private_decrypt($string,$decryptedstring,$privatekey);
    return $decryptedstring;
}
$qry = "SELECT * FROM table WHERE id='120006'";
$result = mysql_query($qry);
if ($result) {
    if (mysql_numrows($result) == '1') {
        $data = mysql_fetch_assoc($result);
        $number = decrypt(base64_decode($data['number']),$key);
        echo 'Number: '.$number;
    }
    else {
    echo 'no rows';
}
}
else {
    die(mysql_error());
}
?>

There is no output to the screen other than Number:. I can’t figure out why it doesn’t decrypt. I can echo the $data['number'] straight from the database and it shows up fine as base 64 encoded data and also I can echo the data after it has been base 64 decoded and it shows up fine as binary on my screen, however when I try to decrypt it there is no output.

The MySQL field number is VARCHAR(128) (have tried VARBINARY, BINARY and BLOB), VARCHAR should be fine for base64, and it is a latin1_swedish_ci collation.

Any help would be greatly appreciated! Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:28:10+00:00Added an answer on June 10, 2026 at 1:28 pm

    I figured it out. The column number was a VARCHAR(128) however the length of the string being inserted was 172, so therefore the end of the string was cut off. Without the end of the string, it didn’t decrypt properly and returned NULL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want use some data from a website with web service. i have a
i want to encrypt and decrypt strings in my c++ appication and use openssl
Like in the title, I want to encrypt data, which is sent over network.
In an application I want to store sensitive data (which can be arbitrarily large)
I'm trying to use PHP and OpenSSL to encrypt some data using a public
I want to use Perl to extract information from a Certificate Signing Request ,
I use OpenSSL to encryt a string. After that I want to encode the
EDIT (the whole question, it was too unclear) I want to use OpenSSL.NET The
I want to use the following command: openssl x509 -noout -in /etc/pki/tls/certs/cert1.pem -enddate openssl
I want use this 1 for using Bar code or QR code scanner. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.