PHP openssl des-ede3-cbc 转换为 Golang

huangapple go评论90阅读模式
英文:

PHP openssl des-ede3-cbc to Golang

问题

以下是用Go实现相同功能的代码:

package main

import (
	"crypto/cipher"
	"crypto/des"
	"crypto/rand"
	"encoding/hex"
	"fmt"
	"io"
)

func main() {
	message := "your_message"
	key := []byte("your_key")

	l := (len(message) + 7) / 8 * 8
	paddedMessage := message + string([]byte{0}) * (l - len(message))

	block, err := des.NewTripleDESCipher(key)
	if err != nil {
		fmt.Printf("err: %s", err)
		return
	}

	iv := make([]byte, des.BlockSize)
	if _, err := io.ReadFull(rand.Reader, iv); err != nil {
		fmt.Printf("err: %s", err)
		return
	}

	cipherText := make([]byte, len(paddedMessage))
	encryptMode := cipher.NewCBCEncrypter(block, iv)
	encryptMode.CryptBlocks(cipherText, []byte(paddedMessage))

	str := hex.EncodeToString(cipherText)

	fmt.Printf("Cipher text: %s", str)
}

请注意,你需要将your_message替换为要加密的实际消息,将your_key替换为实际的密钥。

英文:

I've following code in PHP:

$l = ceil(strlen($message) / 8) * 8;
$enc = substr(openssl_encrypt($message . str_repeat("
$l = ceil(strlen($message) / 8) * 8;
$enc = substr(openssl_encrypt($message . str_repeat("\0", $l - strlen($message)), 'des-ede3-cbc', $key, OPENSSL_RAW_DATA, "\0\0\0\0\0\0\0\0"), 0, $l);
", $l - strlen($message)), 'des-ede3-cbc', $key, OPENSSL_RAW_DATA, "
$l = ceil(strlen($message) / 8) * 8;
$enc = substr(openssl_encrypt($message . str_repeat("\0", $l - strlen($message)), 'des-ede3-cbc', $key, OPENSSL_RAW_DATA, "\0\0\0\0\0\0\0\0"), 0, $l);
$l = ceil(strlen($message) / 8) * 8;
$enc = substr(openssl_encrypt($message . str_repeat("\0", $l - strlen($message)), 'des-ede3-cbc', $key, OPENSSL_RAW_DATA, "\0\0\0\0\0\0\0\0"), 0, $l);
$l = ceil(strlen($message) / 8) * 8;
$enc = substr(openssl_encrypt($message . str_repeat("\0", $l - strlen($message)), 'des-ede3-cbc', $key, OPENSSL_RAW_DATA, "\0\0\0\0\0\0\0\0"), 0, $l);
$l = ceil(strlen($message) / 8) * 8;
$enc = substr(openssl_encrypt($message . str_repeat("\0", $l - strlen($message)), 'des-ede3-cbc', $key, OPENSSL_RAW_DATA, "\0\0\0\0\0\0\0\0"), 0, $l);
$l = ceil(strlen($message) / 8) * 8;
$enc = substr(openssl_encrypt($message . str_repeat("\0", $l - strlen($message)), 'des-ede3-cbc', $key, OPENSSL_RAW_DATA, "\0\0\0\0\0\0\0\0"), 0, $l);
$l = ceil(strlen($message) / 8) * 8;
$enc = substr(openssl_encrypt($message . str_repeat("\0", $l - strlen($message)), 'des-ede3-cbc', $key, OPENSSL_RAW_DATA, "\0\0\0\0\0\0\0\0"), 0, $l);
$l = ceil(strlen($message) / 8) * 8;
$enc = substr(openssl_encrypt($message . str_repeat("\0", $l - strlen($message)), 'des-ede3-cbc', $key, OPENSSL_RAW_DATA, "\0\0\0\0\0\0\0\0"), 0, $l);
$l = ceil(strlen($message) / 8) * 8;
$enc = substr(openssl_encrypt($message . str_repeat("\0", $l - strlen($message)), 'des-ede3-cbc', $key, OPENSSL_RAW_DATA, "\0\0\0\0\0\0\0\0"), 0, $l);
"), 0, $l);

How can I implement it in Go?

I tried like this but got different result:

    block, err := des.NewTripleDESCipher(key)
	if err != nil {
		fmt.Printf("err: %s", err)
		return
	}

	// Create initialization vector from rand.reader
	iv := make([]byte, des.BlockSize)
	if _, err := io.ReadFull(rand.Reader, iv); err != nil {
		fmt.Printf("err: %s", err)
		return
	}

	// Encrypt with CBC mode
	cipherText := make([]byte, len(plainText))
	encryptMode := cipher.NewCBCEncrypter(block, iv)
	encryptMode.CryptBlocks(cipherText, plainText)

	str := hex.EncodeToString(cipherText)

	fmt.Println("Cipher text: %s", str)

答案1

得分: 1

发现了错误。我在使用rand.Reader作为iv,但在PHP中是不同的。

var iv = []byte{0, 0, 0, 0, 0, 0, 0, 0}

已翻译完成。

英文:

Found the mistake. I was using rand.Reader for iv but in PHP was different.

var iv = []byte{0, 0, 0, 0, 0, 0, 0, 0}

huangapple
  • 本文由 发表于 2023年2月7日 22:46:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75374983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定