激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

云服務器|WEB服務器|FTP服務器|郵件服務器|虛擬主機|服務器安全|DNS服務器|服務器知識|Nginx|IIS|Tomcat|

服務器之家 - 服務器技術 - 服務器知識 - 在 Go 項目中封裝 AES 加解密客戶端接口

在 Go 項目中封裝 AES 加解密客戶端接口

2024-01-04 06:46未知服務器之家 服務器知識

1.摘要 在一個中型以上的項目中, 我們一般會在項目工程中開辟一個pkg文件夾用來存放一些基礎工具接口,比如:數據庫、中間件、加解密算法、基礎協議等等。在這篇文章中, 我主要分享一下在基于Go語言的項目中, 加解密算法中如何

1.摘要

在一個中型以上的項目中, 我們一般會在項目工程中開辟一個pkg文件夾用來存放一些基礎工具接口,比如:數據庫、中間件、加解密算法、基礎協議等等。在這篇文章中, 我主要分享一下在基于Go語言的項目中, 加解密算法中如何封裝一個通用的加解密接口, 并以使用比較廣泛的AES加解密算法實現為基礎進行講解, 最后模擬客戶端分別演示調用AES的加密接口和解密接口。

在 Go 項目中封裝 AES 加解密客戶端接口

2.工程文件結構

在一個正規項目中, 我們要封裝的文件主要添加在算法文件夾下, 目錄結構規劃如下:

pkg
 |
 ---- algorithm
        |
        ---- base.go        // 基礎接口函數定義
        |
        ---- aes.go         // aes加解密算法接口
        |
        ---- aes_test.go    // aes加解密算法接口函數測試

我在名為"algorithm"文件夾下新建了三個文件, 其中base.go為基礎接口函數定義, 因為以后可能要加入進來的算法會比較多,因此需要有一個基礎類文件來定義通用函數接口。

aes.go文件中主要實現AES算法的加解密過程, 并提供一個對外的初始化接口,方便應用層調用。

aes_test.go是作為單元測試的文件, 在里面可以針對AES加密函數和解密函數寫測試用例, 不用編譯整個工程實現單元測試。

如果后面有新的算法加入進來, 例如:des算法, 只需要添加一個des.go和des_test.go文件, 在里面實現函數功能即可。

3.基礎接口實現

基礎接口實現主要在base.go文件中, 因為對于所有加密算法來講, 都有兩個最基礎通用的方法:加密函數和解密函數,因此這里定義了兩個通用的方法接口:

type IAlgorithm interface {
  Encrypt() // 加密函數接口
  Decrypt() // 解密函數接口
}

因為現在不知道項目默認需要使用什么算法,因此實現這兩個方法的空接口:

type DefaultAlgorithm struct{}

func (dal DefaultAlgorithm) Encrypt() {}

func (dal DefaultAlgorithm) Decrypt() {}

考慮在應用層方便切換不同的算法, 這里需要設計一個管理接口的方法, 首先定義一個結構體:

type AlgorithmManager struct {
  algorithm IAlgorithm
}

在這個結構體中, 成員是上面接口名稱的對象。

然后我定義了兩個方法, 一個是設置算法對象的方法, 另一個是執行算法方式的方法。

首先是設置算法對象的方法:

func (gor *AlgorithmManager) SetAlgorithm(algorithm IAlgorithm) {
  gor.algorithm = algorithm
}

這個方法會接收一個參數,這個參數就是用戶想要調用哪種算法的對象, 只有給接口賦對應算法的對象,接口才知道調用哪個算法的方法。

其次是運行算法類型的方法:

const (
  encryptMode = "encrypt"
  decryptMode = "decrypt"
)

func (gor *AlgorithmManager) RunAlgorithm(runMode string) {
  switch runMode {
  case encryptMode:
    gor.algorithm.Encrypt()
    break
  case decryptMode:
    gor.algorithm.Decrypt()
    break
  }
}

這里我定義了兩個模式用來標識加密模式和解密模式, 當給RunAlgorithm傳參encryptMode, 則會執行加密函數,反之則執行解密函數。

4.AES加解密算法實現

在AES加解密客戶端調用接口中, 我選擇了選項設計模式, 用戶可以根據加密算法和解密算法參數不同進行靈活的選項傳參。

首先定義一個方法結構體:

type AesAlgorithm struct {
  AppAlg *AlgorithmManager
  EncryptKey string // 密鑰
  PlaintextContent string // 明文內容
  CiphertextContent string // 密文內容
}

在這個結構體中, 密鑰、明文內容、密文內容是我們在使用功能過程中必須傳入的參數, 其中還帶有一個結構對象指針: *AlgorithmManager, 方便我們將AES算法的對象傳給接口,讓其調用AES的加密方法或解密方法。

其次定義一個方便客戶端調用的接口, 并使用動態選項傳參,實現代碼如下:

type AesAlgorithmOption func(aes *AesAlgorithm)

// 用戶初始化調用并傳參
func NewAesAlgorithm(options ...AesAlgorithmOption) *AesAlgorithm {
  aesAlg := &AesAlgorithm{
    AppAlg: new(AlgorithmManager),
    EncryptKey: "",
    PlaintextContent: "",
    CiphertextContent: "",
  }
  for _, option := range options {
    option(aesAlg)
  }
  return aesAlg
}

// 通過該選項函數傳入key
func WithEncryptKey(key string) AesAlgorithmOption {
  return func(aes *AesAlgorithm) {
    aes.EncryptKey = key
  }
}

