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

一个 aes-js 库 里加密算法用 PHP 怎么写?

  •  
  •   wangwenfan · 2019-12-11 18:46:45 +08:00 · 1268 次点击
    这是一个创建于 1589 天前的主题,其中的信息可能已经有所发展或是发生改变。

    文档地址:https://github.com/ricmoo/aes-js#readme

    下面是官方文档 demo 代码

    const aesjs = require('aes-js');
    // An example 128-bit key
    var key = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ];
    
    // The initialization vector (must be 16 bytes)
    var iv = [ 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,35, 36 ];
    
    // Convert text to bytes (must be a multiple of the segment size you choose below)
    var text = 'TextMustBeAMultipleOfSegmentSize';
    var textBytes = aesjs.utils.utf8.toBytes(text);
    
    // The segment size is optional, and defaults to 1
    var segmentSize = 8;
    // console.log(iv);
    // 
    var aesCfb = new aesjs.ModeOfOperation.cfb(key, iv, segmentSize);
    // var encryptedBytes = aesCfb.encrypt(textBytes);
    
    // To print or store the binary data, you may convert it to hex
    var encryptedHex = aesjs.utils.hex.fromBytes(encryptedBytes);
    console.log(encryptedHex);
    // "55e3af2638c560b4fdb9d26a630733ea60197ec23deb85b1f60f71f10409ce27"
    
    // When ready to decrypt the hex string, convert it back to bytes
    var encryptedBytes = aesjs.utils.hex.toBytes(encryptedHex);
    
    // The cipher feedback mode of operation maintains internal state,
    // so to decrypt a new instance must be instantiated.
    var aesCfb = new aesjs.ModeOfOperation.cfb(key, iv, 8);
    var decryptedBytes = aesCfb.decrypt(encryptedBytes);
    
    // Convert our bytes back into text
    var decryptedText = aesjs.utils.utf8.fromBytes(decryptedBytes);
    console.log(decryptedText);
    // "TextMustBeAMultipleOfSegmentSize"
    
    • 怎么改造下用 php 来写?
    <?php
    // 加密算法
    $encryptMethod = 'aes-128-cfb';
    // 明文数据
    $data = 'TextMustBeAMultipleOfSegmentSize';
    $secret = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
    // 生成 IV
    $ivLength = openssl_cipher_iv_length($encryptMethod);
    $iv = openssl_random_pseudo_bytes($ivLength, $isStrong);
    if (false === $iv && false === $isStrong) {
    	die('IV generate failed');
    }
    // 加密
    $encrypted = openssl_encrypt($data, $encryptMethod, $secret, 8, $iv);
    
    
    3 条回复    2019-12-11 20:02:56 +08:00
    zy445566
        1
    zy445566  
       2019-12-11 18:58:41 +08:00 via Android
    写个锤子,用 node 起 http 服务,用 PHP 去掉用不就完事了
    xiangyuecn
        2
    xiangyuecn  
       2019-12-11 18:58:58 +08:00
    用固定 iv、密码、填充方式 不管用什么语言同一明文 AES 加密出来都是一样的。所以不用考虑怎么改造。。。直接研究 PHP 怎么进行 AES 加密。注意:js 那里的 iv 是固定的,不能乱写。
    wangwenfan
        3
    wangwenfan  
    OP
       2019-12-11 20:02:56 +08:00
    @xiangyuecn 我有点不懂的是上面 js 的 iv 定义的是数组,看了文档 php 的 iv 一般是字符串,这两个怎么来统一
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5433 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 08:17 · PVG 16:17 · LAX 01:17 · JFK 04:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.