-
-
Save DaaaaanB/03acf29a90684c2afc9487152324e832 to your computer and use it in GitHub Desktop.
A quick example of basic AES encryption using the Golang AES library.
This file contains hidden or 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" | |
| "io" | |
| "log" | |
| ) | |
| func main() { | |
| CIPHER_KEY := []byte("0123456789012345") | |
| msg := "A quick brown fox jumped over the lazy dog." | |
| if encrypted, err := encrypt(CIPHER_KEY, msg); err != nil { | |
| log.Println(err) | |
| } else { | |
| log.Printf("CIPHER KEY: %s\n", string(CIPHER_KEY)) | |
| log.Printf("ENCRYPTED: %s\n", encrypted) | |
| if decrypted, err := decrypt(CIPHER_KEY, encrypted); err != nil { | |
| log.Println(err) | |
| } else { | |
| log.Printf("DECRYPTED: %s\n", decrypted) | |
| } | |
| } | |
| } | |
| func encrypt(key []byte, message string) (encmess string, err error) { | |
| plainText := []byte(message) | |
| block, err := aes.NewCipher(key) | |
| if err != nil { | |
| return | |
| } | |
| //IV needs to be unique, but doesn't have to be secure. | |
| //It's common to put it at the beginning of the ciphertext. | |
| cipherText := make([]byte, aes.BlockSize+len(plainText)) | |
| iv := cipherText[:aes.BlockSize] | |
| if _, err = io.ReadFull(rand.Reader, iv); err != nil { | |
| return | |
| } | |
| stream := cipher.NewCFBEncrypter(block, iv) | |
| stream.XORKeyStream(cipherText[aes.BlockSize:], plainText) | |
| //returns to base64 encoded string | |
| encmess = base64.URLEncoding.EncodeToString(cipherText) | |
| return | |
| } | |
| func decrypt(key []byte, securemess string) (decodedmess string, err error) { | |
| cipherText, err := base64.URLEncoding.DecodeString(securemess) | |
| if err != nil { | |
| return | |
| } | |
| block, err := aes.NewCipher(key) | |
| if err != nil { | |
| return | |
| } | |
| if len(cipherText) < aes.BlockSize { | |
| err = errors.New("Ciphertext block size is too short!") | |
| return | |
| } | |
| //IV needs to be unique, but doesn't have to be secure. | |
| //It's common to put it at the beginning of the ciphertext. | |
| iv := cipherText[:aes.BlockSize] | |
| cipherText = cipherText[aes.BlockSize:] | |
| stream := cipher.NewCFBDecrypter(block, iv) | |
| // XORKeyStream can work in-place if the two arguments are the same. | |
| stream.XORKeyStream(cipherText, cipherText) | |
| decodedmess = string(cipherText) | |
| return | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, I am trying to use above decrypt method in my application. The cipherKey that I have is not a straightforward 32 bit one, but it looks to be base64 encoded. Hence, I decoded that first and then applied. I don't get any error in this whole decryption process, however the output of this decrypt method looks to be another ciphertext. Not sure if I am missing something as far as the cipherKey is concerned, because it's the only thing which I am providing from my side, everything else I am using from above sample code.
Could you please assist, thank you!