luchaninov
luchaninov

Reputation: 6796

PHP encrypt/decrypt default_charset problem

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?

Upvotes: 1

Views: 1332

Answers (2)

Alix Axel
Alix Axel

Reputation: 154643

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.

Upvotes: 0

toneplex
toneplex

Reputation: 673

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

Upvotes: 1

Related Questions