php - Encrypted data not being inserted into mysql table -


i'm trying upgrade encryption routines des blowfish. have function en/decryption:

function newcryptstr($encryptordecrypt,$str) {   $key='test';    $ivsize=mcrypt_get_iv_size(mcrypt_blowfish,mcrypt_mode_ecb);   $iv=mcrypt_create_iv($ivsize,mcrypt_rand);    if($encryptordecrypt==='encrypt')     {       $str=mcrypt_encrypt(mcrypt_blowfish,$key,$str,mcrypt_mode_ecb,$iv);       $str=mysql_real_escape_string($str);     }    if($encryptordecrypt==='decrypt')     $str=mcrypt_decrypt(mcrypt_blowfish,$key,$str,mcrypt_mode_ecb,$iv);    return $str; } 

this works fine:

$str='test string encrypted decrypted'; print "<p>original $str</p>\n"; $str=newcryptstr('encrypt',$str); print "<p>encrypted $str</p>\n"; $str=newcryptstr('decrypt',$str); print "<p>decrypted $str</p>\n"; 

then i'm running following php script on table of email addresses convert column of plain text & update column encrypted result:

$sql="select uid,str testdata order uid"; $result=mysql_query($sql,$link);  while($row=mysql_fetch_array($result))   {     $encrypted=newcryptstr('encrypt',$row[str]);      $sql="update testdata set str2='$encrypted' uid=$row[uid]";     print "<p>$row[str] > $encrypted</p>\n";     print "<p>$sql</p>\n";     mysql_query($sql,$link) or die("failed $sql ".mysql_error());   } 

it runs without error & prints out load of encrypted strings i'd expect when view data in table none of records have been updated same data printed php above. instead of values in str2 blank have 1 or 2 characters in them.

all sql statements printed fine, & running them individually updates record correctly.

the sql tables & mysql connection both using utf8 encoding, str & str2 columns varchar datatypes.

why isn't table being updated correct data?

edit have solved (skirted round!) issue using base encoding/decoding output/input of newcryptstr function. i'm still curious know why loop through table wasn't working when sql statements happily worked when executed individually.

you have in code: $row[uid], not defined, no records updated.

change $row[uid] $row['uid'];

use:

$sql="update testdata set str2='".$encrypted."' uid=".$row['uid']; 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -