InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Sharing Encrypted Data with Another Website in PHP
PHP

Sharing Encrypted Data with Another Website in PHP

InfinityCoder December 21, 2016

You want to exchange data securely with another website.

If the other website is pulling the data from your site, put the data up on a passwordprotected page. You can also make the data available in encrypted form, with or without a password.

If you need to push the data to another website, submit the potentially encrypted data via post to a password-protected URL.

The following page requires a username and password and then encrypts and displays the contents of a file containing yesterday’s account activity:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$user = 'bank';
$password = 'fas8uj3';
 
if ($_SERVER['PHP_AUTH_USER'] != $user ||
    $_SERVER['PHP_AUTH_PW'] != $password) {
    header('WWW-Authenticate: Basic realm="Secure Transfer"');
    header('HTTP/1.0 401 Unauthorized');
    echo "You must supply a valid username and password for access.";
    exit;
}
 
header('Content-type: text/plain; charset=UTF-8');
$filename = strftime('/usr/local/account-activity.%Y-%m-%d', time() - 86400);
$data = implode('', file($filename));
 
$algorithm   = MCRYPT_BLOWFISH;
$mode = MCRYPT_MODE_CBC;
$key    = "There are many ways to butter your toast.";
 
/* Encrypt data. */
$iv = mcrypt_create_iv(mcrypt_get_iv_size($algorithm, $mode),
                       MCRYPT_DEV_URANDOM);
$ciphertext = mcrypt_encrypt($algorithm, $key, $data, $mode, $iv);
 
echo base64_encode($iv.$ciphertext);

Here’s the corresponding code to retrieve the encrypted page and decrypt the information:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$user = 'bank';
$password = 'fas8uj3';
$algorithm = MCRYPT_BLOWFISH;
$mode = MCRYPT_MODE_CBC;
 
$key = "There are many ways to butter your toast.";
 
$url = 'https://bank.example.com/accounts.php';
$c = curl_init($url);
curl_setopt($c, CURLOPT_USERPWD, "$user:$password");
curl_setopt($c, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($c);
if (FALSE === $data) {
   exit("Transfer failed: " . curl_error($c));
}
 
$binary_data = base64_decode($data);
$iv_size = mcrypt_get_iv_size($algorithm, $mode);
$iv = substr($binary_data, 0, $iv_size);
$ciphertext = substr($binary_data, $iv_size, strlen($binary_data));
 
echo mcrypt_decrypt($algorithm, $key, $ciphertext, $mode, $iv);

The retrieval program does all the steps of the encryption program, but in reverse. It retrieves the Base64-encoded encrypted data, supplying a username and password.
Then, it decodes the data with Base64 and separates out the initialization vector. Last, it decrypts the data and prints it out.
In the previous examples, the username and password are still sent over the network in clear text, unless the connections happen over SSL.

However, if you’re using SSL, it’s probably not necessary to encrypt the contents of the file. We included both passwordprompting and file encryption in these examples to show how it can be done.
There’s one circumstance, however, in which both password protection and file encryption is helpful: if the file isn’t automatically decrypted when it’s retrieved.

An automated program can retrieve the encrypted file and put it, still encrypted, in a place that can be accessed later.

The decryption key thus doesn’t need to be stored in the retrieval program.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Defining Object Stringification in PHP
You want to control how PHP displays an object when …

Defining Object Stringification in PHP

Removing a Directory and Its Contents in PHP
You want to remove a directory and all of its …

Removing a Directory and Its Contents in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap