英文:
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}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论