70 lines
1.2 KiB
Go
70 lines
1.2 KiB
Go
package file_cipher_go
|
|
|
|
import (
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
"io"
|
|
"os"
|
|
)
|
|
|
|
var KEY = []byte("GIDISLLWGHX.EIIEUYWLIGLWI")
|
|
|
|
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()
|
|
}
|