0

I am using OpenSSL AES-256-CBC to encrypt some of my files

openssl aes-256-cbc -in filename.txt -out filename.enc -k password

How can those files be decrypted in Go?

Stageflix
  • 31
  • 4
  • 1
    Have you tried doing something like this example here: https://golang.org/pkg/crypto/cipher/#NewCBCDecrypter – Burak Serdar Jan 27 '20 at 18:57
  • 1
    OpenSSL uses a non-standard, insecure function to convert the password to an encryption key. One option is to derive the key yourself, and use the `-K` option. See https://stackoverflow.com/a/761902/3474 for details on how the built-in function works (you'd port that code to Go). – erickson Jan 27 '20 at 20:18
  • I'm not seeing the complexity here, why not just use OpenSSL bindings in Go? (such as https://godoc.org/github.com/spacemonkeygo/openssl) – Woodstock Jan 27 '20 at 21:03
  • @Woodstock Adding a dependency on OpenSSL (and all the pain that comes with cgo) just to decrypt a file is pretty overkill. Go has very mature crypto support built-in. – Luke Joshua Park Jan 28 '20 at 03:04
  • You can try something like https://github.com/funny/crypto/blob/master/aes256cbc/aes256cbc.go – Dinesh Kumar Jan 28 '20 at 12:54

2 Answers2

0

I hope this can help, make sure you read file bytes in opensslEncrypted variable:

Installation

git clone https://github.com/funny/crypto

Decrypt:

import (
  "fmt"
  "github.com/funny/crypto/aes256cbc"
)

func main() {
    opensslEncrypted := "U2FsdGVkX19ZM5qQJGe/d5A/4pccgH+arBGTp+QnWPU="
    passphrase := "z4yH36a6zerhfE5427ZV"

    dec, err := aes256cbc.DecryptString(passphrase, opensslEncrypted)
    if err != nil {
        fmt.Printf("An error occurred: %s\n", err)
    }

    fmt.Printf("Decrypted text: %s\n", string(dec))
}

Source: github

Dinesh Kumar
  • 545
  • 4
  • 17
0

yes , it's working, but you cmd need run this

openssl aes-256-cbc -in filename.txt -out filename.enc -k password -a