/
crypt.go
185 lines (150 loc) · 4.33 KB
/
crypt.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// This file is part of OpenRansim.
//
// Foobar is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Foobar is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Foobar. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"crypto/aes"
"crypto/cipher"
"crypto/des"
"crypto/rand"
"crypto/rsa"
"crypto/sha512"
"encoding/hex"
"fmt"
"github.com/spf13/cobra"
"io"
rand2 "math/rand"
"time"
)
var CryptCmd = &cobra.Command{
Use: "crypt",
Short: "Realize cryptological operations",
Long: `Realize cryptological operations`,
RunE: func(cmd *cobra.Command, args []string) error {
key := "openransim000000"
if len(args) == 2 {
text, err := read_from_file(args[1])
check(err)
switch args[0] {
case "encrypt_aes":
ciphertext := encrypt_aes(string(text), key)
write_to_file(ciphertext, args[1])
case "decrypt":
plaintext := decrypt(string(text), key)
write_to_file(plaintext, args[1])
default:
fmt.Println("You can:\nencrypt_aes\ndecrypt\nexit")
}
} else {
fmt.Println("Two args is required.\n\tFirst: (encrypt_aes|decrypt)\n\tSecond: File path")
}
return nil
},
}
func decrypt(cipherstring string, keystring string) string {
// Byte array of the string
ciphertext := []byte(cipherstring)
// Key
key := []byte(keystring)
// Create the AES cipher
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
// Before even testing the decryption,
// if the text is too small, then it is incorrect
if len(ciphertext) < aes.BlockSize {
panic("Text is too short")
}
// Get the 16 byte IV
iv := ciphertext[:aes.BlockSize]
// Remove the IV from the ciphertext
ciphertext = ciphertext[aes.BlockSize:]
// Return a decrypted stream
stream := cipher.NewCFBDecrypter(block, iv)
// Decrypt bytes from ciphertext
stream.XORKeyStream(ciphertext, ciphertext)
return string(ciphertext)
}
func encrypt_aes(plainstring, keystring string) string {
// Byte array of the string
plaintext := []byte(plainstring)
// Key
key := []byte(keystring)
// Create the AES cipher
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
// Empty array of 16 + plaintext length
// Include the IV at the beginning
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
// Slice of first 16 bytes
iv := ciphertext[:aes.BlockSize]
// Write 16 rand bytes to fill iv
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
// Return an encrypted stream
stream := cipher.NewCFBEncrypter(block, iv)
// Encrypt bytes from plaintext to ciphertext
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
return string(ciphertext)
}
func encrypt_des(plainstring, keystring string) string {
// Byte array of the string
plaintext := []byte(plainstring)
// Key
key := []byte(keystring[0:8])
// Create the AES cipher
block, err := des.NewCipher(key)
if err != nil {
panic(err)
}
// Empty array of 16 + plaintext length
// Include the IV at the beginning
ciphertext := make([]byte, des.BlockSize+len(plaintext))
// Slice of first 16 bytes
iv := ciphertext[:des.BlockSize]
// Write 16 rand bytes to fill iv
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
// Return an encrypted stream
stream := cipher.NewCTR(block, iv)
// Encrypt bytes from plaintext to ciphertext
stream.XORKeyStream(ciphertext[des.BlockSize:], plaintext)
return string(ciphertext)
}
func generate_rsa_key() string {
fmt.Println("Generating key")
reader := rand.Reader
bitSize := 4096
private, err := rsa.GenerateKey(reader, bitSize)
check(err)
public := private.N
hash := sha512.Sum384([]byte(public.String()))
pass := hex.EncodeToString(hash[:])[0:16]
fmt.Println(pass)
fmt.Sprintf("Key generated: %s", pass)
return pass
}
func generate_key() string {
rand2.Seed(time.Now().UTC().UnixNano())
random := make([]byte, 16)
for i := 0; i < len(random); i++ {
random[i] = byte(65 + rand2.Intn(90-65))
}
return string(random)
}