Skip to content

Instantly share code, notes, and snippets.

@secondarykey
Last active October 17, 2016 21:06
Show Gist options
  • Save secondarykey/06afc4cd1c32fceaa4ed3a7fe8f505fb to your computer and use it in GitHub Desktop.
Save secondarykey/06afc4cd1c32fceaa4ed3a7fe8f505fb to your computer and use it in GitHub Desktop.
package main
import (
"crypto/cipher"
"crypto/aes"
"fmt"
)
var block cipher.Block
func SetAES256Cipher(buf string) error {
key := []byte(buf)
var err error
block, err = aes.NewCipher(key)
if err != nil {
return fmt.Errorf("Create Cipher:%s",err)
}
return nil
}
func EncryptAES256(t string) (string,error) {
if block == nil {
return "",fmt.Errorf("Call SetAES256Cipher()")
}
plain := []byte(t)
cipher := make([]byte, len(plain))
block.Encrypt(cipher, plain)
return string(cipher),nil
}
func DecryptAES256(t string) (string,error) {
if block == nil {
return "",fmt.Errorf("Call SetAES256Cipher()")
}
cipher := []byte(t)
text := make([]byte, len(cipher))
block.Decrypt(text, cipher)
return string(text),nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment