PHP Encryption and Decryption Form POST Data

PHP Encryption and Decryption Form POST Data

In this Post We Will Explain About is PHP Encryption and Decryption Form POST Data With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Encrypt Post Data PHP Solution without SSL Example

In this post we will show you Best way to implement Encrypt and decrypt forms with AES and RSA in PHP, hear for How to Encrypt & Decrypt Form Data using PHP Ajax with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Database

CREATE TABLE IF NOT EXISTS `tbl_student` (
  `id` int(11) NOT NULL,
  `stud_fname` varchar(250) NOT NULL,
  `stud_lname` varchar(250) NOT NULL,
  `mobile` varchar(30) NOT NULL,
  `email` varchar(200) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=latin1;

ALTER TABLE `tbl_student`
  ADD PRIMARY KEY (`id`);
  
  
ALTER TABLE `tbl_student`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=14;
  

db_config.php


index.php



 
  PHP Form Encryption: Encrypt and decrypt forms with AES and RSA
 
 
  

Encryption and Decryption Form Data in PHP


Student List

Student First Name Student Last Name Student Mobile Student Email Student Edit Student Delete

Include External Libs

  
  
  
    
  
  

index.js

$(document).ready(function(){
  
 $('#insert_btn').click(function(){
  $('#liveStudentMdl').modal('show');
  $('#studentForm')[0].reset();
  $('.modal-title').html(" Add Student");
  $('#action').val('Add');
  $('#model_crud_opt').val('Add');
 });
 
 var model_crud_opt = 'fetch_all';
 
 var srydentdataTable = $('#student_data').DataTable({
  "processing":true,
  "serverSide":true,
  "order":[],
  "ajax":{
   url:"student_action.php",
   type:"POST",
   data:{model_crud_opt:model_crud_opt}
  },
  "columnDefs":[
   {
    "targets":[4, 5],
    "orderable":false,
   },
  ],
  "pageLength": 10
 });
 
 $(document).on('submit', '#studentForm', function(event){
  
  event.preventDefault();
  
  var form_data = $(this).serialize();
  
  $.ajax({
   url:"student_action.php",
   method:"POST",
   data:form_data,
   dataType:"json",
   success:function(data)
   {
    if(data.error != '')
    {
     $('#error_validation').html(data.error);
    }
    else
    {
     $('#action_alrt').html(data.message);
     $('#studentForm')[0].reset();
     $('#liveStudentMdl').modal('hide');
     srydentdataTable.ajax.reload();
    }
     
   }
  });  
 });
 
 $(document).on('click', '.update', function(){
  var id = $(this).attr("id");
  model_crud_opt = "fetch_single";
  $.ajax({
   url:"student_action.php",
   method:"POST",
   data:{id:id, model_crud_opt:model_crud_opt},
   dataType:"JSON",
   success:function(data)
   {
    $('#error_validation').html('');
    $('#liveStudentMdl').modal('show');
    $('.modal-title').text('Edit Student');
    $('#stud_fname').val(data.stud_fname);
    $('#stud_lname').val(data.stud_lname);
    $('#mobile').val(data.mobile);
    $('#stud_emailaddr').val(data.stud_emailaddr);
    $('#id').val(id);
    $('#model_crud_opt').val('Edit');
    $('#action').val('Edit');
   }
  });
 });
 
 $(document).on('click', '.delete', function(){
  var id = $(this).attr("id");
  model_crud_opt = "Delete";
  if(confirm("Are you sure you want to student delete this?"))
  {
   $.ajax({
    url:"student_action.php",
    method:"POST",
    data:{id:id, model_crud_opt:model_crud_opt},
    dataType:"json",
    success:function(data)
    {
     $('#action_alrt').html(data.message);
     $('#liveStudentMdl').modal('hide');
     srydentdataTable.ajax.reload();
    }
   });
  }
  else
  {
   return false;
  }
 });
 
});

student_action.php

prepare($my_query);

  $statement->execute();

  $result = $statement->fetchAll();

  $filtered_rows = $statement->rowCount();

  foreach($result as $row)
  {
   $child_arr = array();
   $child_arr[] = string_convert_data('decrypt', $row['stud_fname']);
   $child_arr[] = string_convert_data('decrypt', $row['stud_lname']);
   $child_arr[] = string_convert_data('decrypt', $row['mobile']);
   $child_arr[] = string_convert_data('decrypt', $row['email']);
   $child_arr[] = '';
   $child_arr[] = '';
   $results[] = $child_arr;
  }

  $data = array(
   "draw"    => intval($_POST["draw"]),
   "recordsTotal"  => $filtered_rows,
   "recordsFiltered" => get_total_all_records($connect),
   "data"    => $results
  );
 }
 elseif($_POST["model_crud_opt"] == 'fetch_single')
 {
  $id = string_convert_data('decrypt', $_POST["id"]);
  $my_query = "
  SELECT * FROM tbl_student 
  WHERE id = '$id'
  ";
  $statement = $connect->prepare($my_query);
  $statement->execute();
  $result = $statement->fetchAll();
  foreach($result as $row)
  {
   $data['stud_fname'] = string_convert_data('decrypt', $row['stud_fname']);
   $data['stud_lname'] = string_convert_data('decrypt', $row['stud_lname']);
   $data['mobile'] = string_convert_data('decrypt', $row['mobile']);
   $data['stud_emailaddr'] = string_convert_data('decrypt', $row['email']);
  }
 }
 elseif($_POST["model_crud_opt"] == 'Delete')
 {
  $id = string_convert_data('decrypt', $_POST["id"]);
  $my_query = "
  DELETE FROM tbl_student 
  WHERE id = '$id'
  ";
  $statement = $connect->prepare($my_query);
  $statement->execute();
  $data = array(
   'message'  => '
Student Deleted
' ); } else { $message = ''; $error = ''; $stud_fname = ''; $stud_lname = ''; $mobile = ''; $stud_emailaddr = ''; if(empty($_POST["stud_fname"])) { $error .= '

First Name is Required

'; } else { if (!preg_match("/^[a-zA-Z]*$/",$_POST["stud_fname"])) { $error .= '

Only Alphabet allowed in First Name

'; } else { $stud_fname = clean_text($_POST["stud_fname"]); } } if(empty($_POST["stud_lname"])) { $error .= '

Last Name is Required

'; } else { if (!preg_match("/^[a-zA-Z]*$/",$_POST["stud_lname"])) { $error .= '

Only Alphabet allowed in Last Name

'; } else { $stud_lname = clean_text($_POST["stud_lname"]); } } if(empty($_POST["mobile"])) { $error .= '

Phone Number is Required

'; } else { if (!preg_match("/^[0-9]*$/",$_POST["mobile"])) { $error .= '

Only Numbers allowed in Phone

'; } else { $mobile = clean_text($_POST["mobile"]); } } if(empty($_POST["stud_emailaddr"])) { $error .= '

Email Address is Required

'; } else { if (!filter_var($_POST["stud_emailaddr"], FILTER_VALIDATE_EMAIL)) { $error .= '

Invalid email format

'; } else { $stud_emailaddr = clean_text($_POST["stud_emailaddr"]); } } if($error == '') { $stud_fname = string_convert_data('encrypt', $stud_fname); $stud_lname = string_convert_data('encrypt', $stud_lname); $mobile = string_convert_data('encrypt', $mobile); $stud_emailaddr = string_convert_data('encrypt', $stud_emailaddr); if($_POST["model_crud_opt"] == "Add") { $my_query = " SELECT * FROM tbl_student WHERE email = '$stud_emailaddr' "; $statement = $connect->prepare($my_query); $statement->execute(); $no_of_row = $statement->rowCount(); if($no_of_row > 0) { $error = '
Email Already Exists
'; } else { $my_query = " INSERT INTO tbl_student (stud_fname, stud_lname, mobile, email) VALUES('".$stud_fname."', '".$stud_lname."', '".$mobile."', '".$stud_emailaddr."') "; $message = '
Student Added
'; } } if($_POST["model_crud_opt"] == "Edit") { $id = string_convert_data('decrypt', $_POST["id"]); $my_query = " UPDATE tbl_student SET stud_fname = '$stud_fname', stud_lname = '$stud_lname', mobile = '$mobile', email = '$stud_emailaddr' WHERE id = '$id' "; $message = '
Student Edited
'; } $statement = $connect->prepare($my_query); $statement->execute(); $result = $statement->fetchAll(); if(isset($result)) { $data = array( 'error' => $error, 'message' => $message ); } } else { $data = array( 'error' => $error, 'message' => $message ); } } echo json_encode($data); } ?>

function.php

prepare('SELECT * FROM tbl_student');
 $statement->execute();
 return $statement->rowCount();
}

function clean_text($string)
{
 $string = trim($string);
 $string = stripslashes($string);
 $string = htmlspecialchars($string);
 return $string;
}

function string_convert_data($action, $string)
{
 $results = '';
 $data_encry_mth = "AES-256-CBC";
    $secret_key = 'eaiYYkYTysia2lnHiw0N0vx7t7a3kEJVLfbTKoQIx5o=';
    $secret_iv = 'eaiYYkYTysia2lnHiw0N0';
    //simple hash
    $key = hash('sha256', $secret_key);
 $data_initVct = substr(hash('sha256', $secret_iv), 0, 16);
 if($string != '')
 {
  if($action == 'encrypt')
  {
   $results = openssl_encrypt($string, $data_encry_mth, $key, 0, $data_initVct);
   $results = base64_encode($results);
  } 
  if($action == 'decrypt') 
  {
   $results = openssl_decrypt(base64_decode($string), $data_encry_mth, $key, 0, $data_initVct);
  }
 }
 return $results;
}

?>

You are Most welcome in my youtube Channel Please subscribe my channel. and give me FeedBack.
More Details……
Angularjs Example

Example

I hope you have Got What is Encrypt and decrypt forms with AES and RSA in PHP And how it works.I would Like to have FeedBack From My Blog(Pakainfo.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(Pakainfo.com) Are Most Always Welcome.

Leave a Comment