欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

golang使用aes库实现加解密操作

程序员文章站 2022-04-29 21:39:06
golang实现加密解密的库很多,这里使用的是aes库+base64库来实现.使用时,需要指定一个私钥,来进行加解密,这里指定是:var aeskey = []byte(“321423u9y8d2fw...

golang实现加密解密的库很多,这里使用的是aes库+base64库来实现.

使用时,需要指定一个私钥,来进行加解密,这里指定是:

var aeskey = []byte(“321423u9y8d2fwfl”)

上代码:

package main
import (
 "fmt"
 "crypto/cipher"
 "crypto/aes"
 "bytes"
 "encoding/base64"
)
func pkcs5padding(ciphertext []byte, blocksize int) []byte {
 padding := blocksize - len(ciphertext)%blocksize
 padtext := bytes.repeat([]byte{byte(padding)}, padding)
 return append(ciphertext, padtext...)
}
func pkcs5unpadding(origdata []byte) []byte {
 length := len(origdata)
 unpadding := int(origdata[length-1])
 return origdata[:(length - unpadding)]
}
func aesencrypt(origdata, key []byte) ([]byte, error) {
 block, err := aes.newcipher(key)
 if err != nil {
  return nil, err
 }
 blocksize := block.blocksize()
 origdata = pkcs5padding(origdata, blocksize)
 blockmode := cipher.newcbcencrypter(block, key[:blocksize])
 crypted := make([]byte, len(origdata))
 blockmode.cryptblocks(crypted, origdata)
 return crypted, nil
}
func aesdecrypt(crypted, key []byte) ([]byte, error) {
 block, err := aes.newcipher(key)
 if err != nil {
  return nil, err
 }
 blocksize := block.blocksize()
 blockmode := cipher.newcbcdecrypter(block, key[:blocksize])
 origdata := make([]byte, len(crypted))
 blockmode.cryptblocks(origdata, crypted)
 origdata = pkcs5unpadding(origdata)
 return origdata, nil
}
func main() {
 var aeskey = []byte("321423u9y8d2fwfl")
 pass := []byte("vdncloud123456")
 xpass, err := aesencrypt(pass, aeskey)
 if err != nil {
  fmt.println(err)
  return
 }
 pass64 := base64.stdencoding.encodetostring(xpass)
 fmt.printf("加密后:%v\n",pass64)
 bytespass, err := base64.stdencoding.decodestring(pass64)
 if err != nil {
  fmt.println(err)
  return
 }
 tpass, err := aesdecrypt(bytespass, aeskey)
 if err != nil {
  fmt.println(err)
  return
 }
 fmt.printf("解密后:%s\n", tpass)
}

输出:

加密后:rlyzug0mcef2tbcjdhmyjg==

解密后:vdncloud123456

补充:golang aes cbc 加密

我就废话不多说了,大家还是直接看代码吧~

package main
import (
 "bytes"
 "crypto/aes"
 "crypto/cipher"
 "encoding/base64"
 "fmt"
)
const (
 key = "2018201820182018"
 iv = "1234567887654321"
)
func main() {
 str := "我勒个去"
 es, _ := aesencrypt(str, []byte(key))
 fmt.println(es)
 ds, _ := aesdecrypt(es, []byte(key))
 fmt.println(string(ds))
}
func aesencrypt(encodestr string, key []byte) (string, error) {
 encodebytes := []byte(encodestr)
 //根据key 生成密文
 block, err := aes.newcipher(key)
 if err != nil {
  return "", err
 }
 blocksize := block.blocksize()
 encodebytes = pkcs5padding(encodebytes, blocksize)
 blockmode := cipher.newcbcencrypter(block, []byte(iv))
 crypted := make([]byte, len(encodebytes))
 blockmode.cryptblocks(crypted, encodebytes)
 return base64.stdencoding.encodetostring(crypted), nil
}
func pkcs5padding(ciphertext []byte, blocksize int) []byte {
 padding := blocksize - len(ciphertext)%blocksize
 //填充
 padtext := bytes.repeat([]byte{byte(padding)}, padding)
 return append(ciphertext, padtext...)
}
func aesdecrypt(decodestr string, key []byte) ([]byte, error) {
 //先解密base64
 decodebytes, err := base64.stdencoding.decodestring(decodestr)
 if err != nil {
  return nil, err
 }
 block, err := aes.newcipher(key)
 if err != nil {
  return nil, err
 }
 blockmode := cipher.newcbcdecrypter(block, []byte(iv))
 origdata := make([]byte, len(decodebytes))
 blockmode.cryptblocks(origdata, decodebytes)
 origdata = pkcs5unpadding(origdata)
 return origdata, nil
}
func pkcs5unpadding(origdata []byte) []byte {
 length := len(origdata)
 unpadding := int(origdata[length-1])
 return origdata[:(length - unpadding)]
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。