Home > Software design >  Using R to decrypt messages encrypted in PHP
Using R to decrypt messages encrypted in PHP

Time:05-15

my brain hurts, I can't figure it out, I would be tremendously thankful for your help. I have a database that gets encrypted at storage using PHP. I need to decrypt it in R. I feel like I am close, but I just can't get there.

Here's the PHP code used to encrypt:

<?php

function str_encryptaesgcm($plaintext, $password) {
$iv = "1234567890123456";
$ciphertext = openssl_encrypt(
    $plaintext, 
    "AES-256-CBC", 
    hash('sha256', $password, true), 
    OPENSSL_RAW_DATA, 
    $iv);
return base64_encode($ciphertext);
}

function str_decryptaesgcm($ciphertext, $password) {
$iv = "1234567890123456";
return openssl_decrypt(
        base64_decode($ciphertext), 
        "AES-256-CBC", 
        hash('sha256', $password, true), 
        OPENSSL_RAW_DATA, 
        $iv);
}

$enc = str_encryptaesgcm("Hello, world!", "ABCDEFGHIJKLMNOPQRST");
echo $enc . " ";
$enc = "8FT21xlAENs0Q8GTDE5k0A==";
$dec = str_decryptaesgcm($enc, "ABCDEFGHIJKLMNOPQRST");
echo $dec;

Here, "8FT21xlAENs0Q8GTDE5k0A==" is the encrypted result of the "Hello, world!" message that I have in my database. If I run it through the str_decryptaesgcm function it spits out the original message. I need to decode this in R.

To replicate this process in R one needs to run the following code:

library(openssl)
x <- aes_cbc_encrypt(serialize('Hello, world!', NULL), 
                     key = sha256(charToRaw('ABCDEFGHIJKLMNOPQRST')), 
                     iv = charToRaw('1234567890123456'))
y <- aes_cbc_decrypt(
  data = x, 
  key = sha256(charToRaw('ABCDEFGHIJKLMNOPQRST')), 
  iv = charToRaw('1234567890123456'))
unserialize(y)

with unserialize(y) spitting out the original message.

However, base64_encode(x) produces a string that is completely different from the one produced in PHP: "1y6CgaY23ap tQVXQKGsYyZWDMGj/GxeHjyyFOnyRJufbfieRC4aJ7/9uDzRllC21Q7v 1bADtuzEfG83iakBw==".

I can get R to produce the PHP string, but I need to run the encryption differently:

x <- aes_cbc_encrypt(charToRaw('Hello, world!'), 
                     key = sha256(charToRaw('ABCDEFGHIJKLMNOPQRST')), 
                     iv = charToRaw('1234567890123456'))

Then, base64_encode(x) produces "8FT21xlAENs0Q8GTDE5k0A==". However, plugging this into the decoder produces garbage (note that it does decode, it doesn't throw an error) that I can't convert:

> y <- aes_cbc_decrypt(
    data = x, 
    key = sha256(charToRaw('ABCDEFGHIJKLMNOPQRST')), 
    iv = charToRaw('1234567890123456'))
> y
 [1] 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21
> unserialize(y)
Error in unserialize(y) : unknown input format

So the question is, what should I do to be able to convert "8FT21xlAENs0Q8GTDE5k0A==" back to the original message? I'd really appreciate some advice.

CodePudding user response:

Yes, you were incredibly close. If you look carefully at y:

y
#> [1] 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21

You will see these are the ascii values of your original message as a raw vector. So you need only do:

rawToChar(y)
#> [1] "Hello, world!"
  • Related