Last active
October 17, 2016 21:06
-
-
Save secondarykey/06afc4cd1c32fceaa4ed3a7fe8f505fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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