Adjust some Go related things
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
Go/dotp
|
||||
22
Go/main.go
22
Go/main.go
@@ -1,16 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
otpdecrypt "dotp/otp_decrypt"
|
||||
otpencrypt "dotp/otp_encrypt"
|
||||
otpgenerate "dotp/otp_generate"
|
||||
"flag"
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
"one-time-pad-utils/otp_generate"
|
||||
"one-time-pad-utils/otp_encrypt"
|
||||
"one-time-pad-utils/otp_decrypt"
|
||||
)
|
||||
|
||||
func main(){
|
||||
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 (use quotes if you have spaces)")
|
||||
@@ -25,11 +25,11 @@ func main(){
|
||||
}
|
||||
|
||||
if *encryptmessage != "" {
|
||||
if *otpkey != ""{
|
||||
fmt.Printf("%v\n", otpencrypt.OTPEncrypt(*encryptmessage,*otpkey))
|
||||
if *otpkey != "" {
|
||||
fmt.Printf("%v\n", otpencrypt.OTPEncrypt(*encryptmessage, *otpkey))
|
||||
} else {
|
||||
encryptmessagestring := strings.Replace(*encryptmessage, " ", "", -1)
|
||||
lenofmessage := math.Ceil(float64(len(encryptmessagestring))/float64(5))
|
||||
lenofmessage := math.Ceil(float64(len(encryptmessagestring)) / float64(5))
|
||||
generated_key := otpgenerate.GenerateOTP(int(lenofmessage))
|
||||
fmt.Printf("GENERATED NEW KEY SINCE NONE WAS SPECIFIED:\n%v\n\n%v\n", generated_key, otpencrypt.OTPEncrypt(*encryptmessage, generated_key))
|
||||
}
|
||||
@@ -37,13 +37,13 @@ func main(){
|
||||
}
|
||||
|
||||
if *decryptmessage != "" {
|
||||
if *otpkey != ""{
|
||||
fmt.Printf("%v\n", otpdecrypt.OTPDecrypt(*decryptmessage,*otpkey))
|
||||
if *otpkey != "" {
|
||||
fmt.Printf("%v\n", otpdecrypt.OTPDecrypt(*decryptmessage, *otpkey))
|
||||
} else {
|
||||
fmt.Println("PLEASE SUPPLY A KEY")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
flag.PrintDefaults()
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -1,21 +1,21 @@
|
||||
package otpdecrypt
|
||||
|
||||
import (
|
||||
paddefinitions "dotp/pad_definitions"
|
||||
"strings"
|
||||
"one-time-pad-utils/pad_definitions"
|
||||
)
|
||||
|
||||
func OTPDecrypt(message string, key string) string {
|
||||
message = strings.Replace(message, " ", "", -1)
|
||||
messagesplit := strings.Split(message,"")
|
||||
messagesplit := strings.Split(message, "")
|
||||
var messagesplitint []int
|
||||
|
||||
key = strings.Replace(key, " ", "", -1)
|
||||
keysplit := strings.Split(key,"")
|
||||
keysplit := strings.Split(key, "")
|
||||
var keysplitint []int
|
||||
|
||||
for _, char := range messagesplit {
|
||||
for n, c := range paddefinitions.NumToCharMap{
|
||||
for n, c := range paddefinitions.NumToCharMap {
|
||||
if c == strings.ToUpper(char) {
|
||||
messagesplitint = append(messagesplitint, n)
|
||||
|
||||
@@ -24,7 +24,7 @@ func OTPDecrypt(message string, key string) string {
|
||||
}
|
||||
|
||||
for _, char := range keysplit {
|
||||
for n, c := range paddefinitions.NumToCharMap{
|
||||
for n, c := range paddefinitions.NumToCharMap {
|
||||
if c == strings.ToUpper(char) {
|
||||
keysplitint = append(keysplitint, n)
|
||||
|
||||
@@ -38,8 +38,8 @@ func OTPDecrypt(message string, key string) string {
|
||||
|
||||
var decryptedmessage []string
|
||||
|
||||
for pos, messagenum := range messagesplitint{
|
||||
decryptedmessage = append(decryptedmessage, paddefinitions.NumToCharMap[(((messagenum - keysplitint[pos]) % 36) + 36) % 36]) // crazy modulo for decryption
|
||||
for pos, messagenum := range messagesplitint {
|
||||
decryptedmessage = append(decryptedmessage, paddefinitions.NumToCharMap[(((messagenum-keysplitint[pos])%36)+36)%36]) // crazy modulo for decryption
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
package otpencrypt
|
||||
|
||||
import (
|
||||
paddefinitions "dotp/pad_definitions"
|
||||
"strings"
|
||||
"one-time-pad-utils/pad_definitions"
|
||||
)
|
||||
|
||||
func OTPEncrypt(message string, key string) string {
|
||||
message = strings.Replace(message, " ", "", -1)
|
||||
messagesplit := strings.Split(message,"")
|
||||
messagesplit := strings.Split(message, "")
|
||||
var messagesplitint []int
|
||||
|
||||
key = strings.Replace(key, " ", "", -1)
|
||||
keysplit := strings.Split(key,"")
|
||||
keysplit := strings.Split(key, "")
|
||||
var keysplitint []int
|
||||
|
||||
for _, char := range messagesplit {
|
||||
for n, c := range paddefinitions.NumToCharMap{
|
||||
for n, c := range paddefinitions.NumToCharMap {
|
||||
if c == strings.ToUpper(char) {
|
||||
messagesplitint = append(messagesplitint, n)
|
||||
|
||||
@@ -24,7 +24,7 @@ func OTPEncrypt(message string, key string) string {
|
||||
}
|
||||
|
||||
for _, char := range keysplit {
|
||||
for n, c := range paddefinitions.NumToCharMap{
|
||||
for n, c := range paddefinitions.NumToCharMap {
|
||||
if c == strings.ToUpper(char) {
|
||||
keysplitint = append(keysplitint, n)
|
||||
|
||||
@@ -38,11 +38,11 @@ func OTPEncrypt(message string, key string) string {
|
||||
|
||||
var encryptedmessage []string
|
||||
|
||||
for pos, messagenum := range messagesplitint{
|
||||
if pos != 0 && pos % 5 == 0 {
|
||||
for pos, messagenum := range messagesplitint {
|
||||
if pos != 0 && pos%5 == 0 {
|
||||
encryptedmessage = append(encryptedmessage, " ")
|
||||
}
|
||||
encryptedmessage = append(encryptedmessage, paddefinitions.NumToCharMap[(messagenum + keysplitint[pos]) % 36])
|
||||
encryptedmessage = append(encryptedmessage, paddefinitions.NumToCharMap[(messagenum+keysplitint[pos])%36])
|
||||
}
|
||||
|
||||
return (strings.Join(encryptedmessage, ""))
|
||||
|
||||
@@ -4,29 +4,28 @@ package otpgenerate
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
paddefinitions "dotp/pad_definitions"
|
||||
"math/big"
|
||||
"one-time-pad-utils/pad_definitions"
|
||||
)
|
||||
|
||||
|
||||
func GenerateOTP(chunks int) string {
|
||||
count := 1
|
||||
otpstring := ""
|
||||
|
||||
for count < (chunks*5)+1{
|
||||
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{
|
||||
if count%5 == 0 { // add a space every 5 characters, newline after 10 chunks
|
||||
if count%50 == 0 {
|
||||
otpstring += "\n"
|
||||
} else {
|
||||
otpstring += " "
|
||||
}
|
||||
}
|
||||
count+=1
|
||||
count += 1
|
||||
}
|
||||
|
||||
return otpstring
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
package paddefinitions
|
||||
|
||||
var NumToCharMap = map[int]string{
|
||||
0: "A",
|
||||
1: "B",
|
||||
2: "C",
|
||||
3: "D",
|
||||
4: "E",
|
||||
5: "F",
|
||||
6: "G",
|
||||
7: "H",
|
||||
8: "I",
|
||||
9: "J",
|
||||
0: "A",
|
||||
1: "B",
|
||||
2: "C",
|
||||
3: "D",
|
||||
4: "E",
|
||||
5: "F",
|
||||
6: "G",
|
||||
7: "H",
|
||||
8: "I",
|
||||
9: "J",
|
||||
10: "K",
|
||||
11: "L",
|
||||
12: "M",
|
||||
|
||||
Reference in New Issue
Block a user