5 Kasım 2021

PHP - .NET Uyumlu AES Şifreleme

 C#

using System.Security.Cryptography;

using System.Text;


namespace ConsoleApp16

{

    public class Program

    {

        public static void Main()

        {

            var plain = "Lorem Ipsum";

            var encryptSettings = new EncryptSettings();

            var encryptTools = new EncryptTools(encryptSettings);

            var encrypted = encryptTools.AesEncrypt(plain);

            Console.WriteLine("Encrypted");

            Console.WriteLine(encrypted);

            Console.WriteLine();

            Console.WriteLine("Decrypted");

            var text = encryptTools.AesDecrypt(encrypted);

            Console.WriteLine(text);

        }

    }

    public class EncryptSettings

    {

        public string Key { get; set; }


        public EncryptSettings()

        {

            Key = "passwordstring";

        }

    }


    public class EncryptTools

    {

        private readonly EncryptSettings encryptSettings;


        public EncryptTools(EncryptSettings encryptSettings)

        {

            this.encryptSettings = encryptSettings;

        }


        private byte[] key => Sha256Byte(encryptSettings.Key);

        private byte[] iv => new byte[16] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };


        public string AesEncrypt(string plainText)

        {

            if (plainText == null || plainText.Length <= 0)

                throw new ArgumentNullException("plainText");

            if (key == null || key.Length <= 0)

                throw new ArgumentNullException("Key");

            if (iv == null || iv.Length <= 0)

                throw new ArgumentNullException("IV");

            byte[] encrypted;


            using (var aes = Aes.Create())

            {

                aes.Mode = CipherMode.CBC;

                aes.Key = key;

                aes.IV = iv;


                using (var ms = new MemoryStream())

                {

                    using (ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV))

                    using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))

                    using (StreamWriter swEncrypt = new StreamWriter(cs))

                    {

                        swEncrypt.Write(plainText);

                    }

                    encrypted = ms.ToArray();

                }

            }

            return Convert.ToBase64String(encrypted);

        }


        public string AesDecrypt(string encryptedText)

        {

            var cipherText = Convert.FromBase64String(encryptedText);


            if (cipherText == null || cipherText.Length <= 0)

                throw new ArgumentNullException("cipherText");

            if (key == null || key.Length <= 0)

                throw new ArgumentNullException("Key");

            if (iv == null || iv.Length <= 0)

                throw new ArgumentNullException("IV");


            string plaintext = null;


            using (Aes aes = Aes.Create())

            {

                aes.Mode = CipherMode.CBC;

                aes.Key = key;

                aes.IV = iv;


                using (MemoryStream msDecrypt = new MemoryStream(cipherText))

                using (ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV))

                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))

                using (StreamReader srDecrypt = new StreamReader(csDecrypt))

                {

                    plaintext = srDecrypt.ReadToEnd();

                }

            }


            return plaintext;

        }


        public byte[] Sha256Byte(string plainText)

        {

            byte[] bytes = null;

            using (SHA256 sha256Hash = SHA256.Create())

            {

                bytes = sha256Hash.ComputeHash(Encoding.ASCII.GetBytes(plainText));

            }

            return bytes;

        }

    }


}

PHP

<?php

function encrypt_decrypt($string, $action = 'encrypt')
{
$encrypt_method = "aes-256-cbc";
$secret_key = 'passwordstring'; // user define private key
$key = substr(hash('sha256', $secret_key, true), 0, 32);
$iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0);
if ($action == 'encrypt') {
$output = openssl_encrypt($string, $encrypt_method, $key, OPENSSL_RAW_DATA, $iv);
$output = base64_encode($output);
} else if ($action == 'decrypt') {
$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, OPENSSL_RAW_DATA, $iv);
}
return $output;
}
echo encrypt_decrypt('CSBx7hQRszpDKM9Vfxy8qQ==', 'decrypt');

?>