Using Base64 Encryption for PHP APIs
Submitted by Yorkiebar on Wednesday, July 30, 2014 - 22:50.
Introduction:
This tutorial is on how to use encryption to transfer data securely, or at least more seucrely than plain text, from a Visual Basic application (or other source) to a PHP API.
Why Cryptography?
Cryptography should be used for all sensitive information being used in public release applications, or if the data is sent cross-networks. This is because any attackers or hackers would have to work that little bit extra in order to decrypt any information they manage to receive/intercept.
PHP?
PHP has a couple of cryptography functions built in to it, including; Base64 and MD5. We will be using Base64 for this tutorial since it is decodable while MD5 is a one way encryption.
Source:
So, to decode the base 64 data in the PHP API, the receiving data must be base 64 encrypted/encoded. I will be using Visual Basic for my example of uploading POST information. (refer to this tutorial for uploading plain text POST information; http://www.sourcecodester.com/tutorials/visual-basic-net/7704/uploading-post-data-php-visual-basicnet.html).
Visual Basic .NET also has a Base64 function included within it's .NET framework which makes it easy for us to encode the data just before sending, to do this we simply use the 'ToBase64String' function from the 'Convert' namespace. We convert the plain text information to a base64 encrypted/encoded string, from there we get the bytes and send those the our PHP API.
OLD:
NEW:
FULL:
Destination:
Again, please refer to this tutorial, for PHP plain text POST uploading via Visual Basic .NET; .
To enable base64 decryption on to our PHP API, we want to use the simple 'DecodeBase64' function PHP has bulit in to it. The function accepts one base64 string, and returns it decoded; this means we can simply output the value straight from the function;
Conculsion:
To conclude this tutorial, our modified scripts above from my reference and previously uploaded tutorial now transfers base64 encoded data from the .NET application to the PHP server file. This stops attackers from simply sniffing network packets and catching plain text passwords as easily.
Finished!
- Dim byteData As Byte() = Encoding.Default.GetBytes(Convert.ToBase64String(p))
- Dim byteData As Byte() = p
- Private Function sendPost(ByVal p As Byte()) As String
- Dim encoding As New UTF8Encoding
- Dim byteData As Byte() = Encoding.Default.GetBytes(Convert.ToBase64String(p))
- Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("http://www.website.com/upload.php"), HttpWebRequest)
- postReq.Method = "POST"
- postReq.KeepAlive = True
- postReq.ContentType = "application/x-www-form-urlencoded"
- postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)"
- postReq.ContentLength = byteData.Length
- End Function
- <?php
- echo base64_decode($_POST['message']); //Output the message decoded to plain text from base64 encryption.
- }else
- echo 'No message POST data found.';
- ?>
Add new comment
- 139 views