Quick actions

cmd+k|ctrl+k

Navigation

Languages

Untitled

Snippet info

Language

Go

Visibility

unlisted

Author

legacy-anonymous

Created

2016-05-13T16:34:30Z

Updated

2016-05-13T16:34:30Z

package main

import (
    "fmt"
    "io"
    "crypto/aes"
    "crypto/cipher"
    "encoding/base64"
    "crypto/rand"
)

func encrypt(key []byte, text string) string {
    plaintext := []byte(text)

    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }

    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)

    return base64.URLEncoding.EncodeToString(ciphertext)
}


func main() {
    fmt.Printf(encrypt([]byte("0123456789abcdef"), "test text 123"))
}
INFO