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

Go語言實現AES加密并編寫一個命令行應用程序

 更新時間:2023年02月16日 11:23:15   作者:TtrOps  
密碼學中的高級加密標準(Advanced Encryption Standard,AES),又稱Rijndael加密法,是經常采用的一種區(qū)塊加密標準。本文就來用Go語言實現AES加密算法,需要的可以參考一下

什么是AES

關于AES更多的知識,請自行腦補,密碼學中的高級加密標準(Advanced Encryption Standard,AES),又稱Rijndael加密法,是經常采用的一種區(qū)塊加密標準。

go實現aes加密

在golang的標準庫aes可以實現AES加密,官方標準庫aes文檔鏈接:https://pkg.go.dev/crypto/aes

小案例需求

本篇分享出在實際工作中的實際需求,需求很簡單,就是需要實現一個命令行應用程序,可以對傳入的明文字符串進行加密,傳入密文進行解密。命令行應用叫做passctl,并帶有幫助功能。實現命令行應用程序有很多強大的第三方庫,因為需求過于簡單,那么本篇就用標準庫中os即可。

實戰(zhàn)

加密代碼

package?main

import?(
?"bytes"
?"crypto/aes"
?"crypto/cipher"
?"encoding/base64"
?"fmt"
)

var?EncKey?=?[]byte("QAZWSXEDCRFVTGBY")?//?16位密碼串

func?pkcs7Padding(data?[]byte,?blockSize?int)?[]byte?{
?padding?:=?blockSize?-?len(data)%blockSize
?padText?:=?bytes.Repeat([]byte{byte(padding)},?padding)
?return?append(data,?padText...)
}

func?AesEncrypt(data?[]byte,?key?[]byte)?([]byte,?error)?{
?block,?err?:=?aes.NewCipher(key)
?if?err?!=?nil?{
??return?nil,?err
?}
?blockSize?:=?block.BlockSize()
?encryptBytes?:=?pkcs7Padding(data,?blockSize)
?crypted?:=?make([]byte,?len(encryptBytes))
?blockMode?:=?cipher.NewCBCEncrypter(block,?key[:blockSize])
?blockMode.CryptBlocks(crypted,?encryptBytes)
?return?crypted,?nil
}

func?EncryptByAes(data?[]byte)?(string,?error)?{
?res,?err?:=?AesEncrypt(data,?EncKey)
?if?err?!=?nil?{
??return?"",?err
?}
?return?base64.StdEncoding.EncodeToString(res),?nil
}

func?main()?{
?plaintext?:=?"2wsx$RFV!Qaz"?//?假設這是明文密碼
?p?:=?[]byte(plaintext)
?newp,?_?:=?EncryptByAes(p)?//?開始加密
?fmt.Println(newp)
}

解密代碼

基于上述加密后的密碼,對其進行解密。

package?main

import?(
?"crypto/aes"
?"crypto/cipher"
?"encoding/base64"
?"errors"
?"fmt"
)

var?EncKey?=?[]byte("QAZWSXEDCRFVTGBY")?//?16位密碼串

func?pkcs7UnPadding(data?[]byte)?([]byte,?error)?{
?length?:=?len(data)
?if?length?==?0?{
??return?nil,?errors.New("Sorry,?the?encryption?string?is?wrong.")
?}
?unPadding?:=?int(data[length-1])
?return?data[:(length?-?unPadding)],?nil
}

func?AesDecrypt(data?[]byte,?key?[]byte)?([]byte,?error)?{
?block,?err?:=?aes.NewCipher(key)
?if?err?!=?nil?{
??return?nil,?err
?}
?blockSize?:=?block.BlockSize()
?blockMode?:=?cipher.NewCBCDecrypter(block,?key[:blockSize])
?crypted?:=?make([]byte,?len(data))
?blockMode.CryptBlocks(crypted,?data)
?crypted,?err?=?pkcs7UnPadding(crypted)
?if?err?!=?nil?{
??return?nil,?err
?}
?return?crypted,?nil
}

