云转码密钥算法,php和.net版

发布时间:2017-03-14 16:52:57
云转码最新版地址下载:http://www.yunzhuanma.com/anzhuang/2019-10-20/192.html
应用代码:下面的91flv是默认的云转码后台的密钥,你的云转码后台设置的什么,这里就对应修改为什么
 
php算法示范:
 
<?php
$time=time().'000';
$ip=get_real_ip();
$sign=bin2hex(aes128Encrypt('91flv',"timestamp=".$time."&ip=".$ip));
function aes128Encrypt($key, $data) {
    $padding = 16 - (strlen($data) % 16);
    $data .= str_repeat(chr($padding), $padding);
    $keySize   = 16;
    $ivSize    = 16;
    $rawKey = $key;
    $genKeyData = '';
    do
    {
        $genKeyData = $genKeyData.md5( $genKeyData.$rawKey, true );
    } while( strlen( $genKeyData ) < ($keySize + $ivSize) );
    $generatedKey = substr( $genKeyData, 0, $keySize );
    $generatedIV  = substr( $genKeyData, $keySize, $ivSize );
    return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $generatedKey, $data, MCRYPT_MODE_CBC, $generatedIV);
}
function get_real_ip(){
$ip=false;
if(!empty($_SERVER["HTTP_CLIENT_IP"])){
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
for ($i = 0; $i < count($ips); $i++) {
if (!eregi ("^(10|172.16|192.168).", $ips[$i])) {
$ip = $ips[$i];
break;
}
}
}
return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}
?>
 把以上算法放在动态页面,然后在播放下面的index.m3u8地址这里加上?sign=<?php echo $sgin; ?>
 
