123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- /*
- * Public Domain Software
- *
- * I (Matthias Ladkau) am the author of the source code in this file.
- * I have placed the source code in this file in the public domain.
- *
- * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
- */
- package cryptutil
- import (
- "crypto/aes"
- "crypto/cipher"
- "crypto/rand"
- "crypto/sha256"
- "encoding/base64"
- "fmt"
- "io"
- )
- /*
- EncryptString encrypts a given string using AES (cfb mode).
- */
- func EncryptString(passphrase, text string) (string, error) {
- var ret []byte
- // Create a new cipher with the given key
- key := sha256.Sum256([]byte(passphrase))
- block, err := aes.NewCipher((&key)[:])
- if err == nil {
- // Base64 encode the string
- b := base64.StdEncoding.EncodeToString([]byte(text))
- ciphertext := make([]byte, aes.BlockSize+len(b))
- // Create the initialization vector using random numbers
- iv := ciphertext[:aes.BlockSize]
- if _, err = io.ReadFull(rand.Reader, iv); err == nil {
- // Do the encryption
- cfb := cipher.NewCFBEncrypter(block, iv)
- cfb.XORKeyStream(ciphertext[aes.BlockSize:], []byte(b))
- ret = ciphertext
- }
- }
- return string(ret), err
- }
- /*
- DecryptString decrypts a given string using AES (cfb mode).
- */
- func DecryptString(passphrase, text string) (string, error) {
- var ret []byte
- // Check encrypted text
- if len(text) < aes.BlockSize {
- return "", fmt.Errorf("Ciphertext is too short - must be at least: %v", aes.BlockSize)
- }
- // Create a new cipher with the given key
- key := sha256.Sum256([]byte(passphrase))
- block, err := aes.NewCipher((&key)[:])
- if err == nil {
- // Separate initialization vector and actual encrypted text
- iv := text[:aes.BlockSize]
- text = text[aes.BlockSize:]
- // Do the decryption
- cfb := cipher.NewCFBDecrypter(block, []byte(iv))
- ret = []byte(text) // Reuse text buffer
- cfb.XORKeyStream(ret, []byte(text))
- // Decode text from base64
- ret, err = base64.StdEncoding.DecodeString(string(ret))
- if err != nil {
- // Return a proper error if something went wrong
- ret = nil
- err = fmt.Errorf("Could not decrypt data")
- }
- }
- return string(ret), err
- }
|