diff --git a/Go/go.mod b/Go/go.mod new file mode 100644 index 0000000..37e6991 --- /dev/null +++ b/Go/go.mod @@ -0,0 +1,3 @@ +module one-time-pad-utils + +go 1.19 diff --git a/Go/main.go b/Go/main.go new file mode 100644 index 0000000..3be919a --- /dev/null +++ b/Go/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "fmt" + "flag" + "one-time-pad-utils/otp_gen" + "one-time-pad-utils/otp_encrypt" +) + +func main(){ + var genpad = flag.Bool("generate", false, "Generate a new One-Time Pad using CSPRNG") + var genpadchunks = flag.Int("chunks", 200, "Specify the amount of chunks to generate") + var encryptmessage = flag.String("encrypt", "", "Specify a message you wish to encrypt") + var decryptmessage = flag.String("decrypt", "", "Specify a message you wish to decrypt") + var otpkey = flag.String("key", "", "The specify required key for encryption and decryption") + var _ = encryptmessage + var _ = decryptmessage + var _ = otpkey + + flag.Parse() + + if *genpad { + fmt.Printf("%v\n", otpgen.GenerateOTP(*genpadchunks)) + return + } + + if *encryptmessage != "" { + fmt.Printf("%v\n", otpencrypt.OTPEncrypt(*encryptmessage,*otpkey)) + return + } + + flag.PrintDefaults() +} diff --git a/Go/one-time-pad-utils b/Go/one-time-pad-utils new file mode 100755 index 0000000..82831fd Binary files /dev/null and b/Go/one-time-pad-utils differ diff --git a/Go/otp_encrypt/otp_encrypt.go b/Go/otp_encrypt/otp_encrypt.go new file mode 100644 index 0000000..2b1227f --- /dev/null +++ b/Go/otp_encrypt/otp_encrypt.go @@ -0,0 +1,51 @@ +package otpencrypt + +import ( + "strings" + "one-time-pad-utils/pad_definitions" +) + +func OTPEncrypt(message string, key string) string { + message = strings.Replace(message, " ", "", -1) + messagesplit := strings.Split(message,"") + var messagesplitint []int + + key = strings.Replace(key, " ", "", -1) + keysplit := strings.Split(key,"") + var keysplitint []int + + for _, char := range messagesplit { + for n, c := range paddefinitions.NumToCharMap{ + if c == strings.ToUpper(char) { + messagesplitint = append(messagesplitint, n) + + } + } + } + + for _, char := range keysplit { + for n, c := range paddefinitions.NumToCharMap{ + if c == strings.ToUpper(char) { + keysplitint = append(keysplitint, n) + + } + } + } + + if len(keysplitint) < len(messagesplitint) { + return ("KEY TOO SHORT TO ENCRYPT MESSAGE") + } + + var encryptedmessage []string + + for pos, messagenum := range messagesplitint{ + encryptedmessage = append(encryptedmessage, paddefinitions.NumToCharMap[(messagenum + keysplitint[pos]) % 35]) + + } + + + + return (strings.Join(encryptedmessage, "")) + + +} diff --git a/Go/otp_gen/otp_gen.go b/Go/otp_gen/otp_gen.go new file mode 100644 index 0000000..49d95f6 --- /dev/null +++ b/Go/otp_gen/otp_gen.go @@ -0,0 +1,33 @@ +//One-Time Pad generator + +package otpgen + +import ( + "crypto/rand" + "math/big" + "one-time-pad-utils/pad_definitions" +) + + +func GenerateOTP(chunks int) string { + count := 1 + otpstring := "" + + for count < (chunks*5)+1{ + n, err := rand.Int(rand.Reader, big.NewInt(36)) // generate new cryptographically secure random number + if err != nil { + panic(err) + } + otpstring += paddefinitions.NumToCharMap[int(n.Int64())] // print that number using the character map + if count % 5 == 0 { // add a space every 5 characters, newline after 10 chunks + if count % 50 == 0{ + otpstring += "\n" + } else { + otpstring += " " + } + } + count+=1 + } + + return otpstring +} diff --git a/Go/pad_definitions/pad_definitions.go b/Go/pad_definitions/pad_definitions.go new file mode 100644 index 0000000..48298b2 --- /dev/null +++ b/Go/pad_definitions/pad_definitions.go @@ -0,0 +1,40 @@ +package paddefinitions + +var NumToCharMap = map[int]string{ + 0: "9", + 1: "A", + 2: "B", + 3: "C", + 4: "D", + 5: "E", + 6: "F", + 7: "G", + 8: "H", + 9: "I", + 10: "J", + 11: "K", + 12: "L", + 13: "M", + 14: "N", + 15: "O", + 16: "P", + 17: "Q", + 18: "R", + 19: "S", + 20: "T", + 21: "U", + 22: "V", + 23: "W", + 24: "X", + 25: "Y", + 26: "Z", + 27: "0", + 28: "1", + 29: "2", + 30: "3", + 31: "4", + 32: "5", + 33: "6", + 34: "7", + 35: "8", +}