以下是asp.net算法:
 
        private string GetSign()
        {
            string data = "timestamp=" + ConvertDateTimeInt(DateTime.Now).ToString() + "&ip=" + System.Web.HttpContext.Current.Request.UserHostAddress;
            ViewBag.signq = data;
            string key = "91flv";
            var padding = 16 - (data.Length % 16);
            data += new string((char)padding, padding);
            var keySize = 16;
            var ivSize = 16;
            var rawKey = Encoding.Default.GetBytes(key);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] genKeyData = md5.ComputeHash(rawKey);
            while (genKeyData.Length < (keySize + ivSize))
            {
                genKeyData = MergerArray(genKeyData, md5.ComputeHash(MergerArray(genKeyData, rawKey)));
            }
 
 
            var generatedKey = genKeyData.Take(keySize).ToArray();
            var generatedIV = genKeyData.Skip(keySize).Take(ivSize).ToArray();
          return   ByteToString(AESEncrypt(data, generatedKey, generatedIV));
 
        }
 
        public byte[] MergerArray(byte[] First, byte[] Second)
        {
            byte[] result = new byte[First.Length + Second.Length];
            First.CopyTo(result, 0);
            Second.CopyTo(result, First.Length);
            return result;
        }
 
        public static byte[] AESEncrypt1(String Data, String Key)
        {
 
            DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
 
            DES.Key = Encoding.UTF8.GetBytes(Key);
            DES.Mode = CipherMode.CBC;
            DES.Padding = PaddingMode.Zeros;
 
            ICryptoTransform DESEncrypt = DES.CreateEncryptor();
           // System.Text.Encoding
          return  Encoding.UTF8.GetBytes(Data);
           // return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
        }
 
        /// <summary>  
        /// AES加密(无向量)  
        /// </summary>  
        /// <param name="plainBytes">被加密的明文</param>  
        /// <param name="key">密钥</param>  
        /// <returns>密文</returns>  
        public static byte[] AESEncrypt(String Data, String Key)
        {
            MemoryStream mStream = new MemoryStream();
            RijndaelManaged aes = new RijndaelManaged();
 
            byte[] plainBytes = Encoding.UTF8.GetBytes(Data);
            Byte[] bKey = new Byte[32];
            Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
 
            aes.Mode = CipherMode.ECB;
            aes.Padding = PaddingMode.PKCS7;
            aes.KeySize = 128;
            //aes.Key = _key;  
            aes.Key = bKey;
            //aes.IV = _iV;  
            CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateEncryptor(), CryptoStreamMode.Write);
            try
            {
                cryptoStream.Write(plainBytes, 0, plainBytes.Length);
                cryptoStream.FlushFinalBlock();
                return mStream.ToArray();
                //return Convert.ToBase64String(mStream.ToArray());
            }
            finally
            {
                cryptoStream.Close();
                mStream.Close();
                aes.Clear();
            }
        }  
 
        /// <summary>
        /// 将c# DateTime时间格式转换为Unix时间戳格式
        /// </summary>
        /// <param name="time">时间</param>
        /// <returns>long</returns>
        public static long ConvertDateTimeInt(System.DateTime time)
        {
            //double intResult = 0;
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
            //intResult = (time- startTime).TotalMilliseconds;
            long t = (time.Ticks - startTime.Ticks) / 10000;            //除10000调整为13位
            return t;
        }
 
        public  string ByteToString(byte[] InBytes)
        {
            string StringOut = "";
            foreach (byte InByte in InBytes)
            {
                StringOut = StringOut + String.Format("{0:X2} ", InByte).Trim();
            }
            return StringOut;
        }
 
 
        public string ByteToString1(byte[] InBytes)
        {
            string StringOut = "";
            foreach (byte InByte in InBytes)
            {
                StringOut = StringOut + (char)InByte;
            }
            return StringOut;
        }
        /// <summary>
        /// 有密码的AES加密 
        /// </summary>
        /// <param name="text">加密字符</param>
        /// <param name="password">加密的密码</param>
        /// <param name="iv">密钥</param>
        /// <returns></returns>
        public static byte[] AESEncrypt(string text, byte[] password, byte[] iv)
        {
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
 
            rijndaelCipher.Mode = CipherMode.CBC;
 
            rijndaelCipher.Padding = PaddingMode.Zeros;
 
            rijndaelCipher.KeySize = 128;
 
            rijndaelCipher.BlockSize = 128;
 
            byte[] pwdBytes = password;
 
            byte[] keyBytes = new byte[16];
 
            int len = pwdBytes.Length;
 
            if (len > keyBytes.Length) len = keyBytes.Length;
 
            System.Array.Copy(pwdBytes, keyBytes, len);
 
            rijndaelCipher.Key = keyBytes;
 
 
            byte[] ivBytes =iv;
            rijndaelCipher.IV = ivBytes;
 
            ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
 
            byte[] plainText = Encoding.UTF8.GetBytes(text);
 
            byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);
 
            return cipherBytes;
 
        }
 
        /// <summary>
        /// 随机生成密钥
        /// </summary>
        /// <returns></returns>
        public static string GetIv(int n)
        {
            char[] arrChar = new char[]{
           'a','b','d','c','e','f','g','h','i','j','k','l','m','n','p','r','q','s','t','u','v','w','z','y','x',
           '0','1','2','3','4','5','6','7','8','9',
           'A','B','C','D','E','F','G','H','I','J','K','L','M','N','Q','P','R','T','S','V','U','W','X','Y','Z'
          };
 
            StringBuilder num = new StringBuilder();
 
            Random rnd = new Random(DateTime.Now.Millisecond);
            for (int i = 0; i < n; i++)
            {
                num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString());
 
            }
 
            return num.ToString();
        }
 
        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="text"></param>
        /// <param name="password"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        public static string AESDecrypt(string text, string password, string iv)
        {
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
 
            rijndaelCipher.Mode = CipherMode.CBC;
 
            rijndaelCipher.Padding = PaddingMode.PKCS7;
 
            rijndaelCipher.KeySize = 128;
 
            rijndaelCipher.BlockSize = 128;
 
            byte[] encryptedData = Convert.FromBase64String(text);
 
            byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);
 
            byte[] keyBytes = new byte[16];
 
            int len = pwdBytes.Length;
 
            if (len > keyBytes.Length) len = keyBytes.Length;
 
            System.Array.Copy(pwdBytes, keyBytes, len);
 
            rijndaelCipher.Key = keyBytes;
 
            byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv);
            rijndaelCipher.IV = ivBytes;
 
            ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
 
            byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
 
            return Encoding.UTF8.GetString(plainText);
 
        }