Last active
December 13, 2015 22:36
-
-
Save p4tin/dd820c90dddd704afe6d to your computer and use it in GitHub Desktop.
Demo of Data Encrypt/Decrypt with AES 256 Key
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
jHa1bVPQeck+3AsZJa9exEBcI9Pe40XvNSMsezaRr2c= |
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/aes" | |
"crypto/cipher" | |
"crypto/rand" | |
"encoding/base64" | |
"errors" | |
"fmt" | |
"io" | |
"log" | |
"io/ioutil" | |
) | |
var key []byte | |
func init() { | |
content, err := ioutil.ReadFile("./aes.key") | |
if err != nil { | |
log.Fatalln("Could not read key file (aes.key)") | |
} | |
key, err = base64.StdEncoding.DecodeString(string(content)) | |
if err != nil { | |
log.Fatalln("Could not decode base64 key from file (aes.key)") | |
} | |
} | |
func main() { | |
plaintext := []byte("some really really really long plaintext") | |
fmt.Printf("%s\n", plaintext) | |
ciphertext, err := encrypt(key, plaintext) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Printf("%0x\n", ciphertext) | |
result, err := decrypt(key, ciphertext) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Printf("%s\n", result) | |
} | |
func encrypt(key, text []byte) ([]byte, error) { | |
block, err := aes.NewCipher(key) | |
if err != nil { | |
return nil, err | |
} | |
b := base64.StdEncoding.EncodeToString(text) | |
ciphertext := make([]byte, aes.BlockSize+len(b)) | |
iv := ciphertext[:aes.BlockSize] | |
if _, err := io.ReadFull(rand.Reader, iv); err != nil { | |
return nil, err | |
} | |
cfb := cipher.NewCFBEncrypter(block, iv) | |
cfb.XORKeyStream(ciphertext[aes.BlockSize:], []byte(b)) | |
return ciphertext, nil | |
} | |
func decrypt(key, text []byte) ([]byte, error) { | |
block, err := aes.NewCipher(key) | |
if err != nil { | |
return nil, err | |
} | |
if len(text) < aes.BlockSize { | |
return nil, errors.New("ciphertext too short") | |
} | |
iv := text[:aes.BlockSize] | |
text = text[aes.BlockSize:] | |
cfb := cipher.NewCFBDecrypter(block, iv) | |
cfb.XORKeyStream(text, text) | |
data, err := base64.StdEncoding.DecodeString(string(text)) | |
if err != nil { | |
return nil, err | |
} | |
return data, nil | |
} |
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/rand" | |
"fmt" | |
b64 "encoding/base64" | |
"encoding/hex" | |
"io/ioutil" | |
) | |
func main() { | |
key := make([]byte, 32) | |
_, err := rand.Read(key) | |
if err != nil { | |
// handle error here | |
} | |
fmt.Println("=================") | |
fmt.Println(key) | |
fmt.Println("=================") | |
sEnc := b64.StdEncoding.EncodeToString(key) | |
fmt.Println(sEnc) | |
fmt.Println("=================") | |
sEnc2 := hex.EncodeToString(key) | |
fmt.Println(sEnc2) | |
fmt.Println("=================") | |
sDec, _ := b64.StdEncoding.DecodeString(sEnc) | |
fmt.Println(sDec) | |
fmt.Println("=================") | |
sDec2, _ := hex.DecodeString(sEnc2) | |
fmt.Println(sDec2) | |
d1 := []byte(sEnc) | |
_ = ioutil.WriteFile("./aes.key", d1, 0644) | |
content, err := ioutil.ReadFile("./aes.key") | |
if err != nil { | |
//Do something | |
} | |
fmt.Println(string(content)) | |
fmt.Println("=================") | |
sDec, _ = b64.StdEncoding.DecodeString(string(content)) | |
fmt.Println(sDec) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment