encrypting and decrrypting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shehan31
Forum Commoner
Posts: 59
Joined: Sun Aug 29, 2010 5:24 am

encrypting and decrrypting

Post by shehan31 »

Hello everyone;
I am trying to demonstrate some encryption and decription. What i am trying to do is to match the input meaasages each character with the array. the new plain text character will be assinged to a variable called $newstr. The matching method is not working for me its gives me a parser error.

Code: Select all

<?php
if (isset($_POST['submit1']))
   { 
   $str = $_POST['Customer_name'];
$key=array('A' => 'B', 'B' => 'C', 'C' => 'D','D'=>'E', 'E'=>'F', 'F'=>'G', 'G'=>'H', 'H'=>'I', 'I'=>'J', 'J'=>'K', 'K'=>'L','L'=>'M', 'M'=>'N', 'N'=>'O', 'O'=>'P', 'P'=>'Q','Q'=>'R', 'R'=> 'S', 'S'=>'T', 'T'=>'U', 'U'=>'V','V'=>'W','W'=>'X', 'X'=>'Y', 'Y'=>'Z', 'Z'='A'); 
$length=strlen($str);
$newstr=' ' ;

for ($i=0;$i<$length;++$i) {
//the each character of the message will be matched with the index value of the array.
 if (preg_match($str, $key)) {
    $str = $key[$i];
	$str=$newstr;
	echo"$newstr";}
	
	
  
  /*if (in_array($str[$i],$key[$i])) {
    $newstr.=$key[$i];*/
	
	
}  
}

//this part is ok
   
echo"

 <form action='pinassing.php' method='POST' >
 <td>
   <tr>
        <td>
		
         <font SIZE = 5 FACE = times new roman >Customer name</font>
        </td>
        <td>
        <input type='text' name='Customer_name' maxlength='30'>
        </td>
   </tr>
   </td>
    <tr>
       <td align='center' valign='top' colspan='2'>
        <input type='submit' name='submit1'  value='Confirm'>
       </td>
	   </tr>
		</form>";
regards
Shehan31
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: encrypting and decrrypting

Post by Darhazer »

Code: Select all

$key=array('A' => 'B', 'B' => 'C', 'C' => 'D','D'=>'E', 'E'=>'F', 'F'=>'G', 'G'=>'H', 'H'=>'I', 'I'=>'J', 'J'=>'K', 'K'=>'L','L'=>'M', 'M'=>'N', 'N'=>'O', 'O'=>'P', 'P'=>'Q','Q'=>'R', 'R'=> 'S', 'S'=>'T', 'T'=>'U', 'U'=>'V','V'=>'W','W'=>'X', 'X'=>'Y', 'Y'=>'Z', 'Z'='A'); 
Have to be

Code: Select all

$key=array('A' => 'B', 'B' => 'C', 'C' => 'D','D'=>'E', 'E'=>'F', 'F'=>'G', 'G'=>'H', 'H'=>'I', 'I'=>'J', 'J'=>'K', 'K'=>'L','L'=>'M', 'M'=>'N', 'N'=>'O', 'O'=>'P', 'P'=>'Q','Q'=>'R', 'R'=> 'S', 'S'=>'T', 'T'=>'U', 'U'=>'V','V'=>'W','W'=>'X', 'X'=>'Y', 'Y'=>'Z', 'Z'=>'A'); 
Note the z=>a
shehan31
Forum Commoner
Posts: 59
Joined: Sun Aug 29, 2010 5:24 am

Re: encrypting and decrrypting

Post by shehan31 »

Thank you for your reply.
that isn't the case.
errors.

Warning: preg_match() expects parameter 2 to be string, array given in C:\wamp\www\pin project\pinassing.php on line 22
must be someting differnt.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: encrypting and decrrypting

Post by Darhazer »

You had parse error, now you have warning - it's completely different story :)
What you are trying to achieve with the preg_match?
At first sight it seems that it should be:

Code: Select all

for ($i=0;$i<$length;++$i) {
//the each character of the message will be matched with the index value of the array.
$letter = $str[$i];
 if (isset($key[$letter])) {
    $newstr .= $key[$letter];
}
}
shehan31
Forum Commoner
Posts: 59
Joined: Sun Aug 29, 2010 5:24 am

Re: encrypting and decrrypting

Post by shehan31 »

Hi dazhar;
Thank you for you reply. I hvae check your code and it does not contains any index to compare the elements as i see.
But taking your valuable comments, i have tried some thing different and it doesent work either. here is the code.

Code: Select all

<?php
if (isset($_POST['submit1']))
   { 
   $Customername = $_POST['Customer_name'];
$array=array('A' => 'B', 'B' => 'C', 'C' => 'D','D'=>'E', 'E'=>'F', 'F'=>'G', 'G'=>'H', 'H'=>'I', 'I'=>'J', 'J'=>'K', 'K'=>'L','L'=>'M', 'M'=>'N', 'N'=>'O', 'O'=>'P', 'P'=>'Q','Q'=>'R', 'R'=> 'S', 'S'=>'T', 'T'=>'U', 'U'=>'V','V'=>'W','W'=>'X', 'X'=>'Y', 'Y'=>'Z', 'Z'='a');
$str= str_split($Customername);
 
  for($i=0; $i < count($str1); $i++){
    	echo"$str[$i]";}
 
 $str1=array($str);
 foreach($array as $key=>$value){
 	for($i=0; $i<count($str);$i++){
		if($str[$i]==$key[$i]){
		    
			$str=$value;
			echo"$str";
		}
	}
}
echo"

 <form action='pinassing.php' method='POST' >
 <td>
   <tr>
        <td>
		
         <font SIZE = 5 FACE = times new roman >Customer name</font>
        </td>
        <td>
        <input type='text' name='Customer_name' maxlength='30'>
        </td>
   </tr>
   </td>
    <tr>
       <td align='center' valign='top' colspan='2'>
        <input type='submit' name='submit1'  value='Confirm'>
       </td>
	   </tr>
		</form>";
?>
Post Reply