C # Rijndael algoritması için bu PHP kodu dönüştürmek

4 Cevap

Ben bu php kodu var ve ben tam eşdeğer C # almak istiyorum

$ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_192, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($ivSize, MCRYPT_RAND);
$encryptedData = mcrypt_encrypt(MCRYPT_RIJNDAEL_192, $key, $salt . $message . $nonce, MCRYPT_MODE_CBC, $iv);
$base64Data = base64_encode($salt . $iv . $encryptedData);
$urlEncodedData = rawurlencode($base64Data);

Tüm katkıları minnetle aldı!

4 Cevap

Hayır Sayers ve azap tacirleri, işte!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Web;

namespace EncryptData
{
    class EncryptData
    {
    private static readonly Encoding ASCII_ENCODING = new System.Text.ASCIIEncoding();

    private static string md5(string text)
    {
        return BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(ASCII_ENCODING.GetBytes(text))).Replace("-", "").ToLower();
    }

    public abstract string nonce();

    public abstract string salt();

    public readonly string EncryptedData;

    public EncryptData(string message)
    {

        // set up encrytion object
        RijndaelManaged aes192 = new RijndaelManaged();
        aes192.KeySize = 192;
        aes192.BlockSize = 192;
        aes192.Padding = PaddingMode.None;
        aes192.Mode = CipherMode.CBC;
        aes192.Key = ASCII_ENCODING.GetBytes(md5(SECRET_KEY));
        aes192.GenerateIV();

        string localSalt = salt();
        string localNonce = nonce();

        // form the string for encrypting
        // and put into byte array
        string textToEncrypt = localSalt + message+ localNonce;
        byte[] plainTextBytes = ASCII_ENCODING.GetBytes(textToEncrypt);

        // encrypt the data            
        ICryptoTransform encryptor = aes192.CreateEncryptor();
        MemoryStream ms = new MemoryStream();

        CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write);
        cs.Write(plainTextBytes, 0, plainTextBytes.Length);

        // convert our encrypted data from a memory stream into a byte array.
        byte[] cypherTextBytes = ms.ToArray();

        // close memory stream
        ms.Close();

        byte[] combined = null;
        // combine data and convert to byte array
        combined = CombinedData(localSalt, aes192.IV, cypherTextBytes);
        // url encode data once converted to base64 string
        EncryptedData = HttpUtility.UrlEncode(base64CombinedData(combined), ASCII_ENCODING);


    }

    public byte[] CombinedData(string salt, byte[] IV, byte[] cypherTextBytes)
    {
        // convert salt string into byte array
        byte[] saltBytes = ASCII_ENCODING.GetBytes(salt);

        // catenate all the byte arrays into one
        // set up dest byte array with required size
        byte[] rv = new byte[saltBytes.Length + IV.Length + cypherTextBytes.Length];

        // copy in each byte array
        Buffer.BlockCopy(saltBytes, 0, rv, 0, saltBytes.Length);
        Buffer.BlockCopy(IV, 0, rv, saltBytes.Length, IV.Length);
        Buffer.BlockCopy(cypherTextBytes, 0, rv, saltBytes.Length + IV.Length, cypherTextBytes.Length);

        return rv;
    }

    public string base64CombinedData(byte[] rv)
    {
        return Convert.ToBase64String(rv);
    }

}
}

Doğrudan dönüştürmek için çok fazla değişken; Ben ne yaptığını incelemek ve orijinal kod niyet uymak için C # bunu yeniden öneririz.

Üzgünüz, ama dürüst olmak gerekirse - soru oldukça saçma.

All this code does is some nasty crypting and encoding. PHP is full of global functions from modules that do the thing. For C# You need to find proper libs/classes that have methods for crypting etc. It will probably ahve different logic and parameters. I bet You'll even get some output differences due to different implementations.

I this Sizin soruya bir cevap bulacaksınız yakın şey olduğunu varsayalım

Bu yol MCRYPT_ENCRYPT () padding ekler gibi Rob tarafından sağlanan örnekte ben, onun yerine PaddingMode.None arasında PaddingMode.Zero kullanılmış olması gerektiğini düşünüyorum.