golang使用aes庫實現加解密

2021-08-15 05:57:13 字數 1515 閱讀 2446

golang實現加密解密的庫很多, 這裡使用的是aes庫+base64庫來實現.

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

var aeskey = byte(「321423u9y8d2fwfl」)

上**:

package main

import (

"fmt"

"crypto/cipher"

"crypto/aes"

"bytes"

"encoding/base64"

)func pkcs5padding(ciphertext byte, blocksize int) byte , padding)

return

}func pkcs5unpadding(origdata byte) byte

func aesencrypt(origdata, key byte) (byte, error)

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)

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()

pass64 := base64.stdencoding.encodetostring(xpass)

fmt.printf("加密後:%v\n",pass64)

bytespass, err := base64.stdencoding.decodestring(pass64)

if err != nil

tpass, err := aesdecrypt(bytespass, aeskey)

if err != nil

fmt.printf("解密後:%s\n", tpass)

}

輸出:

加密後:rlyzug0mcef2tbcjdhmyjg==

解密後:vdncloud123456

AES實現大檔案分塊加秘和解秘

搞了三天嘗試了n多方法之後終於搞定,如下 using system using system.text using system.collections using system.componentmodel using system.data using system.net using syst...

使用C 實現AES加密解密

aes演算法描述簡介 des 資料加密標準演算法由於金鑰長度較小 56位 已經不適應當今分布式開放網路對資料加密安全性的要求,因此1997年nist公開徵集新的資料加密標準,即aes。經過三輪的篩選,比利時joan daeman和vincent rijmen提交的rijndael演算法被提議為aes...

用開源加密庫Libgcrypt實現AES加密

libgcrypt是著名的開源加密軟體gnupg的底層庫,是乙個非常成熟的加密演算法庫,支援多種對稱和非對稱加密演算法。現在自己隨便造輪子地寫乙個加密演算法程式顯然是非常不安全的,雖然openssl出現了heartbleed漏洞,但是用已經成熟的加密演算法庫還是會比不成熟的東西好很多的。最近看了看它...