-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
163 lines (118 loc) · 4.15 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
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
package main
import (
"encoding/hex"
"fmt"
"github.com/aaomidi/blockcipher/cbc"
"github.com/aaomidi/blockcipher/ctr"
"github.com/aaomidi/blockcipher/ecb"
"github.com/aaomidi/blockcipher"
x "github.com/aaomidi/go-tea/tea"
)
func main() {
test1()
//test2()
testCTR()
testECB()
testCBC()
}
func test1() {
fmt.Println("Test 1 - My implementation")
data := [8]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}
k, _ := hex.DecodeString("A56BABCD00000000FFFFFFFFABCDEF01")
plainText := x.Block(data)
cipherText := x.Block([8]byte{})
key := x.KeyFromBytes(k)
cipher := x.Cipher{Key: key}
cipher.Encrypt(&cipherText, &plainText)
decryptedText := x.Block([8]byte{})
cipher.Decrypt(&decryptedText, &cipherText)
fmt.Printf("\tPlaintext bytes: %v\n", plainText)
fmt.Printf("\tCiphertext bytes: %v\n", cipherText)
fmt.Printf("\tDecrypted bytes: %v\n", decryptedText)
fmt.Printf("\tDecrypted text: %v\n", hex.EncodeToString(decryptedText.Source()))
}
//func test2() {
// fmt.Println("Test 2 - My Interface Implementation")
// data := [8]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}
//
// k, _ := hex.DecodeString("A56BABCD00000000FFFFFFFFABCDEF01")
//
// plainText := data[:]
// cipherText := make([]byte, 8)
//
// key := x.KeyFromBytes(k)
//
// cipher := x.Cipher{Key: key}
//
// cipher.EncryptBlock(cipherText, plainText)
// decryptedText := make([]byte, 8)
// cipher.DecryptBlock(decryptedText, cipherText)
//
// fmt.Println(plainText)
// fmt.Println(cipherText)
// fmt.Println(decryptedText)
//}
func testCTR() {
fmt.Println("Test CTR")
k, _ := hex.DecodeString("A56BABCD00000000FFFFFFFFABCDEF01")
key := x.KeyFromBytes(k)
cipher := x.Cipher{Key: key}
method := ctr.CTR{
IVIncrement: ctr.IVIncrement(&cipher),
CryptoMethod: blockcipher.CryptoMethod(&cipher),
}
sentence := "Four score and seven years ago our fathers brought forth on this continent, a new\nnation, conceived in Liberty, and dedicated to the proposition that all men are\ncreated equal."
data := []byte(sentence)
dataBuffer := make([]byte, len(data))
copy(dataBuffer, data)
src := x.SliceChunk(dataBuffer, 8)
dst := make([][]byte, len(src))
fmt.Printf("\tPlaintext bytes: %v\n", src)
method.Apply(dst, src, []byte{2, 2, 2, 2, 2, 2, 2, 2})
fmt.Printf("\tEncrypted bytes: %v\n", dst)
method.Apply(src, dst, []byte{2, 2, 2, 2, 2, 2, 2, 2})
fmt.Printf("\tDecrypted bytes: %v\n", src)
fmt.Printf("\tDecrypted string: %s\n", string(x.JoinBytes(src)))
}
func testECB() {
fmt.Println("Test CTR")
k, _ := hex.DecodeString("A56BABCD00000000FFFFFFFFABCDEF01")
key := x.KeyFromBytes(k)
cipher := x.Cipher{Key: key}
method := ecb.ECB{
CryptoMethod: blockcipher.CryptoMethod(&cipher),
}
sentence := "Four score and seven years ago our fathers brought forth on this continent, a new\nnation, conceived in Liberty, and dedicated to the proposition that all men are\ncreated equal."
data := []byte(sentence)
dataBuffer := make([]byte, len(data))
copy(dataBuffer, data)
src := x.SliceChunk(dataBuffer, 8)
dst := make([][]byte, len(src))
fmt.Printf("\tPlaintext bytes: %v\n", src)
method.Encrypt(dst, src)
fmt.Printf("\tEncrypted bytes: %v\n", dst)
method.Decrypt(src, dst)
fmt.Printf("\tDecrypted bytes: %v\n", src)
fmt.Printf("\tDecrypted string: %s\n", string(x.JoinBytes(src)))
}
func testCBC() {
fmt.Println("Test CBC")
k, _ := hex.DecodeString("A56BABCD00000000FFFFFFFFABCDEF01")
key := x.KeyFromBytes(k)
cipher := x.Cipher{Key: key}
method := cbc.CBC{
CryptoMethod: blockcipher.CryptoMethod(&cipher),
}
sentence := "Four score and seven years ago our fathers brought forth on this continent, a new\nnation, conceived in Liberty, and dedicated to the proposition that all men are\ncreated equal."
data := []byte(sentence)
dataBuffer := make([]byte, len(data))
copy(dataBuffer, data)
src := x.SliceChunk(dataBuffer, 8)
dst := make([][]byte, len(src))
fmt.Printf("\tPlaintext bytes: %v\n", src)
method.Encrypt(dst, src, []byte{2, 2, 2, 2, 2, 2, 2, 2})
fmt.Printf("\tEncrypted bytes: %v\n", dst)
method.Decrypt(src, dst, []byte{2, 2, 2, 2, 2, 2, 2, 2})
fmt.Printf("\tDecrypted bytes: %v\n", src)
fmt.Printf("\tDecrypted string: %s\n", string(x.JoinBytes(src)))
}