SlideShare a Scribd company logo
PHP ENCRYPTION AND
MYCRYPT PACKAGE
By
SANA MATEEN
PHP ENCRYPTION FUNCTIONS
• Encryption over the Web is largely useless unless the scripts running the encryption schemes are operating on an
SSL-enabled server. Why?
• PHP is a server-side scripting language, so information must be sent to the server in plain-text format before it can
be encrypted.
• There are many ways that an unwanted third party can watch this information as it is transmitted from the
user to the server if the user is not operating via a secured connection.
• Encrypting Data with the md5() Hash Function
• The md5() function uses MD5, a third-party hash algorithm often used for creating digital signatures (among other
things). Digital signatures can, in turn, be used to uniquely identify the sending party.
• MD5 is considered to be a one-way hashing algorithm, which means there is no practical way to dehash data that
has been hashed using md5(). Its prototype looks like this:
• string md5(string str)
• The MD5 algorithm can also be used as a password verification system. Because it is (in theory) extremely
difficult to retrieve the original string that has been hashed using the MD5 algorithm, you could hash a given
password using MD5 and then compare that encrypted password against those that a user enters to gain access to
restricted information.
• For example, assume that your secret password toystore has an MD5 hash of
• 745e2abd7c52ee1dd7c14ae0d71b9d76.
• You can store this hashed value on the server and compare it to the MD5 hash equivalent of the password the
user attempts to enter.
• Even if an intruder gets hold of the encrypted password, it wouldn’t make much difference because that
intruder can’t return the string to its original format through conventional means. An example of hashing a
string using md5() follows:
• Remember that to store a complete hash in a database, you need to set the field length to 32 characters.
The MCrypt Package
• MCrypt is a popular data-encryption package available for use with PHP, providing support for two-way
• encryption (i.e., encryption and decryption). Before you can use it, you need to follow these installation
• instructions:
• 1.Go to http://mcrypt.sourceforge.net and download the package source.
• 2. Extract the contents of the compressed distribution and follow the installation
• instructions as specified in the INSTALL document.
• 3. Compile PHP with the --with-mcrypt option.
• MCrypt supports the following encryption algorithms:
Encrypting Data with MCrypt
• The mcrypt_encrypt() function encrypts the provided data, returning the encrypted result. The
• prototype follows:
• string mcrypt_encrypt(string cipher, string key, string data,string mode [, string iv])
• The provided cipher names the particular encryption algorithm, and the parameter key determines the key used to encrypt the data.
The mode parameter specifies one of the six available encryption modes:
• electronic codebook, cipher block chaining, cipher feedback, 8-bit output feedback, N-bit output feedback, and a special stream
mode. Each is referenced by an abbreviation: ecb, cbc, cfb, ofb, nofb, and stream, respectively. Finally, the iv parameter initializes
cbc, cfb, ofb, and certain algorithms used in stream mode. Consider an example:
Decrypting Data with MCrypt
• The mcrypt_decrypt() function decrypts a previously encrypted cipher, provided that the cipher, key, and mode are the same as those used to
encrypt the data. Its prototype follows:
• string mcrypt_decrypt(string cipher, string key, string data,string mode [, string iv])
• Go ahead and insert the following line into the previous example, directly after the last statement:
• echo mcrypt_decrypt(MCRYPT_DES, $key, $enc, MCRYPT_MODE_CBC, $iv);
• The methods in this section are only those that are in some way incorporated into the PHPextension set. However, you are not limited to
these encryption/hashing solutions.
File-Based Authentication
• The PHP script required to parse this file and authenticate a user against a
given login pair is only a tad more complicated than the script used to
authenticate against a hard-coded authentication pair. The difference lies in
the script’s additional duty of reading the text file into an array, and then
cycling through that array searching for a match. This involves the use of
several functions, including the following:
• file(string filename): The file() function reads a file into an array, with each
• element of the array consisting of a line in the file.
• explode(string separator, string string [, int limit]): The explode() function
• splits a string into a series of substrings, with each string boundary
determined by a specific separator.
• md5(string str): The md5() function calculates an MD5 hash of a string,
using RSA
• Security Inc.’s MD5 Message-Digest algorithm (www.rsa.com). Because the
• passwords are stored using the same encrypted format, you first use the
md5() function to encrypt the provided password, comparing the result with
what is stored locally.
•Although the file-based authentication system works well for relatively small, static authentication lists, this strategy
can quickly become inconvenient when you’re handling a large number of users;
•when users are regularly being added, deleted, and modified;
Database-Based Authentication
CREATE TABLE logins (
id INTEGER UNSIGNED NOT NULL
AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) NOT NULL,
pswd CHAR(32) NOT NULL
);

More Related Content

PDF
Hashing Algorithm: MD5
PPT
MD5Algorithm
PDF
The MD5 hashing algorithm
PPT
Network security cryptographic hash function
PPTX
Cryptography - Simplified - Hash Functions
PPTX
Cryptographic hash function md5
PPT
Hash Function & Analysis
PDF
Hashing Algorithm: MD5
MD5Algorithm
The MD5 hashing algorithm
Network security cryptographic hash function
Cryptography - Simplified - Hash Functions
Cryptographic hash function md5
Hash Function & Analysis

