Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-14-2006, 12:48 PM   PM User | #1
NancyJ
Senior Coder

 
NancyJ's Avatar
 
Join Date: Feb 2005
Location: Bradford, UK
Posts: 3,162
Thanks: 19
Thanked 65 Times in 64 Posts
NancyJ will become famous soon enough
Decryption Problems

I have an encryption function and a decryption function as part of my credit card class.
The decryption function is passed a value and a key.
If the decryption function is called from the encryption function, it works perfectly.
If the result of the encryption is converted into a string ($v[0].$v[1]) and stored as $this->encryptedNumber then later split in 2 and passed to the decryption function with the same key, it doesnt work.
I printed out both arrays that were passed in and they are both identical.
I'm absolutely baffled as to what the problem is.

PHP Code:
  function encrypt()
  {
     
$k $this->getKey();
     
$v explode("\r\n"chunk_split($this->numberceil(strlen($this->number)/2)));
     
$this->xteaEncrypt($v$k);
  }


  function 
decrypt()
  {
    
$k $this->getKey();
    
$v explode("\r\n"chunk_split($this->encryptedNumberceil(strlen($this->encryptedNumber)/2)));

    
$this->xteaDecrypt($v$k);
    
  }

  function 
getKey()
  {
     
$k[0] = "123456789012345";
$k[1] = "678901234567890";
$k[2] = "987654321098765";
$k[3] = "432109876543210";
     return 
$k;
  }
  
function 
xteaEncrypt($v$k) {
     
$sum 0;
     
$delta 0x9E3779B9;
     for(
$i=0$i<32$i++) {
         
$v[0] += unsigned(($v[1] << $v[1] >> 5) + $v[1] ^ $sum $k[$sum 3]);
         
$sum += $delta;
         
$v[1] += unsigned(($v[0] << $v[0] >> 5) + $v[0] ^ $sum $k[$sum>>11 3]);
     }
    
$this->encryptedNumber $v[0].$v[1];
    
$this->xteaDecrypt($v$k);
 }
 
function 
xteaDecrypt($v$k) {
      
print_r($v)."<br />";
      
print_r($k)."<br />";
     
$sum 0xC6EF3720;
     
$delta 0x9E3779B9;
     for(
$i=0$i<32$i++) {
         
$v[1] -= unsigned(($v[0] << $v[0] >> 5) + $v[0] ^ $sum $k[$sum>>11 3]);
         
$sum -= $delta;
         
$v[0] -= unsigned(($v[1] << $v[1] >> 5) + $v[1] ^ $sum $k[$sum&3]);
     }
     echo 
"<br />Funct Decrypted:".$v[0].$v[1]."<br />";
     
$this->number $v[0].$v[1];
 }
function 
unsigned($val)
{
  
$val $val*$val;
  
$val sqrt($val);
  return 
$val;
  

__________________
http://www.hazelryan.co.uk
NancyJ is offline   Reply With Quote
Old 02-14-2006, 02:37 PM   PM User | #2
NancyJ
Senior Coder

 
NancyJ's Avatar
 
Join Date: Feb 2005
Location: Bradford, UK
Posts: 3,162
Thanks: 19
Thanked 65 Times in 64 Posts
NancyJ will become famous soon enough
I've compared the two, I've printed out the ascii character codes for each character in the array and they still come out the same. But the decryption comes out different.
__________________
http://www.hazelryan.co.uk
NancyJ is offline   Reply With Quote
Old 02-14-2006, 03:08 PM   PM User | #3
NancyJ
Senior Coder

 
NancyJ's Avatar
 
Join Date: Feb 2005
Location: Bradford, UK
Posts: 3,162
Thanks: 19
Thanked 65 Times in 64 Posts
NancyJ will become famous soon enough
I've narrowed down the problem to being that there is some whitespace being lost here:

$this->number = $v[0].$v[1];

doesnt anyone know how I can preserve that white space?
__________________
http://www.hazelryan.co.uk
NancyJ is offline   Reply With Quote
Old 02-14-2006, 10:29 PM   PM User | #4
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,943
Thanks: 7
Thanked 82 Times in 81 Posts
firepages will become famous soon enough
total guess

$this->number = "{$v[0]}.{$v[1]}";

or cast everything as (string)$v[0] etc , otherwise not a clue
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is online now   Reply With Quote
Old 02-14-2006, 11:45 PM   PM User | #5
NancyJ
Senior Coder

 
NancyJ's Avatar
 
Join Date: Feb 2005
Location: Bradford, UK
Posts: 3,162
Thanks: 19
Thanked 65 Times in 64 Posts
NancyJ will become famous soon enough
tried the first one, havent tried casting it as a string. WIll give it a go in the morning.
__________________
http://www.hazelryan.co.uk
NancyJ is offline   Reply With Quote
Old 02-15-2006, 09:46 AM   PM User | #6
NancyJ
Senior Coder

 
NancyJ's Avatar
 
Join Date: Feb 2005
Location: Bradford, UK
Posts: 3,162
Thanks: 19
Thanked 65 Times in 64 Posts
NancyJ will become famous soon enough
that didnt work either :S

Theres got to be a way to preserve that data.... hmmm maybe thats why I didnt find a php version of xTEA
__________________
http://www.hazelryan.co.uk
NancyJ is offline   Reply With Quote
Old 02-15-2006, 01:54 PM   PM User | #7
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,943
Thanks: 7
Thanked 82 Times in 81 Posts
firepages will become famous soon enough
http://php-einfach.de/sonstiges_gene...tea_script.php
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is online now   Reply With Quote
Old 02-15-2006, 03:52 PM   PM User | #8
NancyJ
Senior Coder

 
NancyJ's Avatar
 
Join Date: Feb 2005
Location: Bradford, UK
Posts: 3,162
Thanks: 19
Thanked 65 Times in 64 Posts
NancyJ will become famous soon enough
I wish my german was better lol
I know it shouldnt matter but for some reason its really off-putting
__________________
http://www.hazelryan.co.uk
NancyJ is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:56 AM.


Advertisement
Log in to turn off these ads.