日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

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

作者:TtrOps ? 更新時間: 2023-05-15 編程語言

什么是AES

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

go實現aes加密

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

小案例需求

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

實戰

加密代碼

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]#?

原文鏈接:https://mp.weixin.qq.com/s/FsIfvsuu5jh5_yBXZUS7rA

欄目分類
最近更新