Encrypt код из C# в PHP

Рейтинг: 0Ответов: 1Опубликовано: 10.03.2023

Есть код на C# (VS2022):

using System.Security.Cryptography;
using System.Text;

string _privateKey = "XyLfIyZt";
string Encrypt(string text, Int64 publicKey)
{
    try
    {
        var sha256 = SHA256.Create();
        var md5 = MD5.Create();
        var privateBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(_privateKey));
        var publicBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(publicKey.ToString()));
        using (var cryptoProvider = new RijndaelManaged())
        using (var memoryStream = new MemoryStream())
        using (var cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(privateBytes, publicBytes), CryptoStreamMode.Write))
        using (var writer = new StreamWriter(cryptoStream))
        {
            writer.Write(text);
            writer.Flush();
            cryptoStream.FlushFinalBlock();
            writer.Flush();
            return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
        }
    }
    catch (Exception ex)
    {
        return ex.Message;
    }
}

Int64 publicKey = 1000000001;
string text = "YES, DAMN IT!";

string doneencryption = Encrypt(text, publicKey);

Console.Write(doneencryption);

Тот же код для online редактора:

using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;

public static class Encryption
{
    static string _privateKey = "XyLfIyZt";
    static string Encrypt(string text, Int64 publicKey)
    {
        try
        {
            var sha256 = SHA256.Create();
            var md5 = MD5.Create();
            var privateBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(_privateKey));
            var publicBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(publicKey.ToString()));
            using (var cryptoProvider = new RijndaelManaged())
            using (var memoryStream = new MemoryStream())
            using (var cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(privateBytes, publicBytes), CryptoStreamMode.Write))
            using (var writer = new StreamWriter(cryptoStream))
            {
                writer.Write(text);
                writer.Flush();
                cryptoStream.FlushFinalBlock();
                writer.Flush();
                return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int) memoryStream.Length);
            }
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }

    static void Main(string[] args)
    {
        Int64 publicKey = 1000000001;
        string text = "YES, DAMN IT!";

        string doneencryption = Encrypt(text, publicKey);

        Console.Write(doneencryption);
    }
}

Нужно это чудо перевести на PHP

Что бы не казаться ленивым, вот выжимка из того, что попробовал:

<?php

$text = "YES, DAMN IT!";
$need = "nuUf4djaIYorpbd6M0L5xw==";
$privateKey = "XyLfIyZt";
$publicKey = 1000000001;

$response = [];

$cipher = MCRYPT_RIJNDAEL_128;
$iv = substr(md5($publicKey), 0, mcrypt_get_iv_size($cipher, MCRYPT_MODE_CBC));
$key = hash('sha256', utf8_encode($privateKey), true);
$ciphertext = mcrypt_encrypt($cipher, $key, $text, MCRYPT_MODE_CBC, $iv);
$ciphertext = base64_encode($iv . $ciphertext);
$response['ciphertext1'] = $ciphertext;

$cipher = "AES-256-CBC";
$iv = substr(md5(utf8_encode($publicKey)), 0, openssl_cipher_iv_length($cipher));
$key = hash('sha256', utf8_encode($privateKey));
$ciphertext = openssl_encrypt($text, $cipher, $key, OPENSSL_RAW_DATA, $iv);
$ciphertext = base64_encode($ciphertext);
$response['ciphertext2'] = $ciphertext;

$cipher = "AES-256-CBC";
$iv = substr(md5(utf8_encode($publicKey)), 0, openssl_cipher_iv_length($cipher));
$key = hash('sha256', utf8_encode($privateKey), true);
$ciphertext = openssl_encrypt($text, $cipher, $key, OPENSSL_RAW_DATA, $iv);
$ciphertext = base64_encode($ciphertext);
$response['ciphertext3'] = $ciphertext;

$cipher = "AES-256-CBC";
$iv = substr(md5(utf8_encode($publicKey)), 0, openssl_cipher_iv_length($cipher));
$key = hash('sha256', utf8_encode($privateKey), true);
$ciphertext = openssl_encrypt($text, $cipher, $key, OPENSSL_RAW_DATA, $iv);
$hmac = hash_hmac('sha256', $ciphertext.$iv, $key, true);
$response['ciphertext4'] = base64_encode($iv.$hmac.$ciphertext);

$response['-== need =--'] = $need;

print_r($response);

Естественно, с параметрами игрался как только мог
Возможно не попал в нужную комбинацию...

Ответы

▲ 0

Технологии вытесняют умы, на ChatGP получил ответ:

<?php

class Encrypt {
    private static $_privateKey = "XyLfIyZt";
    public static function encrypt($text, $publicKey) {
        try {
            $sha256 = hash('sha256', self::$_privateKey, true);
            $md5 = hash('md5', strval($publicKey), true);
            $cryptoProvider = new \phpseclib\Crypt\Rijndael();
            $cryptoProvider->setKey($sha256);
            $cryptoProvider->setIV($md5);
            $encrypted = $cryptoProvider->encrypt($text);
            return base64_encode($encrypted);
        } catch (Exception $ex) {
            return $ex->getMessage();
        }
    }
}

$publicKey = 1000000001;
$text = "section engineering education";
$doneencryption = Encrypt::encrypt($text, $publicKey);
echo $doneencryption;

Если без библиотеки, то так:

<?php

$cipher = "AES-256-CBC";
$key = hash('sha256', $privateKey, true);
$iv = hash('md5', strval($publicKey), true);
$ciphertext = openssl_encrypt($text, $cipher, $key, OPENSSL_RAW_DATA, $iv);
$ciphertext = base64_encode($ciphertext);
echo $ciphertext;