PHP openssl des-ede3-cbc to Golang

WBOY
Release: 2024-02-08 23:10:20
forward
415 people have browsed it

PHP openssl des-ede3-cbc 到 Golang

PHP openssl des-ede3-cbc 到 Golang是一篇介绍在PHP和Golang之间进行加密算法转换的文章。在这篇文章中,php小编子墨将详细讲解如何使用openssl库中的des-ede3-cbc算法,在PHP中对数据进行加密,然后将加密后的数据传输到Golang程序中进行解密。通过这篇文章,读者可以了解到如何在不同编程语言之间进行加密算法的转换,从而更好地应用于实际开发中。

问题内容

我有以下 php 代码:

$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);
Copy after login

如何在 go 中实现它?

我尝试了这样的方法,但得到了不同的结果:

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)
Copy after login

解决方法

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

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

The above is the detailed content of PHP openssl des-ede3-cbc to Golang. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!