1

I need to encrypt files at one computer and open it another one using PHP without external libraries. The code should work at both PHP4 and PHP5.

Encryption function makes str_split of the string and encodes each character (ord) using str_split of password. Then it makes chr and I get binary data. This binary data is encoded using base64_encode and I get ascii string.

I transfer this file to another computer who knows the password. I make base64_decode and make decrypt.

The problem appeares sometimes because the first computer has ASCII default_charset and second has UTF-8. That's why nth-char $temproraryBinaryString[$n-1] may have different values at these computers.

Can I ask PHP to treat all strings as ASCII if I cannot control php.ini at any of this computers?

luchaninov
  • 6,792
  • 6
  • 60
  • 75
  • Don’t reinvent the wheel but use an existing encryption algorithm. – Gumbo May 20 '11 at 16:24
  • possible duplicate of [Best way to use PHP to encrypt and decrypt?](http://stackoverflow.com/questions/1289061/best-way-to-use-php-to-encrypt-and-decrypt) – Alix Axel May 20 '11 at 16:45

2 Answers2

1

Take a look at the discussion on this post, as it talks about two-way encryption, using PHP mcrypt, which is what you should use. Two-way encryption: I need to store passwords that can be retrieved

Community
  • 1
  • 1
toneplex
  • 673
  • 5
  • 6
  • + oned for mcrypt, just be aware the 2 servers need the same php versions. Too lazy to look it up myself but I think the implementation or something changed between earlier version and 5.3. – Stephane Gosselin May 21 '11 at 04:15
0

I wrongly flagged as duplicated but it's not, what you're talking is encoding not encryption, just try:

$ascii = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $string);

This should give you a clean ASCII string to work on with str_split() and so on.

Alix Axel
  • 151,645
  • 95
  • 393
  • 500