func?DecryptByAes(data?string)?([]byte,?error)?{
?dataByte,?err?:=?base64.StdEncoding.DecodeString(data)
?if?err?!=?nil?{
??return?nil,?err
?}
?return?AesDecrypt(dataByte,?EncKey)
}

func?main()?{
?ciphertext?:=?"+LxjKS8N+Kpy/HNxsSJMIw=="?//?密文
?pwd,?_?:=?DecryptByAes(ciphertext)???????//?開始解密
?fmt.Println(string(pwd))
}

實現passctl命令行應用

代碼

package?main

import?(
?"bytes"
?"crypto/aes"
?"crypto/cipher"
?"encoding/base64"
?"errors"
?"fmt"
?"os"
)

var?EncKey?=?[]byte("QAZWSXEDCRFVTGBY")?//?16位密碼串

func?pkcs7Padding(data?[]byte,?blockSize?int)?[]byte?{
?padding?:=?blockSize?-?len(data)%blockSize
?padText?:=?bytes.Repeat([]byte{byte(padding)},?padding)
?return?append(data,?padText...)
}

func?pkcs7UnPadding(data?[]byte)?([]byte,?error)?{
?length?:=?len(data)
?if?length?==?0?{
??return?nil,?errors.New("Sorry,?the?encryption?string?is?wrong.")
?}
?unPadding?:=?int(data[length-1])
?return?data[:(length?-?unPadding)],?nil
}

func?AesEncrypt(data?[]byte,?key?[]byte)?([]byte,?error)?{
?block,?err?:=?aes.NewCipher(key)
?if?err?!=?nil?{
??return?nil,?err
?}
?blockSize?:=?block.BlockSize()
?encryptBytes?:=?pkcs7Padding(data,?blockSize)
?crypted?:=?make([]byte,?len(encryptBytes))
?blockMode?:=?cipher.NewCBCEncrypter(block,?key[:blockSize])
?blockMode.CryptBlocks(crypted,?encryptBytes)
?return?crypted,?nil
}

func?AesDecrypt(data?[]byte,?key?[]byte)?([]byte,?error)?{
?block,?err?:=?aes.NewCipher(key)
?if?err?!=?nil?{
??return?nil,?err
?}
?blockSize?:=?block.BlockSize()
?blockMode?:=?cipher.NewCBCDecrypter(block,?key[:blockSize])
?crypted?:=?make([]byte,?len(data))
?blockMode.CryptBlocks(crypted,?data)
?crypted,?err?=?pkcs7UnPadding(crypted)
?if?err?!=?nil?{
??return?nil,?err
?}
?return?crypted,?nil
}

func?EncryptByAes(data?[]byte)?(string,?error)?{
?res,?err?:=?AesEncrypt(data,?EncKey)
?if?err?!=?nil?{
??return?"",?err
?}
?return?base64.StdEncoding.EncodeToString(res),?nil
}

func?DecryptByAes(data?string)?([]byte,?error)?{
?dataByte,?err?:=?base64.StdEncoding.DecodeString(data)
?if?err?!=?nil?{
??return?nil,?err
?}
?return?AesDecrypt(dataByte,?EncKey)
}

const?help?=?`
Help?description?of?encryption?and?decryption?command?line?application

-h?--help?[Display?help]
-e?--encryption?Plaintext?string?encryption
-d?--decrypt?Ciphertext?string?decryption

Example:

1.?encryption?example:
passctl?-e?"your?plaintext?password"

2.?decryption?example:
passctl?-d?"Your?ciphertext?string"

`

func?main()?{
?args?:=?os.Args[1]
?if?args?==?"-h"?||?args?==?"--help"?{
??fmt.Print(help)
?}?else?if?args?==?"-e"?||?args?==?"--encryption"?{
??plaintext?:=?os.Args[2]
??p?:=?[]byte(plaintext)
??newp,?_?:=?EncryptByAes(p)
??fmt.Println(newp)
?}?else?if?args?==?"-d"?||?args?==?"--decrypt"?{
??ciphertext?:=?os.Args[2]
??a,?_?:=?DecryptByAes(ciphertext)
??fmt.Println(string(a))
?}?else?{
??fmt.Println("Invalid?option")
?}
}