// 通過該選項函數傳入明文
func WithPlaintextContent(plainText string) AesAlgorithmOption {
  return func(aes *AesAlgorithm) {
    aes.PlaintextContent = plainText
  }
}

// 通過該選項函數傳入密文
func WithCiphertextContent(cipherContent string) AesAlgorithmOption {
  return func(aes *AesAlgorithm) {
    aes.CiphertextContent = cipherContent
  }
}

下面我們還實現了兩個內部函數,分別是加密和解密過程中需要填充塊的實現方法,代碼如下:

加密填充塊:

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 (aalg *AesAlgorithm) Encrypt() {
  tmpKeys := []byte(aalg.EncryptKey)
  tmpPlaintext := aalg.PlaintextContent
  block, err := aes.NewCipher(tmpKeys)
  if err != nil {
    fmt.Println("aes加密失敗,原因:" + err.Error())
    return
  }
  blockSize := block.BlockSize()
  origData := pkcs5Padding([]byte(tmpPlaintext), blockSize)

  blockMode := cipher.NewCBCEncrypter(block, tmpKeys[:blockSize])
  crypted := make([]byte, len(origData))
  blockMode.CryptBlocks(crypted, origData)
  aalg.CiphertextContent = hex.EncodeToString(crypted)
}

解密接口函數實現:

func (aalg *AesAlgorithm) Decrypt() {
  tmpKeys := []byte(aalg.EncryptKey)
  cryptedByte, _ := hex.DecodeString(aalg.CiphertextContent)
  block, err := aes.NewCipher(tmpKeys)
  if err != nil {
    fmt.Println("aes解密失敗,原因:" + err.Error())
    return
  }
  blockSize := block.BlockSize()
  blockMode := cipher.NewCBCDecrypter(block, tmpKeys[:blockSize])
  origin := make([]byte, len(cryptedByte))
  blockMode.CryptBlocks(origin, cryptedByte)
  decryptStrings := pkcs5UnPadding(origin)
  aalg.PlaintextContent = string(decryptStrings)
}

5.AES加密函數驗證

我在aes_test.go中實現加密函數測試模塊:TestEncrypt(t *testing.T), 代碼如下:

func TestEncrypt(t *testing.T) {
  aesAlg := NewAesAlgorithm(
    WithEncryptKey("ZEplYJFPLlhhMaJI"),
    WithPlaintextContent("qYWwo7!!Eq-TX3q"),
  )
  aesAlg.AppAlg.SetAlgorithm(aesAlg)
  aesAlg.AppAlg.RunAlgorithm("encrypt")
  fmt.Println(aesAlg.CiphertextContent)
}

在上面的代碼中, 我們調用了AES算法的對外統一接口函數:NewAesAlgorithm, 并分別調用WithEncryptKey和WithPlaintextContent傳入了Key內容和明文內容, 并調用接口管理方法:SetAlgorithm進行對象賦值, 最后調用RunAlgorithm("encrypt")方法進行AES加密,實際結果如下:

在 Go 項目中封裝 AES 加解密客戶端接口

6.AES解密函數驗證

同樣在aes_test.go中實現加密函數測試模塊:TestDecrypt(t *testing.T), 代碼如下:

func TestDecrypt(t *testing.T) {
  aesAlg := NewAesAlgorithm(
    WithEncryptKey("ZEplYJFPLlhhMaJI"),
    WithCiphertextContent("31404e2eb60e2d16faae152106882f4b"),
  )
  aesAlg.AppAlg.SetAlgorithm(aesAlg)
  aesAlg.AppAlg.RunAlgorithm("decrypt")
  fmt.Println(aesAlg.PlaintextContent)
}

在上面的代碼中, 我們調用了AES算法的對外統一接口函數:NewAesAlgorithm, 并分別調用WithEncryptKey和WithCiphertextContent傳入了Key內容和上面加密的密文內容, 并調用接口管理方法:SetAlgorithm進行對象賦值, 最后調用RunAlgorithm("decrypt")方法進行AES解密,實際結果如下:

在 Go 項目中封裝 AES 加解密客戶端接口

可以看到,成功解密出密文且跟加密時傳入的明文一致,解密正確。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久久一区二区三区四区 | 午夜爽爽爽男女免费观看hd | 欧洲黄视频 | 久久精品高清 | hdhdhd79xxxxх | 久久精品日产第一区二区三区 | 91嫩草丨国产丨精品入口 | 国产精品99久久久久久久女警 | 成人黄色在线免费观看 | 久久久久国产成人免费精品免费 | 亚洲国产视频网 | 91香蕉影视 | 精品国产91久久久 | 91色爱| 羞羞视频入口 | 国av在线| 欧美成人视 | 久久久精品福利 | 美女扒开腿让男生桶爽网站 | 亚洲一区二区国产 | 韩国精品一区二区三区四区五区 | 亚洲自拍第一 | 久久精品二区 | 日韩欧美综合在线 | 欧美视频国产 | 羞羞电影在线观看 | 欧美精品一区二区久久久 | 91网在线播放 | 国产大片在线观看 | 中国字幕av| 最新欧美精品一区二区三区 | 久久免费视频一区 | 毛片视频网站在线观看 | 欧美一页 | 一区二区久久精品66国产精品 | 国产一区二区久久精品 | 97色在线观看免费视频 | 日韩黄色影视 | 欧美视频一区二区三区在线观看 | 国产精品自拍99 | 黑人三级毛片 |