golang与phplaravel内置加密算法-aes256加密解密

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 "
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值