I am trying to Decrypt and Encrypt a file downloaded as a stream using some sample code from golang. I am encrypting like this.
// read content from your file plaintext, err := ioutil.ReadFile(fileName) if err != nil { panic(err.Error()) } // this is a key key := []byte("example key 1234") 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] fmt.Println(iv) if _, err2 := io.ReadFull(rand.Reader, iv); err2 != nil { panic(err) } stream := cipher.NewCFBEncrypter(block, iv) stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext) metadata := make(map[string]string) //handle the file UploadFile("a_aes.txt", metadata, bytes.NewReader(ciphertext)) This appends the initialization vector to the beginning of the file. Now I when I download the file I get a io.ReadCloser so I am downloading it and trying to decrypt if like this. _, ioReaderCLoser, err := DownloadFile(fileName)if err != nil { panic(err.Error())} key := []byte("example key 1234") block, err := aes.NewCipher(key)if err != nil { panic(err)} //var iv [aes.BlockSize]byte iv := make([]byte, aes.BlockSize) ioReaderCLoser.Read(iv) stream := cipher.NewOFB(block, iv) outFile, err := os.OpenFile("decrypted-file.txt", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)if err != nil { panic(err)} defer outFile.Close() reader := &cipher.StreamReader{S: stream, R: ioReaderCLoser}// Copy the input file to the output file, decrypting as we go.if _, err := io.Copy(outFile, reader); err != nil { panic(err)} The problem is that the IV in golang example code is set to zero. So i had to change the sample code to read the IV off the beginning of the stream. I have changed the sample code from var iv [aes.BlockSize]byte stream := cipher.NewOFB(block, iv[:]) to get it to decypt at all. iv := make([]byte, aes.BlockSize) ioReaderCLoser.Read(iv) stream := cipher.NewOFB(block, iv) In order to get the iv off of the beginning of the file but this still leaves the end of my file jumbled for example when I encrypt something like "test Encrypted file" i get "test Encrypted fc�q+" I think the streams are getting out of sync but I'm unsure of how to fix this. What is the correct way to get the IV from the beginning of the file in this case. I feel like I'm missing something simple. Sorry for the long post. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.