dongshetao1814 2014-07-17 18:01
浏览 45
已采纳

从as3crypto解密php中的AES-CBC

I've found a lot of discussions on this but none seem to get this working for me so any help is appreciated. I'm encoding some text in flash using the as3crypto library and then sending that encrypted text to a php script where I need to decode it. The encryption and decryption works just fine inside of flash but I'm unable to get it to decrypt in php.

Here is the code I have in Flash

private static const KEY:String = "AxiKzCRH5arSABesX9bH2lTSxYmAGEEz";

private static function encrypt(input:String, key:String, algorithm:String = "aes-cbc", padding:String = "None"):String {
    var kdata:ByteArray = Base64.decodeToByteArray(key);
    var data:ByteArray = Hex.toArray(Hex.fromString(input));
    var pad:IPad = padding == "pkcs5" ? new PKCS5 : new NullPad;
    var mode:ICipher = Crypto.getCipher("simple-" + algorithm, kdata, pad);
    pad.setBlockSize(mode.getBlockSize());
    mode.encrypt(data);

    return Hex.fromArray(data);
}

var text-to-send-to-php:String = encrypt('test text', KEY);

And here is the code I have so far in php that isn't working

<?php
$key = 'AxiKzCRH5arSABesX9bH2lTSxYmAGEEz';
$encrypted_text = the-text-i-get-from-flash;

$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');

$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_URANDOM);
mcrypt_generic_init($td, $key, $iv);

$s = mdecrypt_generic($td, $encrypted_text);
echo 'decrypted = ' . $s;

mcrypt_generic_deinit($td);
mcrypt_module_close($td);
?>
  • 写回答

1条回答 默认 最新

  • doufei1893 2014-07-17 19:37
    关注

    So I figured it was an issue with encoding/decoding, but it was also an issue with the IV. Here is the final working code in php

    <?php
    $key = base64_decode('AxiKzCRH5arSABesX9bH2lTSxYmAGEEz');
    $encrypted_text = the-text-i-get-from-flash;
    $encrypted_text = hex2bin($encrypted_text);
    
    $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
    $blocksize = mcrypt_enc_get_block_size($td);
    
    $iv = substr($encrypted_text, 0, $blocksize);
    $encrypted_text = substr($encrypted_text, $blocksize);
    
    mcrypt_generic_init($td, $key, $iv);
    
    $username = mdecrypt_generic($td, $encrypted_text);
    
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