AES-128 点击率的输出与高浪和节点 J 具有不同的输出

无法弄清楚PHP openssl_encrypt是如何工作的,也无法在GoLang和NodeJs中重现其输出,以下是PHP中的简化代码 - 输出:hvAB


<?php

$string = 'aaa';

$cipher = "AES-128-CTR";

$options = 0;

$encryption_iv = '1234567890123456';

$encryption_key = 'bc7316929fe1545bf0b98d114ee3ecb8';

$encryption = openssl_encrypt($string, $cipher, $encryption_key, $options, $encryption_iv);

echo $encryption; // hvAB

在GoLang中,假设密钥必须进行十六进制解码才能获得所需的长度16,以便使用AES 128 - 输出:PQ5k


package main


import (

    "crypto/aes"

    "crypto/cipher"

    "encoding/base64"

    "encoding/hex"

    "fmt"

)


func main() {

    plainText := "aaa"

    fmt.Println(encryption(plainText)) // PQ5k

}


func encryption(plainText string) string {

    bytes := []byte(plainText)

    blockCipher := createCipher()

    stream := cipher.NewCTR(blockCipher, []byte("1234567890123456"))

    stream.XORKeyStream(bytes, bytes)

    return base64.StdEncoding.EncodeToString(bytes)

}


func createCipher() cipher.Block {

    key, _ := hex.DecodeString("bc7316929fe1545bf0b98d114ee3ecb8")

    block, err := aes.NewCipher(key)

    if err != nil {

        panic(err)

    }

    return block

}

在节点Js中 - 输出:PQ5k


var crypto = require('crypto');

var algorithm = 'aes-128-ctr';


function encrypt(text, password) {

  const key = Buffer.from(password, "hex").slice(0, 16);

  const ivBuffer = Buffer.from("1234567890123456");

  const cipher = crypto.createCipheriv(algorithm, key, ivBuffer);

  let encrypted = cipher.update(text,'utf8','base64') +  cipher.final('base64')

  console.log(encrypted) // PQ5k

}


encrypt('aaa', 'bc7316929fe1545bf0b98d114ee3ecb8');

起初认为这是一个编码问题,但我认为这是正确的 - 将返回base64值。我需要将PHP变体翻译成GoLang,但是(几乎)任何其他语言的示例将不胜感激。openssl_encrypt


繁星coding
浏览 71回答 2
2回答

慕桂英546537

在PHP代码中,密钥不是十六进制解码的,而是一个大小为32字节的二进制字符串,因此对于AES-128来说太大了。PHP/OpenSSL 通过仅考虑前 16 个字节来隐式截断密钥。在 Go 中,只需使用key := []byte("bc7316929fe1545b")在节点JS中:获取 PHP 结果。const key = Buffer.from("bc7316929fe1545b", "utf8")相反,在 PHP 代码中,密钥也可以使用 进行十六进制解码。hex2bin()

跃然一笑

这里的 c/c++ 版本,但输出与 golang/nodejs 还不一样。&nbsp; &nbsp; &nbsp; &nbsp;#include <openssl/aes.h>&nbsp; &nbsp; &nbsp; &nbsp;#include <stdio.h>&nbsp; &nbsp; &nbsp; &nbsp;#include <string.h>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;int main() {&nbsp; &nbsp; &nbsp; &nbsp; AES_KEY aes_key;&nbsp; &nbsp; &nbsp; &nbsp; unsigned char key[AES_BLOCK_SIZE] = {0xbc, 0x73, 0x16, 0x92, 0x9f, 0xe1, 0x54, 0x5b, 0xf0, 0xb9, 0x8d, 0x11, 0x4e, 0xe3, 0xec, 0xb8 };&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; unsigned char iv[AES_BLOCK_SIZE]&nbsp; = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6};&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; unsigned char ecount[AES_BLOCK_SIZE]; memset( ecount, 0, 16 );&nbsp; &nbsp; &nbsp; &nbsp; unsigned int num = 0 ;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; const&nbsp; char *x = "aaa";&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; unsigned char out[16];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; AES_set_encrypt_key(key, 128, &aes_key);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; AES_ctr128_encrypt( (const unsigned char *) x, out, AES_BLOCK_SIZE, &aes_key, iv, ecount, &num);&nbsp; &nbsp; &nbsp; &nbsp; for (int k = 0; k < 3; k++)&nbsp; printf("%02x", out[k]); // c9aa18&nbsp; &nbsp; &nbsp; &nbsp; printf( "\n");&nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;// g++ -o aes-simple aes-simple.cpp&nbsp; -I/usr/local/ssl/include&nbsp; -L/usr/local/ssl/lib -g&nbsp; &nbsp; -lcrypto&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go