forked from Sid-Sun/seat-256-cfb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (91 loc) · 2.91 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"errors"
"fmt"
"github.com/sid-sun/seaturtle"
"io"
"io/ioutil"
"os"
)
func main() {
var toEncrypt bool
var text, passPhrase []byte
var outputPath string
if len(os.Args) == 4 || len(os.Args) == 5 {
if os.Args[1] == "-e" || os.Args[1] == "--encrypt" || os.Args[1] == "-encrypt" {
toEncrypt = true
} else if os.Args[1] == "-h" || os.Args[1] == "--help" || os.Args[1] == "-help" {
fmt.Printf("\nUsage:\n")
fmt.Printf(" For encryption: %s (--encrypt / -encrypt / -e) <input file> <passphrase file> <output file (optional)>.\n", os.Args[0])
fmt.Printf(" For decryption: %s (--decrypt / -decrypt / -d) <encrypted input> <passphrase file> <output file (optional)>.\n", os.Args[0])
} else if !(os.Args[1] == "-d" || os.Args[1] == "--decrypt" || os.Args[1] == "-decrypt") {
fmt.Println("Invalid argument:", os.Args[1])
}
if len(os.Args) == 5 {
outputPath = os.Args[4]
}
text = readFromFile(os.Args[2])
passPhrase = readFromFile(os.Args[3])
} else {
fmt.Printf("Usage:\n")
fmt.Printf(" For encryption: %s (--encrypt / -encrypt / -e) <input file> <passphrase file> <output file (optional)>.\n", os.Args[0])
fmt.Printf(" For decryption: %s (--decrypt / -decrypt / -d) <encrypted input> <passphrase file> <output file (optional)>.\n", os.Args[0])
os.Exit(0)
}
if outputPath == "" {
outputPath = os.Args[2] + ".seat"
}
key := sha256.Sum256(passPhrase)
var output []byte
var err error
if toEncrypt {
output, err = encrypt(key[:], text)
if err != nil {
panic(err.Error())
}
} else {
output, err = decrypt(key[:], text)
if err != nil {
panic(err.Error())
}
}
err = ioutil.WriteFile(outputPath, output, 0644)
if err != nil {
panic(err.Error())
}
}
func encrypt(key, plaintext []byte) ([]byte, error) {
block, err := seaturtle.NewCipher(key)
if err != nil {
return nil, err
}
originalPlaintextLength := len(plaintext)
emptyBytes := make([]byte, seaturtle.BlockSize)
plaintext = append(plaintext, emptyBytes...)
iv := plaintext[originalPlaintextLength:]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
}
cfb := cipher.NewCFBEncrypter(block, iv)
cfb.XORKeyStream(plaintext[:originalPlaintextLength], plaintext[:originalPlaintextLength])
//Rotate plaintext by the original text's length so that the iv bytes are in the front again
plaintext = append(plaintext[originalPlaintextLength:], plaintext[:originalPlaintextLength]...)
return plaintext, nil
}
func decrypt(key, ciphertext []byte) ([]byte, error) {
block, err := seaturtle.NewCipher(key)
if err != nil {
return nil, err
}
if len(ciphertext) < seaturtle.BlockSize {
return nil, errors.New("ciphertext too short")
}
iv := ciphertext[:seaturtle.BlockSize]
ciphertext = ciphertext[seaturtle.BlockSize:]
cfb := cipher.NewCFBDecrypter(block, iv)
cfb.XORKeyStream(ciphertext, ciphertext)
return ciphertext, nil
}