2017-08-03 10:08:34 +00:00
|
|
|
package file_cipher_go
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2017-08-04 03:02:50 +00:00
|
|
|
var KEY = []byte("GIDISLLWGHX.EIIEUYWLIGLWI")
|
2017-08-03 10:08:34 +00:00
|
|
|
|
|
|
|
func Encrypt(filePath, destName string) string {
|
|
|
|
|
|
|
|
inFile, err := os.Open(filePath)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer inFile.Close()
|
|
|
|
|
|
|
|
block, err := aes.NewCipher(KEY)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var iv [aes.BlockSize]byte
|
|
|
|
stream := cipher.NewOFB(block, iv[:])
|
|
|
|
|
|
|
|
outFile, err := os.OpenFile(destName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer outFile.Close()
|
|
|
|
|
|
|
|
writer := &cipher.StreamWriter{S: stream, W: outFile}
|
|
|
|
if _, err := io.Copy(writer, inFile); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return outFile.Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Decrypt(filePath, destName string) string {
|
|
|
|
inFile, err := os.Open(filePath)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer inFile.Close()
|
|
|
|
|
|
|
|
block, err := aes.NewCipher(KEY)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var iv [aes.BlockSize]byte
|
|
|
|
stream := cipher.NewOFB(block, iv[:])
|
|
|
|
|
|
|
|
outFile, err := os.OpenFile(destName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer outFile.Close()
|
|
|
|
|
|
|
|
reader := &cipher.StreamReader{S: stream, R: inFile}
|
|
|
|
if _, err := io.Copy(outFile, reader); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return outFile.Name()
|
|
|
|
}
|