What's hot (20)

PPTX
Cryptographic Hashing Functions
PPTX
5. message authentication and hash function
PPTX
Hash Function
PPTX
Information and data security cryptographic hash functions
PDF
The SHA Hashing Algorithm
PPT
Lecture 3b public key_encryption
PDF
MD-5 : Algorithm
PDF
Cns
PPTX
Message digest 5
PPTX
Message Authentication using Message Digests and the MD5 Algorithm
PDF
Hash
PPT
Cryptography and Message Authentication NS3
PDF
CNIT 141: 6. Hash Functions
PPTX
Message authentication with md5
PDF
A technical writing on cryptographic hash function md5
PDF
Cs8792 cns - unit iv
PPS
Message AUthentication Code
Cryptographic Hashing Functions
5. message authentication and hash function
Hash Function
Information and data security cryptographic hash functions
The SHA Hashing Algorithm
Lecture 3b public key_encryption
MD-5 : Algorithm
Cns
Message digest 5
Message Authentication using Message Digests and the MD5 Algorithm
Hash
Cryptography and Message Authentication NS3
CNIT 141: 6. Hash Functions
Message authentication with md5
A technical writing on cryptographic hash function md5
Cs8792 cns - unit iv
Message AUthentication Code
Ad

Similar to Encryption in php (20)

PDF
Cryptography For The Average Developer - Sunshine PHP
PDF
Cryptography For The Average Developer
PDF
Cryptography With PHP
PDF
Cryptography with PHP (Workshop)
PDF
Encryption: It's For More Than Just Passwords
PDF
Cryptography With PHP - ZendCon 2017 Workshop
PDF
SunshinePHP 2017: Tales From The Crypt - A Cryptography Primer
PDF
Cryptography in PHP: use cases
PDF
Strong cryptography in PHP
PDF
Password Storage and Attacking in PHP
PPTX
Web security
PDF
Password Storage And Attacking In PHP - PHP Argentina
PDF
Cryptography in PHP: Some Use Cases
PDF
ZendCon 2017 - Cryptography for Beginners
PDF
PHP Identity and Data Security
PDF
php[world] 2016 - Tales From the Crypto: A Cryptography Primer
PPTX
501 ch 10 cryptography
PDF
Cryptography with Zend Framework
PDF
2014 database - course 3 - PHP and MySQL
PPTX
Cryptography for the mere mortals - for phpXperts Seminar 2011 by Hasin and Tonu
Cryptography For The Average Developer - Sunshine PHP
Cryptography For The Average Developer
Cryptography With PHP
Cryptography with PHP (Workshop)
Encryption: It's For More Than Just Passwords
Cryptography With PHP - ZendCon 2017 Workshop
SunshinePHP 2017: Tales From The Crypt - A Cryptography Primer
Cryptography in PHP: use cases
Strong cryptography in PHP
Password Storage and Attacking in PHP
Web security
Password Storage And Attacking In PHP - PHP Argentina
Cryptography in PHP: Some Use Cases
ZendCon 2017 - Cryptography for Beginners
PHP Identity and Data Security
php[world] 2016 - Tales From the Crypto: A Cryptography Primer
501 ch 10 cryptography
Cryptography with Zend Framework
2014 database - course 3 - PHP and MySQL
Cryptography for the mere mortals - for phpXperts Seminar 2011 by Hasin and Tonu
Ad

More from sana mateen (20)

PPTX
PPTX
PHP Variables and scopes
PPTX
Php intro
PPTX
Php and web forms
PPTX
PPTX
Files in php
PPTX
File upload php
PPTX
Regex posix
PPTX
Authentication methods
PPTX
Xml schema
PPTX
Xml dtd
PPTX
Xml dom
PPTX
PPTX
Intro xml
PPTX
Dom parser
PPTX
Unit 1-subroutines in perl
PPTX
Unit 1-uses for scripting languages,web scripting
PPTX
Unit 1-strings,patterns and regular expressions
PPTX
Unit 1-scalar expressions and control structures
PPTX
Unit 1-perl names values and variables
PHP Variables and scopes
Php intro
Php and web forms
Files in php
File upload php
Regex posix
Authentication methods
Xml schema
Xml dtd
Xml dom
Intro xml
Dom parser
Unit 1-subroutines in perl
Unit 1-uses for scripting languages,web scripting
Unit 1-strings,patterns and regular expressions
Unit 1-scalar expressions and control structures
Unit 1-perl names values and variables

Recently uploaded (20)

PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Pharma ospi slides which help in ospi learning
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
RMMM.pdf make it easy to upload and study
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Basic Mud Logging Guide for educational purpose
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
human mycosis Human fungal infections are called human mycosis..pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Insiders guide to clinical Medicine.pdf
Complications of Minimal Access Surgery at WLH
Module 4: Burden of Disease Tutorial Slides S2 2025
Pharma ospi slides which help in ospi learning
O7-L3 Supply Chain Operations - ICLT Program
PPH.pptx obstetrics and gynecology in nursing
Sports Quiz easy sports quiz sports quiz
Pharmacology of Heart Failure /Pharmacotherapy of CHF
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
RMMM.pdf make it easy to upload and study
Microbial disease of the cardiovascular and lymphatic systems
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Basic Mud Logging Guide for educational purpose
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape

Encryption in php

  • 1. PHP ENCRYPTION AND MYCRYPT PACKAGE By SANA MATEEN
  • 2. PHP ENCRYPTION FUNCTIONS • Encryption over the Web is largely useless unless the scripts running the encryption schemes are operating on an SSL-enabled server. Why? • PHP is a server-side scripting language, so information must be sent to the server in plain-text format before it can be encrypted. • There are many ways that an unwanted third party can watch this information as it is transmitted from the user to the server if the user is not operating via a secured connection. • Encrypting Data with the md5() Hash Function • The md5() function uses MD5, a third-party hash algorithm often used for creating digital signatures (among other things). Digital signatures can, in turn, be used to uniquely identify the sending party. • MD5 is considered to be a one-way hashing algorithm, which means there is no practical way to dehash data that has been hashed using md5(). Its prototype looks like this: • string md5(string str) • The MD5 algorithm can also be used as a password verification system. Because it is (in theory) extremely difficult to retrieve the original string that has been hashed using the MD5 algorithm, you could hash a given password using MD5 and then compare that encrypted password against those that a user enters to gain access to restricted information.
  • 3. • For example, assume that your secret password toystore has an MD5 hash of • 745e2abd7c52ee1dd7c14ae0d71b9d76. • You can store this hashed value on the server and compare it to the MD5 hash equivalent of the password the user attempts to enter. • Even if an intruder gets hold of the encrypted password, it wouldn’t make much difference because that intruder can’t return the string to its original format through conventional means. An example of hashing a string using md5() follows: • Remember that to store a complete hash in a database, you need to set the field length to 32 characters.
  • 4. The MCrypt Package • MCrypt is a popular data-encryption package available for use with PHP, providing support for two-way • encryption (i.e., encryption and decryption). Before you can use it, you need to follow these installation • instructions: • 1.Go to http://mcrypt.sourceforge.net and download the package source. • 2. Extract the contents of the compressed distribution and follow the installation • instructions as specified in the INSTALL document. • 3. Compile PHP with the --with-mcrypt option. • MCrypt supports the following encryption algorithms:
  • 5. Encrypting Data with MCrypt • The mcrypt_encrypt() function encrypts the provided data, returning the encrypted result. The • prototype follows: • string mcrypt_encrypt(string cipher, string key, string data,string mode [, string iv]) • The provided cipher names the particular encryption algorithm, and the parameter key determines the key used to encrypt the data. The mode parameter specifies one of the six available encryption modes: • electronic codebook, cipher block chaining, cipher feedback, 8-bit output feedback, N-bit output feedback, and a special stream mode. Each is referenced by an abbreviation: ecb, cbc, cfb, ofb, nofb, and stream, respectively. Finally, the iv parameter initializes cbc, cfb, ofb, and certain algorithms used in stream mode. Consider an example:
  • 6. Decrypting Data with MCrypt • The mcrypt_decrypt() function decrypts a previously encrypted cipher, provided that the cipher, key, and mode are the same as those used to encrypt the data. Its prototype follows: • string mcrypt_decrypt(string cipher, string key, string data,string mode [, string iv]) • Go ahead and insert the following line into the previous example, directly after the last statement: • echo mcrypt_decrypt(MCRYPT_DES, $key, $enc, MCRYPT_MODE_CBC, $iv); • The methods in this section are only those that are in some way incorporated into the PHPextension set. However, you are not limited to these encryption/hashing solutions.
  • 7. File-Based Authentication • The PHP script required to parse this file and authenticate a user against a given login pair is only a tad more complicated than the script used to authenticate against a hard-coded authentication pair. The difference lies in the script’s additional duty of reading the text file into an array, and then cycling through that array searching for a match. This involves the use of several functions, including the following: • file(string filename): The file() function reads a file into an array, with each • element of the array consisting of a line in the file. • explode(string separator, string string [, int limit]): The explode() function • splits a string into a series of substrings, with each string boundary determined by a specific separator. • md5(string str): The md5() function calculates an MD5 hash of a string, using RSA • Security Inc.’s MD5 Message-Digest algorithm (www.rsa.com). Because the • passwords are stored using the same encrypted format, you first use the md5() function to encrypt the provided password, comparing the result with what is stored locally.
  • 8. •Although the file-based authentication system works well for relatively small, static authentication lists, this strategy can quickly become inconvenient when you’re handling a large number of users; •when users are regularly being added, deleted, and modified;
  • 9. Database-Based Authentication CREATE TABLE logins ( id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, username VARCHAR(255) NOT NULL, pswd CHAR(32) NOT NULL );