猿问

Mcrypt 从 PHP 到 Go

我使用一个类来加密/解密 PHP 中的字符串。


如何在 Go 中加密/解密字符串?


PHP 类:


class Crypto {

    private $encryptKey = 'xxxxxxxxxxxxxxxx';

    private $iv = 'xxxxxxxxxxxxxxxx';

    private $blocksize = 16;

    public function decrypt($data)

    {

        return $this->unpad(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, 

            $this->encryptKey, 

            hex2bin($data),

            MCRYPT_MODE_CBC, $this->iv), $this->blocksize);

    }

    public function encrypt($data)

    {

        //don't use default php padding which is '\0'

        $pad = $this->blocksize - (strlen($data) % $this->blocksize);

        $data = $data . str_repeat(chr($pad), $pad);

        return bin2hex(mcrypt_encrypt(MCRYPT_RIJNDAEL_128,

            $this->encryptKey,

            $data, MCRYPT_MODE_CBC, $this->iv));

    }

    private function unpad($str, $blocksize)

    {

        $len = strlen($str);

        $pad = ord($str[$len - 1]);

        if ($pad && $pad <= $blocksize) {

            if (substr($str, -$pad) === str_repeat(chr($pad), $pad)) {

                return substr($str, 0, $len - $pad);

            }

        }

        return $str;

    }

}

什么能够在 PHP 和 Go 中加密/解密相同的字符串。


一只甜甜圈
浏览 227回答 1
1回答

Cats萌萌

下面是一个例子:package mainimport (&nbsp; &nbsp; "crypto/aes"&nbsp; &nbsp; "crypto/cipher"&nbsp; &nbsp; "crypto/rand"&nbsp; &nbsp; "encoding/base64"&nbsp; &nbsp; "errors"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "io"&nbsp; &nbsp; "log")func main() {&nbsp; &nbsp; key := []byte("xxxxxxxxxxxxxxxx") // 32 bytes&nbsp; &nbsp; plaintext := []byte("TEST")&nbsp; &nbsp; fmt.Printf("%s\n", plaintext)&nbsp; &nbsp; ciphertext, err := encrypt(key, plaintext)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Printf("%0x\n", ciphertext)&nbsp; &nbsp; result, err := decrypt(key, ciphertext)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Printf("%s\n", result)}func encrypt(key, text []byte) ([]byte, error) {&nbsp; &nbsp; block, err := aes.NewCipher(key)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return nil, err&nbsp; &nbsp; }&nbsp; &nbsp; b := base64.StdEncoding.EncodeToString(text)&nbsp; &nbsp; ciphertext := make([]byte, aes.BlockSize+len(b))&nbsp; &nbsp; iv := ciphertext[:aes.BlockSize]&nbsp; &nbsp; if _, err := io.ReadFull(rand.Reader, iv); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return nil, err&nbsp; &nbsp; }&nbsp; &nbsp; cfb := cipher.NewCFBEncrypter(block, iv)&nbsp; &nbsp; cfb.XORKeyStream(ciphertext[aes.BlockSize:], []byte(b))&nbsp; &nbsp; return ciphertext, nil}func decrypt(key, text []byte) ([]byte, error) {&nbsp; &nbsp; block, err := aes.NewCipher(key)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return nil, err&nbsp; &nbsp; }&nbsp; &nbsp; if len(text) < aes.BlockSize {&nbsp; &nbsp; &nbsp; &nbsp; return nil, errors.New("ciphertext too short")&nbsp; &nbsp; }&nbsp; &nbsp; iv := text[:aes.BlockSize]&nbsp; &nbsp; text = text[aes.BlockSize:]&nbsp; &nbsp; cfb := cipher.NewCFBDecrypter(block, iv)&nbsp; &nbsp; cfb.XORKeyStream(text, text)&nbsp; &nbsp; data, err := base64.StdEncoding.DecodeString(string(text))&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return nil, err&nbsp; &nbsp; }&nbsp; &nbsp; return data, nil}主要借鉴和改编自:https : //golang.org/src/crypto/cipher/example_test.go// Input => TEST// Output => 13360adba03733e11dd2702de441ff8bbb90676ad762fc83更新要将字符串用作decode函数的参数,您需要将字符串转换为byteusinghex.Decodestringdata, _ := hex.DecodeString("1d6f12d3aa2353b23c6012dbc85816632129363d58a76063")result, err := decrypt(key, data)if err != nil {&nbsp; &nbsp; log.Fatal(err)}fmt.Printf("%s\n", result)不要忘记包含"encoding/hex"在包列表中。
随时随地看视频慕课网APP

相关分类

Go
我要回答