PHP:
$key = "testtesttest";
$text = "bublijuja";
$iv = openssl_random_pseudo_bytes( openssl_cipher_iv_length("blowfish"));
for($i = 0; $i < strlen($iv); $i++)
{
echo ord($iv[$i])." ";
}
echo "\n";
$et = openssl_encrypt( $text, "blowfish", $key, OPENSSL_RAW_DATA, $iv );
for($i = 0; $i < strlen($et); $i++)
{
echo ord($et[$i])." ";
}
echo "\n";
此打印(第一行为IV,第二行为加密文本:
253 145 220 198 224 78 40 124
208 51 12 30 46 92 13 181 19 210 50 57 174 207 93 130
将该IV复制到戈朗:
package main
import (
"golang.org/x/crypto/blowfish"
"crypto/cipher"
"fmt"
)
func main() {
key := "testtesttest"
plaintext := "bublijuja"
byteSlice := []int{253, 145, 220, 198, 224, 78, 40, 124}
iv := make([]byte, len(byteSlice))
for i, b := range byteSlice {
iv[i] = byte(b)
}
fmt.Println(iv)
ciphertext := make([]byte, blowfish.BlockSize+len(plaintext))
block, err := blowfish.NewCipher([]byte(key))
if err != nil {
fmt.Println(err.Error())
return
}
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(ciphertext, pad([]byte(plaintext)))
fmt.Println(ciphertext)
}
func pad(pt []byte) []byte {
// calculate modulus of plaintext to blowfish's cipher block size
// if result is not 0, then we need to pad
modulus := len(pt) % blowfish.BlockSize
if modulus != 0 {
// how many bytes do we need to pad to make pt to be a multiple of
//blowfish's block size?
padlen := blowfish.BlockSize - modulus
// let's add the required padding
for i := 0; i < padlen; i++ {
// add the pad, one at a time
pt = append(pt, 0)
}
}
// return the whole-multiple-of-blowfish.BlockSize-sized plaintext
// to the calling function
return pt
}
我得到:
[253 145 220 198 224 78 40 124]
[61 82 97 183 42 220 119 173 114 107 250 139 174 236 113 91 0]
我也尝试过欧洲央行的模式。我能匹配前8个字节一次,但我搞砸了。我试图弄清楚php版本是如何处理它的,以便与go实现相匹配,但到目前为止我失败了。
发布于 2020-09-17 12:14:15
以下问题导致了不同的结果:
OPENSSL_DONT_ZERO_PAD_KEY
。如果额外设置了此标志(OPENSSL_RAW_DATA | OPENSSL_DONT_ZERO_PAD_KEY
),则以下输出结果(在所需的环境中):
253 145 220 198 224 78 40 124 61 82 97 97 183 42 220 119 173 26 156 153 152 139 105 237
这里有一个可以设置标志的联机PHP环境。pad
函数中的pad
)。https://stackoverflow.com/questions/63934788
复制相似问题