将PHP加密代码转换为C#(mcrypt_encrypt)

时间:2019-01-09 20:37:01

标签: c# php encryption

我在PHP中有几行代码,我想在C#中使用相同的代码 我尝试自己翻译,但无法

这是我的代码:

$data = array(
  'id' => 1,
  'name' => 'Custom Login',
  'avatar' => 'http://example.com/avatar/1.png',
  'expiration' => round(microtime(true) * 1000) + 60*60*1000 // in millisecond
);

$data = json_encode($data);

$blocksize = 16;

$secret = '';
$md5 = md5($secret);

// Strictly maintains the length of key and iv
$key = substr($md5, 0, 16);
$iv = substr($md5, 16, 16);

// We need to pad the input manually to match with the server-side's padding scheme
$pad = $blocksize - (strlen($data) % $blocksize);
$data = $data . str_repeat(chr($pad), $pad);

$encryptedSession = bin2hex(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, $iv));

我现在熟悉PHP,并且这些方法在PHP和c#中似乎具有不同的功能

这是我尝试过的代码:

public static string CreateMD5(string input)
            {
                using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
                {
                    byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
                    byte[] hashBytes = md5.ComputeHash(inputBytes);

                    // Convert the byte array to hexadecimal string
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < hashBytes.Length; i++)
                    {
                        sb.Append(hashBytes[i].ToString("X2"));
                    }
                    return sb.ToString();
                }
            }

上面获取MD5的部分

static public String EncryptRJ256(string data,int blockSize,string secret)
        {
            var md5 = CreateMD5(secret);

            var encoding = new UTF8Encoding();
            var Key = md5.Substring(0, 16);
            var IV = md5.Substring(16,16);
            var pad = blockSize - (data.Length % blockSize);

            data = string.Join("", Enumerable.Repeat((char)pad, 2));
            byte[] encrypted;
            using (var rj = new RijndaelManaged())
            {
                try
                {
                    rj.Padding = PaddingMode.PKCS7;
                    rj.Mode = CipherMode.CBC;
                    rj.KeySize = 256;
                    rj.BlockSize = blockSize;
                    rj.Key = Encoding.ASCII.GetBytes(Key);
                    rj.IV = Encoding.ASCII.GetBytes(IV);

                    var ms = new MemoryStream();

                    using (var cs = new CryptoStream(ms, rj.CreateEncryptor(rj.Key, rj.IV), CryptoStreamMode.Write))
                    {
                        using (var sr = new StreamWriter(cs))
                        {
                            sr.Write(data);
                            sr.Flush();
                            cs.FlushFinalBlock();
                        }
                        encrypted = ms.ToArray();
                    }
                }
                finally
                {
                    rj.Clear();
                }
            }

            return Convert.ToBase64String(encrypted);
        }

上面模拟php mcrypt_encrypt的部分

有什么建议吗?

0 个答案:

没有答案