Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GoLang Encrypt string to base64 and vice versa using AES encryption.
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"fmt"
"io"
)
func main() {
originalText := "encrypt this golang"
fmt.Println(originalText)
key := []byte("example key 1234")
// encrypt value to base64
cryptoText := encrypt(key, originalText)
fmt.Println(cryptoText)
// encrypt base64 crypto to original value
text := decrypt(key, cryptoText)
fmt.Printf(text)
}
// encrypt string to base64 crypto using AES
func encrypt(key []byte, text string) string {
// key := []byte(keyText)
plaintext := []byte(text)
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
// The IV needs to be unique, but not secure. Therefore it's common to
// include 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 {
panic(err)
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
// convert to base64
return base64.URLEncoding.EncodeToString(ciphertext)
}
// decrypt from base64 to decrypted string
func decrypt(key []byte, cryptoText string) string {
ciphertext, _ := base64.URLEncoding.DecodeString(cryptoText)
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
// The IV needs to be unique, but not secure. Therefore it's common to
// include it at the beginning of the ciphertext.
if len(ciphertext) < aes.BlockSize {
panic("ciphertext too short")
}
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)
return fmt.Sprintf("%s", ciphertext)
}
@manishtpatel

This comment has been minimized.

Copy link
Owner Author

commented Jan 2, 2014

Updated to use UrlEncoding instead of StdEncoding. I see that UrlEncoding has broader usage, atleast i need it.

@melihmucuk

This comment has been minimized.

Copy link

commented Dec 4, 2015

how can I decrypt from node.js ? any suggestions ?

@kkirsche

This comment has been minimized.

Copy link

commented Jan 26, 2016

I would move from using AES directly to using the Cipher package similar to what's done in https://gist.github.com/kkirsche/e28da6754c39d5e7ea10 based on feedback from the Golang team when I proposed including an example of proper AES encryption in the examples.

@henryx

This comment has been minimized.

Copy link

commented Jan 10, 2017

A note: the key (line 30) should be 16, 24 or 32 byte when creating new cipher (line 32), as reported in https://golang.org/src/crypto/aes/cipher.go

@kolaente

This comment has been minimized.

Copy link

commented Feb 15, 2017

Thank you for this package, helped me a lot!

@blaskovicz

This comment has been minimized.

Copy link

commented Oct 3, 2017

Thank you for the code!

I wrapped this code in a package if anyone cares to use it or help contribute: https://github.com/blaskovicz/go-cryptkeeper.

It provides types.Encrypt(), types.Decrypt(), and a types.CryptStringstruct that can be used for databaseScan()/Value()andMarshalJSON()` operations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.