سؤال

محدث

لقد قمت بإجراء التغييرات على رمز C# بحيث يستخدم حجم كتلة 256. ولكن الآن يبدو عالم Hello هكذا http://pastebin.com/5SXHMV11 ولا يمكنني معرفة ما يجب أن أستخدمه مع rtrim () للحصول على ركوب الفوضى في النهاية.

أيضًا عندما تقول إن IV يجب أن يكون عشوائيًا ، فهذا يعني أنك لا تستخدم نفس الرابع أكثر ثم مرة واحدة أو هي الطريقة التي قمت بترميزها بشكل خاطئ؟

شكرًا لك مرة أخرى!

أهلاً،

أحاول فك تشفير سلسلة مع PHP تم تشفيرها في C#. لا يبدو لي أن أحصل على PHP لفك تشفيره باستخدام Mcrypt ويمكن أن أفعل ببعض المساعدة من فضلك. أحصل على الخطأ التالي مع PHP لذلك أعتقد أنني لا أقوم بتعيين IV بشكل صحيح.

خطأ: يجب أن تكون المعلمة IV طالما أن الكتل

تستخدم كلتا الوظيفة نفس الشفرات والمفتاح والرابع وتعيينها على وضع CBC:

النص المشفر من c# = umzucnazthh0nmkiumisqg ==
المفتاح 32 طويل = qwertyuiopasdfghjklzxcvbnmqwerty
الرابع 16 طويل = 1234567890123456

ج#

    public static string EncryptString(string message, string KeyString, string IVString)
    {
        byte[] Key = ASCIIEncoding.UTF8.GetBytes(KeyString);
        byte[] IV = ASCIIEncoding.UTF8.GetBytes(IVString);

        string encrypted = null;
        RijndaelManaged rj = new RijndaelManaged();
        rj.Key = Key;
        rj.IV = IV;
        rj.Mode = CipherMode.CBC;

        try
        {
            MemoryStream ms = new MemoryStream();

            using (CryptoStream cs = new CryptoStream(ms, rj.CreateEncryptor(Key, IV), CryptoStreamMode.Write))
            {
                using (StreamWriter sw = new StreamWriter(cs))
                {
                    sw.Write(message);
                    sw.Close();
                }
                cs.Close();
            }
            byte[] encoded = ms.ToArray();
            encrypted = Convert.ToBase64String(encoded);

            ms.Close();
        }
        catch (CryptographicException e)
        {
            Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
            return null;
        }
        catch (UnauthorizedAccessException e)
        {
            Console.WriteLine("A file error occurred: {0}", e.Message);
            return null;
        }
        catch (Exception e)
        {
            Console.WriteLine("An error occurred: {0}", e.Message);
        }
        finally
        {
            rj.Clear();
        }

        return encrypted;
    }

بي أتش بي

var $mcrypt_cipher = MCRYPT_RIJNDAEL_256;
var $mcrypt_mode = MCRYPT_MODE_CBC;

function decrypt($key, $iv, $encrypted)
{
    $encrypted = base64_decode($encrypted);

    $decrypted = rtrim(mcrypt_decrypt($this->mcrypt_cipher, $key, $encrypted, $this->mcrypt_mode, $iv), "\0");;
    return $decrypted;
}

شكرًا

هل كانت مفيدة؟

المحلول

إذا كنت ترغب في استخدام Rijndael256 في تطبيق C# الخاص بك ، فعليك تعيين الكتل على 256.

RijndaelManaged rj = new RijndaelManaged();
rj.BlockSize = 256;

ثم يجب أن يكون IV الخاص بك 256 بت طويلة أيضًا.
يرى synmetricalgorithm.blocksize خاصية


أو العكس الجولة: حاليًا يستخدم تطبيق C# Rijndael128 ، وكذلك يجب على البرنامج النصي PHP الخاص بك.

<?php
class Foo {
  protected $mcrypt_cipher = MCRYPT_RIJNDAEL_128;
  protected $mcrypt_mode = MCRYPT_MODE_CBC;

  public function decrypt($key, $iv, $encrypted)
  {
    $iv_utf = mb_convert_encoding($iv, 'UTF-8');
    return mcrypt_decrypt($this->mcrypt_cipher, $key, base64_decode($encrypted), $this->mcrypt_mode, $iv_utf);
  }
}



$encrypted = "UmzUCnAzThH0nMkIuMisqg==";
$key = "qwertyuiopasdfghjklzxcvbnmqwerty";
$iv = "1234567890123456";

$foo = new Foo;
echo $foo->decrypt($key, $iv, $encrypted);

مطبوعات hello world

نصائح أخرى

تشفير باستخدام PHP ؛

/Generate public key for encrytion
$path = "keys/";

    $crt = openssl_x509_read(file_get_contents($path."cert.crt"));
    $publickey = openssl_get_publickey($crt);

    //Encrypt using public key
    openssl_public_encrypt($source, $crypted, $publickey);

    //openssl_private_encrypt($source, $crypted, $privkey);
    echo base64_encode($crypted);

فك التشفير باستخدام C#

    X509Certificate2 x509cert = new X509Certificate2(pKeyFilename);
    RSACryptoServiceProvider.UseMachineKeyStore = false;
    RSACryptoServiceProvider crypt = (RSACryptoServiceProvider)x509cert.PrivateKey;                

    byte[] decrypted = crypt.Decrypt(Convert.FromBase64String(data), false);
    return ASCIIEncoding.UTF8.GetString(decrypted);

حيث يكون PkeyFilename ملف تبادل المعلومات الشخصي الذي تم إنشاؤه باستخدام ملف الشهادة cert.crt. تستخدم هذه الأمثلة تشفير AES-256.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top