編譯成二進制后使用

#?編譯
[root@devhost?encryptionDecryption]#?go?build?-o?passctl?main.go

#?查看幫助
[root@devhost?encryptionDecryption]#?./passctl?-h

Help?description?of?encryption?and?decryption?command?line?application

-h?--help?[Display?help]
-e?--encryption?Plaintext?string?encryption
-d?--decrypt?Ciphertext?string?decryption

Example:

1.?encryption?example:
passctl?-e?"your?plaintext?password"

2.?decryption?example:
passctl?-d?"Your?ciphertext?string"

#?加密
[root@devhost?encryptionDecryption]#?./passctl?-e?abc123456
nGi3ls+2yghdv7o8Ly2Z+A==

#?解密
[root@devhost?encryptionDecryption]#?./passctl?-d?nGi3ls+2yghdv7o8Ly2Z+A==
abc123456
[root@devhost?encryptionDecryption]#?

到此這篇關于Go語言實現AES加密并編寫一個命令行應用程序的文章就介紹到這了,更多相關Go語言AES加密內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Go語言中函數可變參數(Variadic Parameter)詳解

    Go語言中函數可變參數(Variadic Parameter)詳解

    在Python中,在函數參數不確定數量的情況下,可以動態(tài)在函數內獲取參數。在Go語言中,也有類似的實現方式,本文就來為大家詳細講解一下
    2022-07-07
  • 淺析Go語言中的方法集合與選擇receiver類型

    淺析Go語言中的方法集合與選擇receiver類型

    這篇文章主要為大家詳細介紹了Go語言中的方法集合與選擇receiver類型的相關知識,文中的示例代碼講解詳細,對我們深入學習go語言有一定的幫助,需要的可以參考下
    2023-11-11
  • 解決golang.org不能訪問的問題(推薦)

    解決golang.org不能訪問的問題(推薦)

    這篇文章主要介紹了解決golang.org不能訪問的問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-11-11
  • golang gorm 操作mysql及gorm基本用法

    golang gorm 操作mysql及gorm基本用法

    golang 官方的那個操作mysql的有點麻煩所以就使用了gorm,下面就gorm的使用做下簡單介紹,感興趣的朋友跟隨小編一起看看吧
    2018-11-11
  • 使用Viper處理Go應用程序的配置方法

    使用Viper處理Go應用程序的配置方法

    Viper是一個應用程序配置解決方案,用于Go應用程序,它支持JSON、TOML、YAML、HCL、envfile和Java properties配置文件格式,這篇文章主要介紹了使用Viper處理Go應用程序的配置,需要的朋友可以參考下
    2023-09-09
  • Go語言中JSON文件的讀寫操作

    Go語言中JSON文件的讀寫操作

    本文主要介紹了Go語言JSON文件的讀寫操作,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-04-04
  • Go?語言簡單實現Vigenere加密算法

    Go?語言簡單實現Vigenere加密算法

    這篇文章主要介紹了Go語言簡單實現Vigenere加密算法,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的朋友可以參考一下
    2022-09-09
  • Kubernetes上使用Jaeger分布式追蹤基礎設施詳解

    Kubernetes上使用Jaeger分布式追蹤基礎設施詳解

    這篇文章主要為大家介紹了Kubernetes上使用Jaeger分布式追蹤基礎設施詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • golang切片原理詳細解析

    golang切片原理詳細解析

    這篇文章主要介紹了golang切片原理詳細解析,切片在編譯時定義為Slice結構體,并通過NewSlice()函數進行創(chuàng)建,更多相關內容感興趣的小伙伴可以參考一下下面文章內容
    2022-06-06
  • Go項目編寫Makefile規(guī)則文件概述

    Go項目編寫Makefile規(guī)則文件概述

    這篇文章主要為大家介紹了Go項目編寫Makefile文件規(guī)則概述,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪
    2022-04-04

最新評論