Decrypt data from PHP in Xojo

Recently we discussed how to encrypt something in PHP and then decrypt in Xojo. Check out the CipherMBS class in our plugin, which corresponds to openssl_encrypt/openssl_decrypt functions in PHP.

Here is a sample script in PHP to generate a random initial vector and a random key. Then encrypt some text with openssl using blowfish algorithm and CBC as block mode:

$cipher = 'bf-cbc';
$iv_size = openssl_cipher_iv_length($cipher);
$key_size = 20;
$iv = random_bytes($iv_size);
$key = random_bytes($key_size);

$encrypted = openssl_encrypt('Hello World. Just a test here!', $cipher, $key, OPENSSL_RAW_DATA, $iv);

print "iv: ".base64_encode($iv)."\n";
print "key: ".base64_encode($key)."\n";
print "encrypted: ".base64_encode($encrypted)."\n";

For such an encryption, you need to replicate on both sides the same settings:

  • Identical key data. May need you to hash key first.
  • Identical initial vector
  • Identical setting for padding of data to match required block size.
  • Identical handling for keys with too small size. e.g. fill up with zero bytes.

You can run the PHP script above and copy the values into the Xojo code below:

Var Cipher As CipherMBS = CipherMBS.bf_cbc Call Cipher.EncryptInit(DecodeBase64("79aMfVRxPw0="), DecodeBase64("2peenIoTK1k=")) Var Decrypted As MemoryBlock = Cipher.ProcessMemory(DecodeBase64("JBvDgbeC6vECJGiRnh27GQ==")) + Cipher.FinalizeAsMemory() Print Decrypted

If everything works fine, the result in the unencrypted text as in the PHP script.

1 Like

Hi,
CipherMBS class from the MBS Xojo Encryption Plugin.


Dim cipher As New CipherMBS
cipher.Mode = CipherMBS.bf_cbc

Dim iv As MemoryBlock = DecodeBase64("79aMfVRxPw0=")
Dim key As MemoryBlock = DecodeBase64("2peenIoTK1k=")
cipher.SetKey(key)

Dim encryptedData As MemoryBlock = DecodeBase64("JBvDgbeC6vECJGiRnh27GQ==")

Dim decryptedData As MemoryBlock
If cipher.DecryptInit(iv) Then
  decryptedData = cipher.ProcessMemory(encryptedData)
  decryptedData = decryptedData + cipher.FinalizeAsMemory()
  
   Convert the decrypted data to a string
  Dim decryptedText As String = decryptedData.StringValue(0, decryptedData.Size, Encodings.UTF8)
  
  MsgBox decryptedText
End If

Make sure MBS Xojo Encryption Plugin installed and properly configured in your Xojo project. Replace the values of the iv, key , and encryptedData variables with the corresponding values generated by the PHP script.
The decrypted data will be stored in the decryptedData MemoryBlock, and you can convert it to a string using the appropriate encoding (in this example, UTF-8).
Now you can display the decrypted text using a MsgBox or perform any other desired processing.

1 Like