/
test.go
299 lines (256 loc) · 7.11 KB
/
test.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package main
import (
"bytes"
"crypto"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
cryptoRand "crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"fmt"
"io"
"math/big"
"net"
"strconv"
)
type PartyInfo struct {
IP []byte `json:"IP"`
Port int `json:"Port"`
PublicKey []byte `json:"PublicKey"`
}
func EncryptAES(encKey []byte, plainText []byte) (ciphertext []byte, ok bool) {
ok = false //assume failure
// encKey = append(encKey, hash)
c, err := aes.NewCipher(encKey)
if err != nil {
//fmt.Println(err)
}
gcm, err := cipher.NewGCM(c)
if err != nil {
//fmt.Println(err)
return
}
nonce := make([]byte, gcm.NonceSize())
if _, err = io.ReadFull(cryptoRand.Reader, nonce); err != nil {
//fmt.Println(err)
return
}
ciphertext = gcm.Seal(nonce, nonce, plainText, nil)
//fmt.Println(ciphertext)
ok = true
return
}
func DecryptAES(encKey []byte, cipherText []byte) (plainText []byte, ok bool) {
ok = false //assume failure
c, err := aes.NewCipher(encKey)
if err != nil {
//fmt.Println(err)
return
}
gcm, err := cipher.NewGCM(c)
if err != nil {
//fmt.Println(err)
return
}
nonceSize := gcm.NonceSize()
if len(cipherText) < nonceSize {
//fmt.Println(err)
return
}
nonce, cipherText := cipherText[:nonceSize], cipherText[nonceSize:]
plainText, err = gcm.Open(nil, nonce, cipherText, nil)
if err != nil {
//fmt.Println(err)
return
}
//fmt.Println(string(plaintext))
ok = true
return
}
//Convert32BytesToByteStream receives a byte array returns the first 32 bytes from it
func Convert32BytesToByteStream(msg [32]byte) []byte {
key := make([]byte, 32)
for jk, tmpo := range msg {
key[jk] = tmpo
}
return key
}
//SHA256Hash Hashes a byte array using sha256
func SHA256Hash(msg []byte) [32]byte {
return sha256.Sum256(msg)
}
// GetIP getting The IP
func GetIP() (net.IP, error) {
conn, err := net.Dial("udp", "8.8.8.8:80")
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP, err
}
// GetFreePort asks the kernel for a free open port that is ready to use.
func GetFreePort() (int, error) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
return 0, err
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
panic(err)
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port, nil
}
//RSAPublicEncrypt encrypts data with a given rsa.publickey
func RSAPublicEncrypt(key *rsa.PublicKey, data []byte) ([]byte, error) {
return rsa.EncryptPKCS1v15(rand.Reader, key, data)
}
//RSAPrivateDecrypt decrypts encrypted data with a given rsa.privatekey
func RSAPrivateDecrypt(key *rsa.PrivateKey, data []byte) ([]byte, error) {
return rsa.DecryptPKCS1v15(rand.Reader, key, data)
}
//GenerateRSAKey generates Public/Private Key pair, advised rsaKeySize = 2048
func GenerateRSAKey(rsaKeySize int) (*rsa.PrivateKey, *rsa.PublicKey) {
if rsaKeySize < 1 {
rsaKeySize = 2048
}
pri, err := rsa.GenerateKey(rand.Reader, rsaKeySize)
if err != nil {
panic(err)
}
return pri, &pri.PublicKey
}
//RSAPublicKeyFromBytes extracts rsa.publickey from its byte array encoding
func RSAPublicKeyFromBytes(key []byte) *rsa.PublicKey {
pk, err := x509.ParsePKCS1PublicKey(key)
if err != nil {
panic(err)
}
return pk
}
//BytesFromRSAPublicKey returns byte array encoding from an rsa.publickey
func BytesFromRSAPublicKey(pk *rsa.PublicKey) []byte {
pubBytes := x509.MarshalPKCS1PublicKey(pk)
return pubBytes
}
//BytesFromRSAPrivateKey returns byte array encoding from an rsa.privatekey
func BytesFromRSAPrivateKey(sk *rsa.PrivateKey) []byte {
priBytes, err := x509.MarshalPKCS8PrivateKey(sk)
if err != nil {
panic(err)
}
return priBytes
}
//RSAPrivateKeyFromBytes extracts rsa.privatekey from its byte array encoding
func RSAPrivateKeyFromBytes(key []byte) *rsa.PrivateKey {
pri, err := x509.ParsePKCS8PrivateKey(key)
if err != nil {
panic(err)
}
p, ok := pri.(*rsa.PrivateKey)
if !ok {
panic("Invalid Key type")
}
return p
}
//RSAPrivateSign makes a signature with a private key
func RSAPrivateSign(key *rsa.PrivateKey, data []byte) ([]byte, error) {
return rsa.SignPKCS1v15(rand.Reader, key, crypto.SHA256, Convert32BytesToByteStream(SHA256Hash(data)))
}
//RSAPrivateVerify verifies a signature made with a private key
func RSAPrivateVerify(key *rsa.PrivateKey, sign, data []byte) error {
h, err := RSAPrivateDecrypt(key, sign)
if err != nil {
return err
}
if !bytes.Equal(h, Convert32BytesToByteStream(SHA256Hash(data))) {
return rsa.ErrVerification
}
return nil
}
//RSAPublicSign makes a signature with a public key
func RSAPublicSign(key *rsa.PublicKey, data []byte) ([]byte, error) {
return RSAPublicEncrypt(key, Convert32BytesToByteStream(SHA256Hash(data)))
}
//RSAPublicVerify verifies a signature made with a public key
func RSAPublicVerify(key *rsa.PublicKey, sign, data []byte) error {
return rsa.VerifyPKCS1v15(key, crypto.SHA256, Convert32BytesToByteStream(SHA256Hash(data)), sign)
}
//GetPartyInfo for a party to extract his own communication info
func GetPartyInfo() (PartyInfo, []byte) {
port, err := GetFreePort()
if err != nil {
panic(err)
}
sk, pk := GenerateRSAKey(0)
if err != nil {
panic(err)
}
ip, err := GetIP()
if err != nil {
panic(err)
}
pI := PartyInfo{
IP: ip,
Port: port,
PublicKey: BytesFromRSAPublicKey(pk),
}
return pI, BytesFromRSAPrivateKey(sk)
}
func main() {
//ip := GetMyIP()
PI, skb := GetPartyInfo()
var ip net.IP
ip = PI.IP
fmt.Println(ip)
pk := RSAPublicKeyFromBytes(PI.PublicKey)
sk := RSAPrivateKeyFromBytes(skb)
// fmt.Println(pk)
//fmt.Println(sk)
msg1 := "Hi sala7"
hash := SHA256Hash([]byte("This is my key"))
key := Convert32BytesToByteStream(hash)
/*key := make([]byte, 32)
for jk, tmpo := range hash {
key[jk] = tmpo
}*/
//return key
cipherText, _ := EncryptAES(key, []byte(msg1))
cipher1, _ := RSAPublicEncrypt(pk, key)
key1, _ := RSAPrivateDecrypt(sk, cipher1)
plainText, _ := DecryptAES(key1, cipherText)
fmt.Println(string(plainText))
signature, _ := RSAPrivateSign(sk, []byte(msg1))
err := RSAPublicVerify(pk, signature, []byte(msg1))
if err == nil {
fmt.Println("Yes it's his signature")
}
signature2, _ := RSAPublicSign(pk, []byte(msg1))
err = RSAPrivateVerify(sk, signature2, []byte(msg1))
if err == nil {
fmt.Println("Yes it's his signature")
}
i1 := 1234
/** converting the i1 variable into a string using Itoa method */
str1 := strconv.Itoa(i1)
fmt.Println(str1)
i2 := 5678
/** converting the i2 variable into a string using FormatInt method */
str2 := strconv.FormatInt(int64(i2), 10)
fmt.Println(str2)
c := []byte("Hi World I'm Amr")
d := []byte("Yes I am")
e := ByteSliceMul(c, d)
fmt.Println("Now multiplying")
fmt.Println(new(big.Int).SetBytes(c))
fmt.Println(new(big.Int).SetBytes(d))
fmt.Println(new(big.Int).SetBytes(e))
test := new(big.Int)
fmt.Println(test.Mul(new(big.Int).SetBytes(c), new(big.Int).SetBytes(d)))
fmt.Println(test.Bytes())
}
//ByteSliceMul performs multiplication of A * B by converting them into Big Ints and storing the byte slice value into C
func ByteSliceMul(A []byte, B []byte) (C []byte) {
C = (new(big.Int).Mul(new(big.Int).SetBytes(A), new(big.Int).SetBytes(B))).Bytes()
return
}