欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

golang使用aes庫實現(xiàn)加解密操作

 更新時間:2020年12月22日 16:15:39   作者:robertkun  
這篇文章主要介紹了golang使用aes庫實現(xiàn)加解密操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

golang實現(xiàn)加密解密的庫很多,這里使用的是aes庫+base64庫來實現(xiàn).

使用時,需要指定一個私鑰,來進行加解密,這里指定是:

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)
 //根據(jù)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)]
}

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

  • 淺談Go語言中字符串和數(shù)組

    淺談Go語言中字符串和數(shù)組

    這篇文章主要簡單介紹了Go語言中字符串和數(shù)組的使用方法和申明方式,需要的朋友可以參考下
    2015-01-01
  • Go中defer使用場景及注意事項

    Go中defer使用場景及注意事項

    defer 會在當(dāng)前函數(shù)返回前執(zhí)行傳入的函數(shù),它會經(jīng)常被用于關(guān)閉文件描述符、關(guān)閉數(shù)據(jù)庫連接以及解鎖資源。這篇文章主要介紹了Go中defer使用注意事項,需要的朋友可以參考下
    2021-12-12
  • golang中json和struct的使用說明

    golang中json和struct的使用說明

    這篇文章主要介紹了golang中json和struct的使用說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-05-05
  • 使用Go實現(xiàn)優(yōu)雅重啟服務(wù)功能

    使用Go實現(xiàn)優(yōu)雅重啟服務(wù)功能

    這篇文章主要介紹了如何使用Go來實現(xiàn)優(yōu)雅重啟服務(wù),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-11-11
  • Golang中的[]byte與16進制(String)之間的轉(zhuǎn)換方式

    Golang中的[]byte與16進制(String)之間的轉(zhuǎn)換方式

    這篇文章主要介紹了Golang中的[]byte與16進制(String)之間的轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • 一文帶你輕松學(xué)會Go語言動態(tài)調(diào)用函數(shù)

    一文帶你輕松學(xué)會Go語言動態(tài)調(diào)用函數(shù)

    這篇文章主要是帶大家學(xué)習(xí)一下Go語言是如何動態(tài)調(diào)用函數(shù)的,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Go語言有一定的幫助,需要的可以參考下
    2022-11-11
  • GO語言make()分配用法實例

    GO語言make()分配用法實例

    這篇文章主要介紹了GO語言make()分配用法,實例分析了make()的功能及使用技巧,需要的朋友可以參考下
    2015-02-02
  • 從Context到go設(shè)計理念輕松上手教程

    從Context到go設(shè)計理念輕松上手教程

    這篇文章主要為大家介紹了從Context到go設(shè)計理念輕松上手教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • Golang 函數(shù)執(zhí)行時間統(tǒng)計裝飾器的一個實現(xiàn)詳解

    Golang 函數(shù)執(zhí)行時間統(tǒng)計裝飾器的一個實現(xiàn)詳解

    這篇文章主要介紹了Golang 函數(shù)執(zhí)行時間統(tǒng)計裝飾器的一個實現(xiàn)詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • go語言的panic和recover函數(shù)用法實例

    go語言的panic和recover函數(shù)用法實例

    今天小編就為大家分享一篇關(guān)于go語言的panic和recover函數(shù)用法實例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-04-04

最新評論