将C#AES加密代码转换为php

时间:2016-03-16 05:17:08

标签: c# php

这是我的c#代码我必须将其转换为php。输出这段代码是 FblN3XOgPFBhPbALglniBw ==所以在php中它应该是相同的 我试过但有线请求帮助

EncryptAes("999002:GuestCSL", "FREO2016"); 
public static byte[] EncryptAes(string input, string key)
    {
        var keyArray = HashSha256(key);
        var toEncryptArray = Encoding.ASCII.GetBytes(input);

        byte[] result;

        using (var aes = new AesCryptoServiceProvider
        {
            Key = keyArray,
            Mode = CipherMode.ECB,
            Padding = PaddingMode.PKCS7
        })
        {
          var cTransform = aes.CreateEncryptor();
          result = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            aes.Clear();
        }
        return result;
    }

    static byte[] HashSha256(string phrase)
    {
        var encoder = new UTF8Encoding();
        var sha256Hasher = new SHA256CryptoServiceProvider();
        var hashedDataBytes = sha256Hasher.ComputeHash(encoder.GetBytes(phrase));
        return hashedDataBytes;
    }

1 个答案:

答案 0 :(得分:0)

<?php
  $key = hash('sha256', "key here", true);  
  $text = "username:password";


  function pkcs7_pad($text, $blocksize)
  {
       $pad = $blocksize - (strlen($text) % $blocksize);
       return $text . str_repeat(chr($pad), $pad);
  }

  $input = pkcs7_pad($text, 16);

  echo  base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $input,  MCRYPT_MODE_ECB));