Tuesday 2 June 2015

Simple numerical encryption/decryption

Ability to store strings in encrypted array-string 
<?php
function num2abc($chars = "") 
{ 
 $str = '';
 $chars = explode(",",$chars);
 $alpha = range('a','z');  
 foreach($chars as $char) 
 { 
  $str .= ($char==-1) ? ' ' : $alpha[$char]; 
 } 
 return $str; 
}

function abc2num($chars = "")
{
 $arr = array();
 $numbers = array_combine(range('a','z'),range(0,25));
 $letters = str_split($chars);
 foreach ($letters as $letter)
 {
  $str .= ($letter==' ') ? ',-1' : ',' . $numbers[$letter];
 }
 return trim($str,',');
}

/*
echo num2abc('0,1,2,-1,23,24,25'); // abc xyz
echo abc2num('abc xyz'); // 0,1,2,-1,23,24,25
*/
?>
 

Usage

num2abc is the decrypter, takes an array-based string of numbers and returns the string based on abc2num.

abc2num takes a string and returns an encrypted numerical string.
 

0 comments:

Post a Comment