Encryption - Documentation

Submit form to revial code.

Encryption - Code

Password Hash

  1. <?php
  2.  
  3. $PasswordHash = "Xr6RD73ALuOwRqh5";
  4.  

Private Key

  1. <?php
  2.  
  3. $PrivateKey = "rwkJcDCuIHE8QRDg";
  4.  

Public Key

  1. <?php
  2.  
  3. $PublicKey = "SLeuGs3jOxcAGr4r";
  4.  

Reqiure Files

  1. <?php
  2.  
  3. require_once 'keys/PasswordHash.php';
  4. require_once 'keys/PrivateKey.php';
  5. require_once 'keys/PublicKey.php';
  6.  
  7. require_once 'encryption/encryption-class.php';
  8. require_once 'encryption/encryption-functions.php';
  9. require_once 'encryption/encryption-config.php';
  10.  

Encryption Class

  1. <?php
  2.  
  3. class KeyChain {
  4.  
  5. public static function join_keys() {
  6. global $PrivateKey;
  7. global $PublicKey;
  8. $FullRing = $PrivateKey . $PublicKey;
  9. return $FullRing;
  10. }
  11.  
  12. public static function hash_keys() {
  13. global $PasswordHash;
  14. return $PasswordHash;
  15. }
  16.  
  17. public static function PrivateKey() {
  18. global $PrivateKey;
  19. return $PrivateKey;
  20. }
  21.  
  22. public static function PublicKey() {
  23. global $PublicKey;
  24. return $PublicKey;
  25. }
  26.  
  27. }
  28.  
  29. class Encrypt extends KeyChain {
  30.  
  31. public static function lock($string) {
  32. $keys = parent::join_keys();
  33. $hash = parent::hash_keys();
  34. $key = hash('sha256', $keys);
  35. $initialization_vector = substr(hash('sha256', $hash), 0, 16);
  36. $output = openssl_encrypt($string, 'AES-256-CBC', $key, 0, $initialization_vector);
  37. $output = base64_encode($output);
  38. return $output;
  39. }
  40.  
  41. public static function unlock($string) {
  42. $keys = parent::join_keys();
  43. $hash = parent::hash_keys();
  44. $key = hash('sha256', $keys);
  45. $initialization_vector = substr(hash('sha256', $hash), 0, 16);
  46. $output = openssl_decrypt(base64_decode($string), 'AES-256-CBC', $key, 0, $initialization_vector);
  47. return $output;
  48. }
  49.  
  50. }
  51.  

Encryption Config

  1. <?php
  2.  
  3. // add or remove as needed
  4. $fields_array = [
  5. 'username',
  6. 'password',
  7. 'email',
  8. 'name',
  9. 'blank',
  10. ];
  11. /*
  12.  * blank added to validate if statment
  13.  * once you know the value you can compare
  14.  * example $blank = "b2VVU09PcStIZ21JZElPUElKN0QzZz09" ;
  15.  * if ($username==$blank) {
  16.  *
  17.  * }
  18.  */

Encryption Functions

  1. <?php
  2.  
  3. // SQL injection protecion
  4. function SQLClean($string) {
  5. $value = trim($string);
  6. $value = stripslashes($string);
  7. $value = htmlentities($string);
  8. return $value;
  9. }
  10.  
  11. // Encrypt all fields in $fields_array on post
  12. foreach ($fields_array as $value) {
  13. $Postvalue = $value;
  14. if (isset($_POST[$Postvalue])) {
  15. $_POST[$Postvalue] = Encrypt::lock(SQLClean($_POST[$Postvalue]));
  16. }
  17. }
  18.  
  19. // Encrypt on the fly
  20. function LockEncrypt($string) {
  21. return Encrypt::lock($string);
  22. }
  23.  
  24. // Decrypt
  25. function UnLockEncrypt($string) {
  26. return Encrypt::unlock($string);
  27. }
  28.  

Sample Form

  1. <?php
  2. if (isset($_POST['name'])) {
  3. // Encrypt on the fly
  4. $encrypt_title = LockEncrypt('Encrypted Posted Values');
  5. // value = Wnp6UHVsb2dnSEFYajg1ZzB0YXBzTDMyWnloWUFMNFRsbWw2V2ZUSis2MD0=
  6. $title = UnLockEncrypt($encrypt_title);
  7. echo $title;
  8. echo "<pre>";
  9. print_r($_POST);
  10. echo "</pre>";
  11. $_SESSION['name'] = UnLockEncrypt($_POST['name']);
  12. $_SESSION['email'] = UnLockEncrypt($_POST['email']);
  13. $_SESSION['blank'] = UnLockEncrypt($_POST['blank']);
  14. echo 'Decrypted Posted Values ';
  15. echo "<pre>";
  16. print_r($_SESSION);
  17. echo "</pre>";
  18. }
  19. ?>
  20. <form id="contactForm" action="" method="post" data-toggle="validator">
  21. <div class="form-group">
  22. <input type="text" class="form-control" id="name" value="<?php if (isset($_SESSION['name'])) echo $_SESSION['name'] ?>" name="name" placeholder="Full Name" required>
  23. <div class="help-block with-errors"></div>
  24. </div>
  25. <div class="form-group">
  26. <input type="email" class="form-control" id="email" value="<?php if (isset($_SESSION['email'])) echo $_SESSION['email'] ?>" name="email" placeholder="Email" required>
  27. <div class="help-block with-errors"></div>
  28. </div>
  29. <div class="form-group">
  30. <input type="text" class="form-control" id="blank" placeholder="blank no value" readonly="" name="blank" >
  31. <div class="help-block with-errors"></div>
  32. </div>
  33. <input type="submit" value="Submit" class="btn btn-primary" />
  34. </form>

Encryption - Key Generator