目录
- 什么是AES
- go实现aes加密
- 小案例需求
- 实战
- 加密代码
- 解密代码
- 实现passctl命令行应用
- 代码
- 编译成二进制后使用
什么是AES
关于AES更多的知识,请自行脑补,密码学中的高级加密标准(Advanced Encryption Standard,AES),又称Rijndael加密法,是经常采用的一种区块加密标准。
go实现aes加密
在golang的标准库aes可以实现AES加密,官方标准库aes文档链接:https://pkg.go.dev/crypto/aes
小案例需求
本篇分享出在实际工作中的实际需求,需求很简单,就是需要实现一个命令行应用程序,可以对传入的明文字符串进行加密,传入密文进行解密。命令行应用叫做passctl,并带有帮助功能。实现命令行应用程序有很多强大的第三方库,因为需求过于简单,那么本篇就用标准库中os即可。
实战
加密代码
packagemain import( "bytes" "crypto/aes" "crypto/cipher" "encoding/base64" "fmt" ) varEncKey=[]byte("QAZWSXEDCRFVTGBY")//16位密码串 funcpkcs7Padding(data[]byte,blockSizeint)[]byte{ padding:=blockSize-len(data)%blockSize padText:=bytes.Repeat([]byte{byte(padding)},padding) returnappend(data,padText...) } funcAesEncrypt(data[]byte,key[]byte)([]byte,error){ block,err:=aes.NewCipher(key) iferr!=nil{ returnnil,err } blockSize:=block.BlockSize() encryptBytes:=pkcs7Padding(data,blockSize) crypted:=make([]byte,len(encryptBytes)) blockMode:=cipher.NewCBCEncrypter(block,key[:blockSize]) blockMode.CryptBlocks(crypted,encryptBytes) returncrypted,nil } funcEncryptByAes(data[]byte)(string,error){ res,err:=AesEncrypt(data,EncKey) iferr!=nil{ return"",err } returnbase64.StdEncoding.EncodeToString(res),nil } funcmain(){ plaintext:="2wsx$RFV!Qaz"//假设这是明文密码 p:=[]byte(plaintext) newp,_:=EncryptByAes(p)//开始加密 fmt.Println(newp) }
解密代码
基于上述加密后的密码,对其进行解密。
packagemain www.devze.comimport( "crypto/aes" "crypto/cipher" "encoding/base64" "errors" "fmt" ) varEncKey=[]byte("QAZWSXEDCRFVTGBY")//16位密码串 funcpkcs7UnPadding(data[]byte)([]byte,error){ length:=len(data) iflength==0{ returnnil,errors.New("Sorry,theencryptionstringiswrong.") } unPadding:=int(data[length-1]) returndata[:(length-unPadding)],nil } funcAesDecrypt(data[]byte,key[]byte)([]byte,error){ block,err:=aes.NewCipher(key) iferr!=nil{ returnnil,err } blockSize:=block.BlockSize() blockMode:=cipher.NewCBCDecrypter(block,key[:blockSize]) crypted:=make([]byte,len(data)) blockMode.CryptBlocks(crypted,data) crypted,err=pkcs7UnPadding(crypted) iferr!=nil{ returnnil,err } returncrypted,nil } funcDecryptByAes(datastring)([]byte,error){ dataByte,err:=base64.StdEncoding.DecodeString(data) iferr!=nil{ returnnil,err } returnAesDecrypt(dataByte,EncKey) } funcmain(){ ciphertext:="+LxjKS8N+Kpy/HNxsSJMIw=="//密文 pwd,_:=DecryptByAes(ciphertext)//开始解密 fmt.Println(string(pwd)) }
实现passctl命令行应用
代码
packagemain import( "bytes" "crypto/aes" "crypto/cipher" "encoding/base64" "errors" "fmt" "os" ) varEncKey=[]byte("QAZWSXEDCRFVTGBY")//16位密码串 funcpkcs7Padding(data[]byte,blockSizeint)[]byte{ padding:=blockSize-len(data)%blockSize padText:=bytes.Repeat([]byte{byte(padding)},padding)javascript returnappend(data,padText...) } funcpkcs7UnPadding(data[]byte)([]byte,error){ length:=len(data) iflength==0{ returnnil,errors.New("Sorry,theencryptionstringiswrong.") } unPadding:=int(data[length-1]) returndata[:(length-unPadding)],nil } funcAesEncrypt(data[]byte,key[]byte)([]byte,error){ block,err:=aes.NewCipher(key) iferr!=nil{ returnnil,err } blockSize:=block.BlockSize() encryptBytes:=pkcs7Padding(data,blockSize) crypted:=make([]byte,len(encryptBytes)) blockMode:=cipher.NewCBCEncrypter(block,key[:blockSize]) blockMode.CryptBlocks(crypted,encryptBytes) returncrypted,nil } funcAesDecrypt(data[]byte,key[]byte)([]byte,error){ block,err:=aes.NewCipher(key) iferr!=nil{ returnnil,err } blockSize:=block.BlockSize() blockMode:=cipher.NewCBCDecrypter(blo开发者_Go培训ck,key[:blockSize]javascript) crypted:=make([]byte,len(data)) blockMode.CryptBlocks(crypted,data) crypted,err=pkcs7UnPadding(crypted) iferr!=nil{ returnnil,err } returncrypted,nil } funcEncryptByAes(data[]byte)(string,error){ res,err:=AesjsEncrypt(data,EncKey) iferr!=nil{ return"",err } returnbase64.StdEncoding.EncodeToString(res),nil } funcDecryptByAes(datastring)([]byte,error){ dataByte,err:=base64.StdEncoding.DecodeString(data) iferr!=nil{ returnnil,err } returnAesDecrypt(dataByte,EncKey) } consthelp=` Helpdescriptionofencryptionanddecryptioncommandlineapplication -h--help[Displayhelp] -e--encryptionPlaintextstringencryption -d--decryptCiphertextstringdecryption Example: 1.encryptionexample: passctl-e"yourplaintextpassword" 2.decryptionexample: passctl-d"Yourciphertextstring" ` funcmain(){ args:=os.Args[1] ifargs=="-h"||args=="--help"{ fmt.Print(help) }elseifargs=="-e"||args=="--encryption"{ plaintext:=os.Args[2] p:=[]byte(plaintext) newp,_:=EncryptByAes(p) fmt.Println(newp) }elseifargs=="-d"||args=="--decrypt"{ ciphertext:=os.Args[2] a,_:=DecryptByAes(ciphertext) fmt.Println(string(a)) }else{ fmt.Println("Invalidoption") } }
编译成二进制后使用
#编译 [root@devhostencryptionDecryption]#gobuild-opassctlmain.go #查看帮助 [root@devhostencryptionDecryption]#./passctl-h Helpdescriptionofencryptionanddecryptioncommandlineapplication -h--help[Displayhelp] -e--encryptionPlaintextstringencryption -d--decryptCiphertextstringdecryption Example: 1.encryptionexample: passctl-e"yourplaintextpassword" 2.decryptionexample: passctl-d"Yourciphertextstring" #加密 [root@devhostencryptionDecryption]#./passctl-eabc123456 nGi3ls+2yghdv7o8Ly2Z+A== #解密 [root@devhostencryptionDecryption]#./passctl-dnGi3ls+2yghdv7o8Ly2Z+A== abc123456 [root@devhostencryptionDecryption]#
到此这篇关于Go语言实现AES加密并编写一个命令行应用程序的文章就介绍到这了,更多相关Go语言AES加密内容请搜索我们以前的文章或继续浏览下面的相关文js章希望大家以后多多支持我们!
精彩评论