将C#中的AES加密令牌代码转换为php

将C#中的AES加密令牌代码转换为php,c#,php,encryption,aes,C#,Php,Encryption,Aes,我有下面的.Net代码,它接受两个输入。1) 128位base 64编码密钥和2)用户标识。它输出AES加密令牌。 我需要相同代码的php等价物,但不知道RNGCryptoServiceProvider、RijndaelManaged、ICryptoTransform、MemoryStream和CryptoStream将使用哪些对应的php类。 我被卡住了,所以任何关于这方面的帮助都将非常感激 using System; using System.Text; using System.IO; u

我有下面的.Net代码,它接受两个输入。1) 128位base 64编码密钥和2)用户标识。它输出AES加密令牌。 我需要相同代码的php等价物,但不知道RNGCryptoServiceProvider、RijndaelManaged、ICryptoTransform、MemoryStream和CryptoStream将使用哪些对应的php类。 我被卡住了,所以任何关于这方面的帮助都将非常感激

using System;
using System.Text;
using System.IO;
using System.Security.Cryptography;

class AESToken
{
  [STAThread]
  static int Main(string[] args)
  {
    if (args.Length != 2)
    {
      Console.WriteLine("Usage: AESToken key userId\n");
      Console.WriteLine("key Specifies 128-bit AES key base64 encoded supplied by MediaNet to the partner");
      Console.WriteLine("userId specifies the unique id");
      return -1;
    }

    string key = args[0];
    string userId = args[1];

    StringBuilder sb = new StringBuilder();
    // This example code uses the magic string “CAMB2B”. The implementer
    // must use the appropriate magic string for the web services API.
    sb.Append("CAMB2B");
    sb.Append(args[1]);     // userId
    sb.Append('|');  // pipe char
    sb.Append(System.DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ssUTC")); //timestamp
    Byte[] payload = Encoding.ASCII.GetBytes(sb.ToString());

    byte[] salt = new Byte[16];  // 16 bytes of random salt
    RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
    rng.GetBytes(salt);  // the plaintext is 16 bytes of salt followed by the payload.

    byte[] plaintext = new byte[salt.Length + payload.Length];
    salt.CopyTo(plaintext, 0);
    payload.CopyTo(plaintext, salt.Length);

    // the AES cryptor: 128-bit key, 128-bit block size, CBC mode
    RijndaelManaged cryptor = new RijndaelManaged();
    cryptor.KeySize = 128;
    cryptor.BlockSize = 128;
    cryptor.Mode = CipherMode.CBC;
    cryptor.GenerateIV();
    cryptor.Key = Convert.FromBase64String(args[0]); // the key
    byte[] iv = cryptor.IV;   // the IV.

    // do the encryption
    ICryptoTransform encryptor = cryptor.CreateEncryptor(cryptor.Key, iv);
    MemoryStream ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write);
    cs.Write(plaintext, 0, plaintext.Length);
    cs.FlushFinalBlock();
    byte[] ciphertext = ms.ToArray();
    ms.Close();
    cs.Close();

    // build the token
    byte[] tokenBytes = new byte[iv.Length + ciphertext.Length];
    iv.CopyTo(tokenBytes, 0);
    ciphertext.CopyTo(tokenBytes, iv.Length);
    string token = Convert.ToBase64String(tokenBytes);
    Console.WriteLine(token);
    return 0;
   }
}
请帮忙。
谢谢。

您可以使用PHP中的库来实现相同的功能。

我们也在尝试找出PHP中相同的C。您可以在不使用密钥的情况下发布代码

第一种方法:

    // Open the cipher:
    // Using Rijndael 128 in CBC mode.
    $m = mcrypt_module_open('rijndael-128', '', 'cbc', ''); 
    $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($m), MCRYPT_RAND); 

    // Initialize the encryption:
    mcrypt_generic_init($m, (base64_decode($key_)), $iv);

    // Encrypt the data:
    $cryptext = mcrypt_generic($m, $plain_text);

    //echo "IV SIZE ".mcrypt_enc_get_iv_size($m);
    $tx2 =  base64_encode($iv.$cipherText);

    // Close the encryption handler:
    mcrypt_generic_deinit($m);

    // Close the cipher:
    mcrypt_module_close($m);
第二种初始化方法:

    $m = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
    $iv_size = mcrypt_enc_get_iv_size($m); 
    $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($m), MCRYPT_RAND); 
    $key128 = base64_decode($key_);

    // Encrypt the data:
    $cryptext = mcrypt_generic($m, $plain_text);

    $tx2 =  base64_encode($iv.$cipherText);

    // Close the encryption handler:
    mcrypt_generic_deinit($m); 

您可以看到以下有效的代码:

<?php

class UserData
{
    public $email;
    public $name;
    public $expires;
}

class Application 
{
    private $api_key = "<private_key>";
    private $app_key = "appkey";

    public function run()
    {
       $user = new UserData();
       $date = new DateTime(null, new DateTimeZone('UTC'));
       $date->modify('+5 minute');
       $user->expires = $date->format('c');
       $user->email = "testing@domain.com";
       $user->name = "PHP5 Example";
       $encrypted_data = $this->encryptUserData($user);
       // Example login URL
       printf("http://<domain>/multipass?sso=%s", $encrypted_data);
    }

    private function encryptUserData($user_data)
    {
        $app_key = $this->app_key;
        $api_key = $this->api_key;
        $json = json_encode($user_data);

        $salted = $api_key . $app_key;
        $saltedHash = substr(sha1($salted, true), 0, 16);

        $pad = 16 - (strlen($json) % 16);
        $data = $json . (str_repeat(chr($pad), $pad));

        if (!function_exists('mcrypt_encrypt'))
            throw new Exception('Mcrypt extension is not installed for PHP.');
        $aes = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $saltedHash, $data, MCRYPT_MODE_CBC, str_repeat("\0", 16));

        $b64token = base64_encode($aes);
        $b64token = rtrim(str_replace(array('+', '/'), array('-', '_'), $b64token), '=');

        return $b64token;
    }
}

$app = new Application();
$app->run();

?>

我希望这对你有帮助。谢谢