Domanda

AGGIORNAMENTO

ho fatto le modifiche apportate al codice C # in modo che utilizza una dimensione di blocco di 256. Ma ora il ciao sguardi mondo come questo http://pastebin.com/5sXhMV11 e Non riesco a capire cosa devo usare con rtrim () per ottenere giro del caos alla fine.

Anche quando si dice il IV dovrebbe essere casuale, da questo vuoi dire non utilizzare lo stesso IV più di una volta o è il modo in cui ho codificato male?

Grazie ancora!

Ciao,

Sto cercando di decifrare una stringa con PHP che è stato codificato in C #. Io non riesco a ottenere PHP per decifrare utilizzando mcrypt e potrebbe fare con un po 'di aiuto per favore. Ottengo il seguente errore con PHP in modo da sto indovinando non sto impostando la IV correttamente.

Errore: Il parametro IV deve essere lungo quanto la dimensione del blocco

Entrambe le funzioni utilizzano lo stesso cifrario, chiave, IV e impostato in modalità CBC:

testo cifrato da C # = == UmzUCnAzThH0nMkIuMisqg
chiave 32 lungo = qwertyuiopasdfghjklzxcvbnmqwerty
iv 16 = lunga 1234567890123456

C #

    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;
    }

PHP

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;
}

Grazie

È stato utile?

Soluzione

Se si desidera utilizzare Rijndael256 in C # applicazione è necessario impostare la dimensione dei blocchi a 256.

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

E poi il tuo iv deve essere di 256 bit di lunghezza pure.
vedi SymmetricAlgorithm.BlockSize Proprietà


O il contrario: Attualmente il tuo # applicazione C utilizza Rijndael128 e così deve lo script 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);

stampe hello world

Altri suggerimenti

Encrypt utilizzando 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);

Decrypt utilizzando 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);

dove pKeyFilename è un file Personal Information Exchange creato con il file del certificato cert.crt. Questo esempio utilizza una crittografia AES-256.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top