Skip to content

Instantly share code, notes, and snippets.

@shreyner
Forked from ayubmalik/cipherstreams.go
Created February 7, 2023 13:48
Show Gist options
  • Select an option

  • Save shreyner/f2e4dc5cabfecd86a144372a1621c6b3 to your computer and use it in GitHub Desktop.

Select an option

Save shreyner/f2e4dc5cabfecd86a144372a1621c6b3 to your computer and use it in GitHub Desktop.

Revisions

  1. @ayubmalik ayubmalik revised this gist Nov 17, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cipherstreams.go
    Original file line number Diff line number Diff line change
    @@ -106,7 +106,7 @@ func main() {
    w, _ = EncryptedWriter(secretKey, file)
    io.WriteString(w, "Hello again gophers!")

    file, _ = os.Open("/tmp/encmsg.txt")
    file, _ = os.Open("/tmp/encmsg.txt") // use flags/file param for prod code
    r, _ = EncryptedReader(secretKey, file)
    contents, _ := ioutil.ReadAll(r)
    fmt.Printf("contents of decrypted file = %s\n", contents)
  2. @ayubmalik ayubmalik revised this gist Jan 23, 2020. 1 changed file with 10 additions and 8 deletions.
    18 changes: 10 additions & 8 deletions cipherstreams.go
    Original file line number Diff line number Diff line change
    @@ -15,10 +15,6 @@ import (

    // EncryptedWriter wraps w with an OFB cipher stream.
    func EncryptedWriter(key string, w io.Writer) (*cipher.StreamWriter, error) {
    block, err := newBlock(key)
    if err != nil {
    return nil, err
    }

    // generate random initial value
    iv := make([]byte, aes.BlockSize)
    @@ -32,16 +28,17 @@ func EncryptedWriter(key string, w io.Writer) (*cipher.StreamWriter, error) {
    return nil, errors.New("could not write initial value")
    }

    block, err := newBlock(key)
    if err != nil {
    return nil, err
    }

    stream := cipher.NewOFB(block, iv)
    return &cipher.StreamWriter{S: stream, W: w}, nil
    }

    // EncryptedReader wraps r with an OFB cipher stream.
    func EncryptedReader(key string, r io.Reader) (*cipher.StreamReader, error) {
    block, err := newBlock(key)
    if err != nil {
    return nil, err
    }

    // read initial value
    iv := make([]byte, aes.BlockSize)
    @@ -50,6 +47,11 @@ func EncryptedReader(key string, r io.Reader) (*cipher.StreamReader, error) {
    return nil, errors.New("could not read initial value")
    }

    block, err := newBlock(key)
    if err != nil {
    return nil, err
    }

    stream := cipher.NewOFB(block, iv)
    return &cipher.StreamReader{S: stream, R: r}, nil
    }
  3. @ayubmalik ayubmalik revised this gist Jan 23, 2020. No changes.
  4. @ayubmalik ayubmalik revised this gist Jan 23, 2020. No changes.
  5. @ayubmalik ayubmalik revised this gist Jan 23, 2020. No changes.
  6. @ayubmalik ayubmalik revised this gist Jan 23, 2020. No changes.
  7. @ayubmalik ayubmalik revised this gist Jan 23, 2020. No changes.
  8. @ayubmalik ayubmalik revised this gist Jan 23, 2020. No changes.
  9. @ayubmalik ayubmalik renamed this gist Jan 23, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  10. @ayubmalik ayubmalik created this gist Jan 23, 2020.
    111 changes: 111 additions & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,111 @@
    package main

    import (
    "crypto/aes"
    "crypto/cipher"
    "crypto/md5"
    "crypto/rand"
    "errors"
    "fmt"
    "io"
    "io/ioutil"
    "os"
    "strings"
    )

    // EncryptedWriter wraps w with an OFB cipher stream.
    func EncryptedWriter(key string, w io.Writer) (*cipher.StreamWriter, error) {
    block, err := newBlock(key)
    if err != nil {
    return nil, err
    }

    // generate random initial value
    iv := make([]byte, aes.BlockSize)
    if _, err := io.ReadFull(rand.Reader, iv); err != nil {
    return nil, err
    }

    // write clear IV to allow for decryption
    n, err := w.Write(iv)
    if err != nil || n != len(iv) {
    return nil, errors.New("could not write initial value")
    }

    stream := cipher.NewOFB(block, iv)
    return &cipher.StreamWriter{S: stream, W: w}, nil
    }

    // EncryptedReader wraps r with an OFB cipher stream.
    func EncryptedReader(key string, r io.Reader) (*cipher.StreamReader, error) {
    block, err := newBlock(key)
    if err != nil {
    return nil, err
    }

    // read initial value
    iv := make([]byte, aes.BlockSize)
    n, err := r.Read(iv)
    if err != nil || n != len(iv) {
    return nil, errors.New("could not read initial value")
    }

    stream := cipher.NewOFB(block, iv)
    return &cipher.StreamReader{S: stream, R: r}, nil
    }

    func newBlock(key string) (cipher.Block, error) {
    hash := md5.Sum([]byte(key))
    block, err := aes.NewCipher(hash[:])
    if err != nil {
    return nil, err
    }
    return block, nil
    }

    func main() {

    // Load your secret key from a safe
    // place and use for encrypt/decrypt
    secretKey := "1234567890abcdefghijk"

    /******************************************/
    /* example to encrypt/decrypt to a string */
    /******************************************/
    var sb strings.Builder
    w, err := EncryptedWriter(secretKey, &sb)
    if err != nil {
    panic(err)
    }

    // as w is a StreamWriter we can write/encrypt directly to it
    w.Write([]byte("Hello gophers!"))
    encrypted := sb.String()
    fmt.Printf("encrypted message = %s\n", encrypted)

    // as r is a StreamReader we can decrypt/read directly from it
    r, err := EncryptedReader(secretKey, strings.NewReader(encrypted))
    if err != nil {
    panic(err)
    }

    decrypted, _ := ioutil.ReadAll(r)
    fmt.Printf("decrypted message = %s\n", decrypted)

    /*********************************************************/
    /* example to encrypt/decrypt to a file */
    /* skipping error handling and file.Close() for examples */
    /*********************************************************/
    file, err := os.Create("/tmp/encmsg.txt")
    if err != nil {
    panic(err)
    }

    w, _ = EncryptedWriter(secretKey, file)
    io.WriteString(w, "Hello again gophers!")

    file, _ = os.Open("/tmp/encmsg.txt")
    r, _ = EncryptedReader(secretKey, file)
    contents, _ := ioutil.ReadAll(r)
    fmt.Printf("contents of decrypted file = %s\n", contents)
    }