This is code from Pascal:
x := Ord(s[i]) xor (Key shr 8);
Is it possible to write it in PHP?
This is code from Pascal:
x := Ord(s[i]) xor (Key shr 8);
Is it possible to write it in PHP?
It’s possible, yes. PHP’s bit shifting is arithmetic rather than logical which means you’ll need to do extra checking around your shr
translation. If you provide the rest of the surrounding context of your code, there might be a neater way to get the result you want.
Thank you for the reply.
Here is the complete function:
function EncryptStr(const S :WideString; Key: Word): String;
var i :Integer;
RStr :RawByteString;
RStrB :TBytes Absolute RStr;
begin
Result:= '';
RStr:= UTF8Encode(S);
for i := 0 to Length(RStr)-1 do begin
RStrB[i] := RStrB[i] xor (Key shr 8);
Key := (RStrB[i] + Key) * CKEY1 + CKEY2;
end;
for i := 0 to Length(RStr)-1 do begin
Result:= Result + IntToHex(RStrB[i], 2);
end;
end;
Taken from here: https://stackoverflow.com/questions/6798188/delphi-simple-string-encryption
But it is actually only this part I am trying to convert to php:
What is the real problem you are trying to solve by doing this?
Thanks for the reply.
What is the real problem you are trying to solve by doing this?
I need to write a Encryption-function in PHP, similar to the Pascal encryption-function.
<?php
class Aes
{
public $key ;
public $iv = '1234567890123456';
public function __construct()
{
$this->key = '1234567890123456';
}
public function decode($str)
{
return openssl_decrypt(base64_decode($str),"AES-128-CBC",$this->key,OPENSSL_RAW_DATA, $this->iv);
}
public function encode($str)
{
return base64_encode(openssl_encrypt($str,"AES-128-CBC",$this->key,OPENSSL_RAW_DATA, $this->iv));
}
}
$aes = new AES();
$encryptedMessage = $aes->encode('My Secret Message');
var_dump($encryptedMessage); // 4jZf0a8oV3Xa5e0TyI7EcLAI3FGstD9Hn6teGkzjFIQ=
var_dump($aes->decode($encryptedMessage));// My Secret Message
Libsodium Example (Php >= 7.2)
* Need to add extension=php_sodium.dll to php.ini
$msg = 'Secret message!';
$key = random_bytes(SODIUM_CRYPTO_SECRETBOX_KEYBYTES);
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
// Encrypt
$ciphertext = sodium_crypto_secretbox($msg, $nonce, $key);
// Decrypt
$plaintext = sodium_crypto_secretbox_open($ciphertext, $nonce, $key);
echo $plaintext === $msg ? 'Success' : 'Error';
Do you need it to encrypt using the same algorithm that the Pascal function does, so that it produces the same output for a given input? PHP has several encryption functions built into it, could you use one of those?
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.