1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| package main
import ( "crypto/aes" "crypto/cipher" "crypto/rand" "encoding/hex" "errors" "fmt" "io" "sync" )
var commonKey = []byte("0123456789abcdef") var syncMutext sync.Mutex
func SetAeskey(key string) { syncMutext.Lock() defer syncMutext.Unlock() commonKey = []byte(key) }
func AesEncrypt(plaintext string) (string, error) { block, err := aes.NewCipher(commonKey) if err != nil { return "", err } in := []byte(plaintext) leng := len(plaintext) if leng%16 != 0 { leng = leng/16*16 + 16 leng = leng - len(plaintext) for i := 0; i < leng; i++ { in = append(in, 0) } leng = len(in) }
cipherText := make([]byte, aes.BlockSize+leng) iv := cipherText[:aes.BlockSize] if _, err := io.ReadFull(rand.Reader, iv); err != nil { return "", err } cipher.NewCBCEncrypter(block, iv).CryptBlocks(cipherText[aes.BlockSize:], in) return hex.EncodeToString(cipherText), nil }
func AesDecrpt(d string) (string, error) { ciphertext, err := hex.DecodeString(d) if err != nil { return "", err } block, err := aes.NewCipher(commonKey) if err != nil { return "", err } if len(ciphertext) < aes.BlockSize { return "", errors.New("ciphertext too short") } iv := ciphertext[:aes.BlockSize] ciphertext = ciphertext[aes.BlockSize:] cipher.NewCBCDecrypter(block, iv).CryptBlocks(ciphertext, ciphertext) return string(ciphertext), nil }
func main() { se, err := AesEncrypt("12345") fmt.Println(se, err) sd, err := AesDecrpt(se) fmt.Println(sd, err) }
|