otp generation and encryption added with go
This commit is contained in:
parent
dacdf6dcd3
commit
c3cb8eaf83
33
Go/main.go
Normal file
33
Go/main.go
Normal file
@ -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()
|
||||
}
|
BIN
Go/one-time-pad-utils
Executable file
BIN
Go/one-time-pad-utils
Executable file
Binary file not shown.
51
Go/otp_encrypt/otp_encrypt.go
Normal file
51
Go/otp_encrypt/otp_encrypt.go
Normal file
@ -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, ""))
|
||||
|
||||
|
||||
}
|
33
Go/otp_gen/otp_gen.go
Normal file
33
Go/otp_gen/otp_gen.go
Normal file
@ -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
|
||||
}
|
40
Go/pad_definitions/pad_definitions.go
Normal file
40
Go/pad_definitions/pad_definitions.go
Normal file
@ -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",
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user