Hey Leute um einen string zu verschlüsseln und zu entschlüsseln verwende ich:
Nun werden jedoch Sonderzeichen also z.B. '€','$',',' etc.. nicht richtig ver/entschlüsselt. Beispiel:
Kennt da jmd ne gute lösung für das Problem?
Gruß
PHP-Code:
class Encryption
{
const CIPHER = MCRYPT_RIJNDAEL_128;
const MODE = MCRYPT_MODE_CBC;
private $key;
public function __construct($key) {
$this->key = $key;
}
public function encrypt($plaintext) {
$ivSize = mcrypt_get_iv_size(self::CIPHER, self::MODE);
$iv = mcrypt_create_iv($ivSize, MCRYPT_DEV_RANDOM);
$ciphertext = mcrypt_encrypt(self::CIPHER, $this->key, $plaintext, self::MODE, $iv);
return base64_encode($iv.$ciphertext);
}
public function decrypt($ciphertext) {
$ciphertext = base64_decode($ciphertext);
$ivSize = mcrypt_get_iv_size(self::CIPHER, self::MODE);
if (strlen($ciphertext) < $ivSize) {
throw new Exception('Missing initialization vector');
}
$iv = substr($ciphertext, 0, $ivSize);
$ciphertext = substr($ciphertext, $ivSize);
$plaintext = mcrypt_decrypt(self::CIPHER, $this->key, $ciphertext, self::MODE, $iv);
return rtrim($plaintext, "\0");
}
}
PHP-Code:
$key = '24-stelliger-key';
$crypt = new Encryption($key);
$encrypted_string = $crypt->encrypt('$');
$key = 'der-gleiche-24-stellige-key';
$crypt = new Encryption($key);
$decrypted_string = $crypt->decrypt($encrypted_string);
echo $decrypted_string;
// output: %E2%82%AC
Gruß
Kommentar