6 Replies - 359 Views - Last Post: 12 June 2020 - 12:05 PM Rate Topic: -----

#1 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2757
  • View blog
  • Posts: 8,059
  • Joined: 15-January 14

Converting C# to PHP

Posted 10 June 2020 - 07:20 PM

If anyone knows C#, check my work on converting this function. It's not decrypting the file correctly, I've got some questions into my counterpart on the other end but if anyone spots any mistakes I'd appreciate it.

The C#:
private static void FileDecrypt(string inputFile, string outputFile, string password)
        {
            byte[] passwordBytes = System.Text.Encoding.UTF8.GetBytes(password);
            byte[] salt = new byte[32];
            FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
            fsCrypt.Read(salt, 0, salt.Length);

            RijndaelManaged AES = new RijndaelManaged();
            AES.KeySize = 256;
            AES.BlockSize = 128;
            var key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
            AES.Key = key.GetBytes(AES.KeySize / 8);
            AES.IV = key.GetBytes(AES.BlockSize / 8);
            AES.Padding = PaddingMode.PKCS7;
            AES.Mode = CipherMode.CFB;

            CryptoStream cs = new CryptoStream(fsCrypt, AES.CreateDecryptor(), CryptoStreamMode.Read);
            FileStream fsOut = new FileStream(outputFile, FileMode.Create);

            int read;
            byte[] buffer = new byte[1048576];

            try
            {
                while ((read = cs.Read(buffer, 0, buffer.Length)) > 0)
                {
                    //Application.DoEvents();
                    fsOut.Write(buffer, 0, read);
                }
            }
            catch (CryptographicException ex_CryptographicException)
            {
                Console.WriteLine("CryptographicException error: " + ex_CryptographicException.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
        }


My work in progress:

function decryptCustomFile($inFile, $outFile, $password) {
    $saltLength = 32; // bytes
    $keyBytes = 256 / 8;
    $ivBytes = 128 / 8;

    echo 'Decrypt "'.$inFile.'"<br>';

    $in = fopen($inFile, 'rb');
    if (!$in) {
        return false;
    }

    $salt = fread($in, $saltLength);
    $data = fread($in, filesize($inFile));
    fclose($in);

    echo 'Read ' . strlen($data) . ' bytes of data<br>';

    $randomBytesLength = ($keyBytes + $ivBytes);
    echo 'Getting ' . $randomBytesLength . ' random bytes: ';
    $randomBytes = openssl_pbkdf2($password, $salt, $randomBytesLength, 50000);
    echo bin2hex($randomBytes) . '<br>';
    $key = substr($randomBytes, 0, $keyBytes);
    $iv = substr($randomBytes, $keyBytes);

    echo 'Salt: ' . bin2hex($salt) . '<br>';
    echo 'Key: ' . bin2hex($key) . '<br>';
    echo 'IV: ' . bin2hex($iv) . '<br>';

    $text = openssl_decrypt($data, 'aes-256-cfb', $key, 0, $iv);
    file_put_contents($outFile, $text);

    while ($msg = openssl_error_string())
        echo $msg . "<br />\n";

    return true;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Converting C# to PHP

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,993
  • Joined: 08-August 08

Re: Converting C# to PHP

Posted 10 June 2020 - 08:27 PM

Could the file have a BOM?

Are you sure the encryption algorithm is the same for both functions?
Was This Post Helpful? 0
  • +
  • -

#3 Ornstein   User is offline

  • D.I.C Head

Reputation: 105
  • View blog
  • Posts: 216
  • Joined: 13-May 15

Re: Converting C# to PHP

Posted 11 June 2020 - 08:55 AM

If I recall, openssl_decrypt will expect the data to be base64-encoded (and may return a base64-encoded result) if you don't pass OPENSSL_RAW_DATA as the 4th argument.

That's the first thing that comes to mind, anyway. Everything else seems fine at first glance.
Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2757
  • View blog
  • Posts: 8,059
  • Joined: 15-January 14

Re: Converting C# to PHP

Posted 11 June 2020 - 11:19 AM

Quote

Could the file have a BOM?

The encrypted version wouldn't. Maybe the decrypted one does.

Quote

Are you sure the encryption algorithm is the same for both functions?

I'm about 95% sure, based on reading the documentation for the .NET classes used in that code and finding the relevant PHP.

Quote

If I recall, openssl_decrypt will expect the data to be base64-encoded (and may return a base64-encoded result) if you don't pass OPENSSL_RAW_DATA as the 4th argument.

I'll check into that. I had him change from CFB to CBC, apparently you can do some things in .NET with Rijndael and CFB that make it incompatible with AES, so I'll look at the new file and check that option you mentioned.

Currently I'm turning a 410KB encrypted file into a 9 byte decrypted file. So, amazing compression, but something is weird.
Was This Post Helpful? 0
  • +
  • -

#5 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2757
  • View blog
  • Posts: 8,059
  • Joined: 15-January 14

Re: Converting C# to PHP

Posted 11 June 2020 - 11:31 AM

Trying to use CBC was a no-go, errors about the wrong block size. But I did add the raw option and at least got a file that was about the same size back. I remember looking through the documentation yesterday and seeing "Takes a raw or base64 encoded string and decrypts it" and for the briefest second considered how it knows but didn't look at the options.
Was This Post Helpful? 0
  • +
  • -

#6 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2757
  • View blog
  • Posts: 8,059
  • Joined: 15-January 14

Re: Converting C# to PHP

Posted 11 June 2020 - 01:36 PM

I think the problem is on his side, the file is full of "efbfbd" sequences:

https://www.dreaminc...nd-byte-arrays/
Was This Post Helpful? 0
  • +
  • -

#7 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2757
  • View blog
  • Posts: 8,059
  • Joined: 15-January 14

Re: Converting C# to PHP

Posted 12 June 2020 - 12:05 PM

Here's my final code, there were a bunch of 0x10 characters at the end for some reason. The problem was on the other side during the FTP upload, not the encryption.

    $saltLength = 32; // bytes
    $keyBytes = 256 / 8;
    $ivBytes = 128 / 8;

    $in = fopen($inFile, 'rb');
    if (!$in) {
        return false;
    }

    $salt = fread($in, $saltLength);
    $data = fread($in, filesize($inFile));
    fclose($in);

    $randomBytesLength = ($keyBytes + $ivBytes);
    $randomBytes = openssl_pbkdf2($password, $salt, $randomBytesLength, 50000);
    $key = substr($randomBytes, 0, $keyBytes);
    $iv = substr($randomBytes, $keyBytes);

    $text = openssl_decrypt($data, 'aes-256-cfb', $key, OPENSSL_RAW_DATA, $iv);
    file_put_contents($outFile, trim($text, " \t\n\r\0\x0B\x10"));

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1