golang写的与php框架laravel内置安全加密算法对接
附上: laravel内部安全加密算法/golang安全加密算法,内有demo示例https://github.com/jiangshengxin/golang-phplaravel-aes256-cbc
Golang代码
package main
import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"encoding/json"
"errors"
"strings"
"github.com/elliotchance/phpserialize"
"io"
"bytes"
"crypto/rand"
"crypto/hmac"
"crypto/sha256"
"fmt"
"os"
)
func main() {
//加密
token, err := encode("QQ群644344070")
if err != nil {
}
println(token)
os.Exit(1)
//解密
str, err := decode("eyJpdiI6IkJLZmJoOTBGa1A0MGRiLy8zemg4c1E9PSIsIm1hYyI6IjAzZDZiZGQ5YWY4NGY2NGZkMTgwMmFjZTFkZWMwNDgzM2I4ZmUyZTUzOTI2OGY5ZjEzNDQ1OGMwMWE2YmYxYzYiLCJ2YWx1ZSI6IlBqQ1llMW81eFlIZUppaEgyQldrdWxpQzNRb3kyaHNCb0hlS1JTTDR0b2s9In0=")
if err != nil {
}
println(str)
os.Exit(2)
}
//加密
func encode(ciphertext string) (string,error) {
//初始化密钥
key:= []byte("xxxxxxxxxxxxxxxxxxxxxxxxxxx")
//序列化密文
ciphertextNew,err := phpserialize.Marshal(ciphertext,nil)
if err != nil {
return "",err
}
plaintext := []byte(ciphertextNew)
//填充明文至加密要求长度
/* paddingCount := aes.BlockSize - len(plaintext)%aes.BlockSize
if paddingCount != 0 {
plaintext = append(plaintext, bytes.Repeat([]byte{byte(0)}, paddingCount)...)
}*/
plaintext,err = Pad(plaintext,aes.BlockSize)
if err != nil {
return "",err
}
// CBC mode works on blocks so plaintexts may need to be padded to the
// next whole block. For an example of such padding, see
// https://tools.ietf.org/html/rfc5246#section-6.2.3.2. Here we'll
// assume that the plaintext is already of the correct length.
//检查密文长度是否合法
/*if len(plaintext)%aes.BlockSize != 0 {
return "