V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
icemanpro
V2EX  ›  PHP

mcrypt 问题

  •  
  •   icemanpro · 2014-07-29 19:04:12 +08:00 · 2227 次点击
    这是一个创建于 3556 天前的主题,其中的信息可能已经有所发展或是发生改变。
    <?php
    /* Open the cipher */
    $td = mcrypt_module_open('rijndael-256', '', 'ofb', '');

    /* Create the IV and determine the keysize length, use MCRYPT_RAND
    * on Windows instead */
    $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM);
    $ks = mcrypt_enc_get_key_size($td);

    /* Create key */
    $key = substr(md5('very secret key'), 0, $ks);

    /* Intialize encryption */
    mcrypt_generic_init($td, $key, $iv);

    /* Encrypt data */
    $encrypted = mcrypt_generic($td, 'This is very important data');
    echo (base64_encode($encrypted));

    ?>

    像这样,每次生成的encrypted都不一样. 那怎么能下次再解密?
    1 条回复    2014-07-30 03:26:06 +08:00
    vixvix
        1
    vixvix  
       2014-07-30 03:26:06 +08:00
    你把mdecrypt_generic的文档和例子看看. 我没用过这个库,理论上来说,你只要用相同模块rijndael-256, 和相同的key - 'very secret key'就可以解开加密的文件。加密的data由于使用random生成initialization vector(iv), 所以生成的加密数据每次也不同,具体细节可以参考使用的加密模块,例如这里用到的rijndael-256.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5327 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 08:17 · PVG 16:17 · LAX 01:17 · JFK 04:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.