-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (96 loc) · 2.94 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
package main
import (
"fmt"
"strconv"
"strings"
)
func encryptMessage (message string) {
// Generate key for each character
var charCountInMessage = len(message)
// stores generated secret keys
var cipherKeys string
// stores encrypted message i.e ciphertext
var cipherTexts string
/*
* Produce random key and corresponding cipherText for each character in the message
*/
for i:=0; i < charCountInMessage; i++ {
randomKey := GenerateRandomBinary()
hexValue, err := ConvertBinaryToHex(randomKey)
if err != nil {
fmt.Println("Error:", err)
} else {
// Doing this to avoid additing space before the first cipher Key
if i == 0 {
cipherKeys += hexValue
} else {
cipherKeys += " " + hexValue
}
}
// Gets the corresponding ascii value of a current character
asciiValue := byte(message[i])
// Convert to binary character
binaryRepresentation := fmt.Sprintf("%08b", asciiValue)
// find the bitwise (xor) of the secret key and binary representation of current character
xorResult, err := FindXOR(randomKey, binaryRepresentation)
if err != nil {
fmt.Println("Error:", err)
} else {
// convert binary to hex
hexValue, err := ConvertBinaryToHex(xorResult)
if err != nil {
fmt.Println("Error:", err)
} else {
if i == 0 {
cipherTexts += hexValue
} else {
cipherTexts += " " + hexValue
}
}
}
}
fmt.Println("Message encrypted:")
fmt.Printf("Cipher Key => : %s\n",cipherKeys)
fmt.Printf("Encrypted Message =>: %s\n",cipherTexts)
}
func decryptMessage(cipherText, cipherKey string) {
// stores the decrypted message
var message string
// Split the data string into individual hex strings
cipherTextAsArray := strings.Split(cipherText, " ")
cipherKeyAsArray := strings.Split(cipherKey, " ")
for i:=0; i<len(cipherTextAsArray); i++ {
// convert the current hex values for text and key into binary
TextInBinaryFormat, textErr := ConvertHexToBinary(cipherTextAsArray[i])
KeyInBinaryFormat, keyErr := ConvertHexToBinary(cipherKeyAsArray[i])
if textErr != nil && keyErr != nil {
fmt.Println("Error:", textErr)
} else {
xorResult, err := FindXOR(KeyInBinaryFormat, TextInBinaryFormat)
if err != nil {
fmt.Println("Error:", err)
} else {
// convert the bitwise(xor) result to whole number
number, err := strconv.ParseUint(xorResult, 2, 8)
if err != nil {
fmt.Println("Error:", err)
} else {
// get the corresponding ascii character
char := rune(number)
message += string(char)
}
}
}
}
fmt.Println("Message Decrypted:")
fmt.Println("message =>",message)
}
func main() {
// Encrypting a message
encryptMessage("HELLO, WORLD!")
fmt.Println("-------------------------------------")
// Decrypting a message using the secret key and encrypted message (both in hex format)
cipherText := "EE E3 9 A3 56 7 D6 5C 52 60 12 93 11"
cipherKey := "A6 A6 45 EF 19 2B F6 B 1D 32 5E D7 30"
decryptMessage(cipherText, cipherKey)
}