0

I am using this solution to encrypt and post some form data to my data base. On a separate page I now need to decrypt that data and save it all in to a CSV file with multiple rows.

My code decrypts fine (as when I echo $responses it prints all the correct data to the page) but I can't load it in to the CSV. This code creates a CSV but each field is in a random place and there are loads of odd characters. I've tried loads of variations of the below code (including loading it in to an array) with no luck.

$theresults = $wpdb->get_results( 
"
SELECT * 
FROM my_table
"
);

foreach ( $theresults as $result ) {

$iv = $result->iv;

$responses = openssl_decrypt($result->title,'AES-256-CBC',$encryption_key,0,$iv).",".
openssl_decrypt($result->fname,'AES-256-CBC',$encryption_key,0,$iv).",".
openssl_decrypt($result->lname,'AES-256-CBC',$encryption_key,0,$iv).",".
openssl_decrypt($result->address,'AES-256-CBC',$encryption_key,0,$iv).",".
openssl_decrypt($result->emailadd,'AES-256-CBC',$encryption_key,0,$iv).",".
openssl_decrypt($result->who,'AES-256-CBC',$encryption_key,0,$iv);

fputcsv($output, explode(',',$responses));

}

UPDATE

So I got this working by passing my data in to variables then creating my array like so:

$responses = array($title,$fname,$lname,$address,$age,$emailadd,$who);
fputcsv($output, $responses);

This inserts the correct rows in to a CSV file.

The issue I now have is the data is suffixed with odd characters such as diamond shapes, music symbols, lines, boxes with ? in them. Does anyone know what causes this?

Community
  • 1
  • 1
Zoee
  • 9
  • 2
  • So you have a *correct* csv in $responses, but can't get it to write to disk correctly? – JLB Mar 24 '16 at 14:43
  • *"each field is in a random place"* - are you trying to say that what you get consists of random characters and **not** the ones you expected? If so, you used a wrong encryption method, wrong key or wrong encoding. How is this encrypted? Can you show us the code? – Artjom B. Mar 24 '16 at 14:45
  • What is `$output`? I don't see it defined anywhere in your code. – Chris Mar 24 '16 at 14:47
  • My output is $output = fopen('php://output', 'w'); – Zoee Mar 31 '16 at 13:21

1 Answers1

2

You're manually building a comma-separated string and then splitting on commas. If any of your fields actually contain commas, this will break since you're not escaping them. This will result in more fields than you expected, and they'll be in the wrong place. Instead, just drop the fields directly into an array and pass that array to fputcsv(), which will do the comma escaping for you.

$responses = [
    openssl_decrypt($result->title ... ),
    openssl_decrypt($result->fname ... ),
    ...
];
fputcsv($output, $responses);
Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
  • Thanks I'll give that a go later :) – Zoee Mar 30 '16 at 14:49
  • That isn't working for me, I've tried so many ways to load it in to an array and it just errors each time, I don't even get an error its just a white page, even removes all my echo's. I can echo the data and have also tried loading it in to variables but as soon as I try and put it in an array it falls apart! – Zoee Mar 31 '16 at 13:22
  • If your PHP is too old you won't be able to use the short array syntax `[1,2,3]` and you'll have to use the old style `array(1,2,3)`. You can also run `php -l file.php` on your source file from the command line, and it will perform a syntax check and tell you if anything is obviously wrong. – Alex Howansky Mar 31 '16 at 13:24
  • Thanks Alex, array() worked. I've updated my post above, I just have some odd issues with strange symbols in my CSV now... – Zoee Mar 31 '16 at 13:47