overflow_probe/file_cipher/file_cipher.go
insanity@loafle.com fbf679b13a fix
2017-08-04 12:02:50 +09:00

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()
}