RSA签名函数问题

发布于 2024-11-15 12:31:25 字数 2263 浏览 1 评论 0原文

我正在开发 RSA sign() 函数,用于生成私人流媒体的签名 URL。我正在测试 PHP 代码,但我想在 Flex 中重新编码。这是 PHP 代码的一部分:

function getCannedPolicy($resource, $expires, $key, $privatekeyfile){
         $priv_key = file_get_contents($privatekeyfile);
         $pkeyid = openssl_get_privatekey($priv_key);
         $policy_str = '{"Statement":[{"Resource":"'.$resource.'","Condition":{"DateLessThan":{"AWS:EpochTime":'.$expires.'}}}]}';
         $policy_str = trim( preg_replace( '/\s+/', '', $policy_str ) );
         $res = openssl_sign($policy_str, $signature, $pkeyid, OPENSSL_ALGO_SHA1);
         $signature_base64 = (base64_encode($signature));
         $repl = array('+' => '-','=' => '_','/' => '~');
         $signature_base64 = strtr($signature_base64,$repl);
         $url = $resource . '?Expires='.$expires. '&Signature=' . $signature_base64 . '&Key-Pair-Id='. $key;

         return $url;
}

我在 Flex 中编写了相同的函数。代码如下:

private function getCannedPolicy(resource:String, expires:uint, key:String, privatekey:String):String{          
    var unsigned:String = '{"Statement":[{"Resource":"' +resource+ '","Condition":{"DateLessThan":{"AWS:EpochTime":' +expires+ '}}}]}';
    var signed:String = '';
    var signature:String = '';
    var regex:RegExp = /\s+/g;          
    unsigned = unsigned.replace(regex,'');
    var src:ByteArray = new ByteArray();            
    src.writeUTFBytes(unsigned);            
    var dst:ByteArray = new ByteArray();            
    var hash:SHA1 = new SHA1();
    src = hash.hash(src);                       
    var rsa:RSAKey = PEM.readRSAPrivateKey(privatekey);
    trace(rsa.dump());
    rsa.sign(src, dst, src.length);
    dst.position = 0;           
    signature = Base64.encodeByteArray(dst);                            
    signature = signature.split("+").join("-");
    signature = signature.split("=").join("_");
    signature = signature.split("\/").join("~");
    signed = resource+'?Expires=' +expires+ '&Signature=' +signature+ '&Key-Pair-Id=' +key; 

    return signed;
}

两个函数(PHP 和 Flex)的输出格式相同。但是,当我使用 Flex 函数中的签名 URL 时,该流无法工作。

我用于 openssl_sign() php 函数的替代方案是 as3crypto 库中的 sign() 函数。也许问题就在这里?也许加密方式不同。

I'm working on an RSA sign() function for generating a signed URL for private streaming. I was testing on PHP code, but I want to re-code that in Flex. Here is the part of PHP code:

function getCannedPolicy($resource, $expires, $key, $privatekeyfile){
         $priv_key = file_get_contents($privatekeyfile);
         $pkeyid = openssl_get_privatekey($priv_key);
         $policy_str = '{"Statement":[{"Resource":"'.$resource.'","Condition":{"DateLessThan":{"AWS:EpochTime":'.$expires.'}}}]}';
         $policy_str = trim( preg_replace( '/\s+/', '', $policy_str ) );
         $res = openssl_sign($policy_str, $signature, $pkeyid, OPENSSL_ALGO_SHA1);
         $signature_base64 = (base64_encode($signature));
         $repl = array('+' => '-','=' => '_','/' => '~');
         $signature_base64 = strtr($signature_base64,$repl);
         $url = $resource . '?Expires='.$expires. '&Signature=' . $signature_base64 . '&Key-Pair-Id='. $key;

         return $url;
}

I write the same function in Flex. Here is the code:

private function getCannedPolicy(resource:String, expires:uint, key:String, privatekey:String):String{          
    var unsigned:String = '{"Statement":[{"Resource":"' +resource+ '","Condition":{"DateLessThan":{"AWS:EpochTime":' +expires+ '}}}]}';
    var signed:String = '';
    var signature:String = '';
    var regex:RegExp = /\s+/g;          
    unsigned = unsigned.replace(regex,'');
    var src:ByteArray = new ByteArray();            
    src.writeUTFBytes(unsigned);            
    var dst:ByteArray = new ByteArray();            
    var hash:SHA1 = new SHA1();
    src = hash.hash(src);                       
    var rsa:RSAKey = PEM.readRSAPrivateKey(privatekey);
    trace(rsa.dump());
    rsa.sign(src, dst, src.length);
    dst.position = 0;           
    signature = Base64.encodeByteArray(dst);                            
    signature = signature.split("+").join("-");
    signature = signature.split("=").join("_");
    signature = signature.split("\/").join("~");
    signed = resource+'?Expires=' +expires+ '&Signature=' +signature+ '&Key-Pair-Id=' +key; 

    return signed;
}

The outputs from the two functions (the PHP and the Flex) are the same format. But, when I'm using the signed URL from the Flex function, the stream not work.

The alternative I'm using for openssl_sign() php function is sign() function from as3crypto library. Maybe here is the problem? Maybe the encryption is different.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅忆流年 2024-11-22 12:31:25

不幸的是,as3crypto 的 RSAKey.sign() 与 php 的 openssl_sign() 不是同一个函数。他们的输出是不同的签名。因此,我决定调用远程 php 函数来生成我的签名。现在可以了!

Unfortunately, the as3crypto's RSAKey.sign() is not the same function as php's openssl_sign(). Their outputs are different signatures. For that reason I decide to call remote php function to generated my signature. It works now!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文