2016-02-11 22:43:09 -08:00
|
|
|
package main
|
|
|
|
|
2016-02-11 22:54:51 -08:00
|
|
|
import (
|
|
|
|
"fmt"
|
2016-03-11 23:16:08 -08:00
|
|
|
"log"
|
2016-02-11 22:54:51 -08:00
|
|
|
"math/rand"
|
2016-03-11 21:31:57 -08:00
|
|
|
"os"
|
2016-03-12 21:04:59 -08:00
|
|
|
"strings"
|
2016-02-11 22:54:51 -08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"s.mcquay.me/dm/psyfer/psyfer"
|
2016-02-22 18:01:19 -08:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2016-02-11 22:54:51 -08:00
|
|
|
)
|
|
|
|
|
2016-03-14 12:50:45 -07:00
|
|
|
func clean(input string) string {
|
|
|
|
input = strings.ToUpper(strings.Replace(input, ",", "", -1))
|
|
|
|
input = strings.ToUpper(strings.Replace(input, ".", "", -1))
|
|
|
|
input = strings.ToUpper(strings.Replace(input, "'", "", -1))
|
|
|
|
input = strings.ToUpper(strings.Replace(input, "\"", "", -1))
|
|
|
|
input = strings.ToUpper(strings.Replace(input, "(", "", -1))
|
|
|
|
input = strings.ToUpper(strings.Replace(input, ")", "", -1))
|
|
|
|
input = strings.ToUpper(strings.Replace(input, ";", "", -1))
|
|
|
|
input = strings.ToUpper(strings.Replace(input, ":", "", -1))
|
|
|
|
input = strings.ToUpper(strings.Replace(input, "!", "", -1))
|
|
|
|
input = strings.ToUpper(strings.Replace(input, "?", "", -1))
|
|
|
|
input = strings.ToUpper(strings.Replace(input, "$", "", -1))
|
|
|
|
return input
|
|
|
|
}
|
|
|
|
|
2016-02-11 22:54:51 -08:00
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
}
|
2016-02-11 22:43:09 -08:00
|
|
|
|
2016-03-11 23:16:08 -08:00
|
|
|
var key128 = psyfer.Block{
|
|
|
|
0x2b, 0x28, 0xab, 0x09,
|
|
|
|
0x7e, 0xae, 0xf7, 0xcf,
|
|
|
|
0x15, 0xd2, 0x15, 0x4f,
|
|
|
|
0x16, 0xa6, 0x88, 0x3c,
|
|
|
|
}
|
|
|
|
var key192 = psyfer.Block{
|
|
|
|
0x00, 0x04, 0x08, 0x0c, 0x10, 0x14,
|
|
|
|
0x01, 0x05, 0x09, 0x0d, 0x11, 0x15,
|
|
|
|
0x02, 0x06, 0x0a, 0x0e, 0x12, 0x16,
|
|
|
|
0x03, 0x07, 0x0b, 0x0f, 0x13, 0x17,
|
|
|
|
}
|
|
|
|
var key256 = psyfer.Block{
|
|
|
|
0x00, 0x04, 0x08, 0x0c, 0x10, 0x14, 0x18, 0x1c,
|
|
|
|
0x01, 0x05, 0x09, 0x0d, 0x11, 0x15, 0x19, 0x1d,
|
|
|
|
0x02, 0x06, 0x0a, 0x0e, 0x12, 0x16, 0x1a, 0x1e,
|
|
|
|
0x03, 0x07, 0x0b, 0x0f, 0x13, 0x17, 0x1b, 0x1f,
|
|
|
|
}
|
|
|
|
|
2016-02-11 22:43:09 -08:00
|
|
|
func main() {
|
2016-03-11 21:50:32 -08:00
|
|
|
var key string
|
2016-03-11 23:16:08 -08:00
|
|
|
var keysize int
|
2016-03-11 21:31:57 -08:00
|
|
|
var decrypt bool
|
2016-03-11 23:16:08 -08:00
|
|
|
var ascii bool
|
2016-02-22 18:01:19 -08:00
|
|
|
|
2016-03-11 21:31:57 -08:00
|
|
|
var trans = &cobra.Command{
|
|
|
|
Use: "trans mode",
|
|
|
|
Short: "transposition cipher",
|
|
|
|
Long: `perform transposition cipher`,
|
2016-02-22 18:01:19 -08:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2016-03-11 21:31:57 -08:00
|
|
|
fmt.Println("missing input, see -h (--help) for more info")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-03-11 21:36:35 -08:00
|
|
|
var sub = &cobra.Command{
|
2016-03-11 21:50:32 -08:00
|
|
|
Use: "sub",
|
2016-03-11 21:36:35 -08:00
|
|
|
Short: "substitution cipher",
|
|
|
|
Long: `perform substitution cipher`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2016-03-11 21:50:32 -08:00
|
|
|
if key == "" || len(args) < 1 {
|
|
|
|
fmt.Println("missing input, see -h (--help) for more info")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
psyfer.ReadConfig(key)
|
|
|
|
for _, arg := range args {
|
|
|
|
fmt.Println(psyfer.Substitution(arg))
|
|
|
|
}
|
2016-03-11 21:36:35 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-03-11 21:31:57 -08:00
|
|
|
var aes = &cobra.Command{
|
2016-03-11 23:16:08 -08:00
|
|
|
Use: "aes",
|
2016-03-11 21:31:57 -08:00
|
|
|
Short: "aes cipher",
|
|
|
|
Long: `perform aes cipher`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2016-03-11 23:16:08 -08:00
|
|
|
if len(args) < 1 {
|
|
|
|
fmt.Println("missing input, see -h (--help) for more info")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
for _, arg := range args {
|
|
|
|
all := psyfer.BlockGen(arg)
|
|
|
|
if ascii {
|
|
|
|
switch keysize {
|
|
|
|
case 128:
|
|
|
|
fmt.Println(psyfer.ToString(all, 128, key128, decrypt))
|
|
|
|
case 192:
|
|
|
|
fmt.Println(psyfer.ToString(all, 192, key192, decrypt))
|
|
|
|
case 256:
|
|
|
|
fmt.Println(psyfer.ToString(all, 256, key256, decrypt))
|
|
|
|
default:
|
|
|
|
log.Fatal("keysize not valid")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch keysize {
|
|
|
|
case 128:
|
|
|
|
fmt.Println(psyfer.ToHex(all, 128, key128, decrypt))
|
|
|
|
case 192:
|
|
|
|
fmt.Println(psyfer.ToHex(all, 192, key192, decrypt))
|
|
|
|
case 256:
|
|
|
|
fmt.Println(psyfer.ToHex(all, 256, key256, decrypt))
|
|
|
|
default:
|
|
|
|
log.Fatal("keysize not valid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-11 21:31:57 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var vig = &cobra.Command{
|
2016-03-12 23:06:24 -08:00
|
|
|
Use: "vig",
|
2016-03-14 13:59:24 -07:00
|
|
|
Short: "vigenere cipher",
|
2016-03-11 21:31:57 -08:00
|
|
|
Long: `perform vigenere cipher`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2016-03-12 21:04:59 -08:00
|
|
|
if len(args) < 1 || key == "" {
|
|
|
|
fmt.Println("missing input, see -h (--help) for more info")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
for _, arg := range args {
|
2016-03-14 12:50:45 -07:00
|
|
|
arg = clean(arg)
|
2016-03-12 21:04:59 -08:00
|
|
|
arg = strings.ToUpper(strings.Replace(arg, " ", "", -1))
|
|
|
|
key = strings.ToUpper(strings.Replace(key, " ", "", -1))
|
|
|
|
fmt.Printf("%v\n", psyfer.VigenereCipher(arg, key, decrypt))
|
|
|
|
}
|
2016-03-11 21:31:57 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-03-12 23:06:24 -08:00
|
|
|
var guess = &cobra.Command{
|
|
|
|
Use: "guess",
|
2016-03-14 12:50:45 -07:00
|
|
|
Short: "guess caesar cipher",
|
|
|
|
Long: `will try to guess a caesar cipher`,
|
2016-03-12 23:06:24 -08:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
if len(args) < 1 {
|
|
|
|
fmt.Println("missing input, see -h (--help) for more info")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2016-03-14 12:50:45 -07:00
|
|
|
fmt.Println("Top 5 possibilities:")
|
|
|
|
for _, arg := range args {
|
|
|
|
arg = clean(arg)
|
|
|
|
psyfer.Guess(arg)
|
|
|
|
}
|
2016-03-12 23:06:24 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-03-11 21:31:57 -08:00
|
|
|
var random = &cobra.Command{
|
|
|
|
Use: "random",
|
|
|
|
Short: "randomly transpose",
|
|
|
|
Long: `randomly transposes input`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
if len(args) < 1 {
|
|
|
|
fmt.Println("Please provide an input string")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
for _, arg := range args {
|
|
|
|
fmt.Println(psyfer.TransposeRandom(arg))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var railfence = &cobra.Command{
|
|
|
|
Use: "railfence",
|
|
|
|
Short: "railfence transpose",
|
|
|
|
Long: `performs railfence transposition on input`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
if len(args) < 1 {
|
|
|
|
fmt.Println("Please provide an input string")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
for _, arg := range args {
|
|
|
|
if decrypt {
|
|
|
|
fmt.Println(psyfer.DeTransposeRailFence(arg))
|
|
|
|
} else {
|
|
|
|
fmt.Println(psyfer.TransposeRailFence(arg))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var split = &cobra.Command{
|
|
|
|
Use: "split",
|
|
|
|
Short: "split transpose",
|
|
|
|
Long: `performs split transposition on input`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
if len(args) < 1 {
|
|
|
|
fmt.Println("Please provide an input string")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
for _, arg := range args {
|
|
|
|
if decrypt {
|
|
|
|
fmt.Println(psyfer.DeTransposeSplit(arg))
|
|
|
|
} else {
|
|
|
|
fmt.Println(psyfer.TransposeSplit(arg))
|
2016-02-22 18:01:19 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-03-11 23:16:08 -08:00
|
|
|
//transpose flags
|
2016-03-11 21:31:57 -08:00
|
|
|
split.Flags().BoolVarP(
|
|
|
|
&decrypt,
|
|
|
|
"decrypt",
|
|
|
|
"d",
|
|
|
|
false,
|
|
|
|
"decrypt",
|
2016-02-22 18:01:19 -08:00
|
|
|
)
|
2016-03-11 21:31:57 -08:00
|
|
|
railfence.Flags().BoolVarP(
|
|
|
|
&decrypt,
|
|
|
|
"decrypt",
|
|
|
|
"d",
|
|
|
|
false,
|
|
|
|
"decrypt",
|
2016-02-22 18:01:19 -08:00
|
|
|
)
|
2016-03-11 23:16:08 -08:00
|
|
|
//sub flags
|
2016-03-11 21:31:57 -08:00
|
|
|
sub.Flags().StringVarP(
|
2016-03-11 21:50:32 -08:00
|
|
|
&key,
|
|
|
|
"key",
|
|
|
|
"k",
|
2016-02-22 18:01:19 -08:00
|
|
|
"",
|
2016-03-11 21:50:32 -08:00
|
|
|
"file containing key",
|
2016-02-22 18:01:19 -08:00
|
|
|
)
|
2016-03-11 23:16:08 -08:00
|
|
|
//aes flags
|
|
|
|
aes.Flags().IntVarP(
|
|
|
|
&keysize,
|
|
|
|
"keysize",
|
|
|
|
"k",
|
|
|
|
0,
|
|
|
|
"keysize",
|
|
|
|
)
|
|
|
|
aes.Flags().BoolVarP(
|
|
|
|
&ascii,
|
|
|
|
"ascii",
|
|
|
|
"a",
|
|
|
|
false,
|
|
|
|
"display in ascii (may mess up terminal :) )",
|
|
|
|
)
|
|
|
|
aes.Flags().BoolVarP(
|
|
|
|
&decrypt,
|
|
|
|
"decrypt",
|
|
|
|
"d",
|
|
|
|
false,
|
|
|
|
"decrypt",
|
|
|
|
)
|
2016-03-12 21:04:59 -08:00
|
|
|
//virenere flags
|
|
|
|
vig.Flags().StringVarP(
|
|
|
|
&key,
|
|
|
|
"key",
|
|
|
|
"k",
|
|
|
|
"",
|
|
|
|
"encryption key string",
|
|
|
|
)
|
|
|
|
vig.Flags().BoolVarP(
|
|
|
|
&decrypt,
|
|
|
|
"decrypt",
|
|
|
|
"d",
|
|
|
|
false,
|
|
|
|
"decrypt",
|
|
|
|
)
|
2016-03-11 21:31:57 -08:00
|
|
|
|
2016-02-22 18:01:19 -08:00
|
|
|
var rootCmd = &cobra.Command{Use: "app"}
|
2016-03-12 23:06:24 -08:00
|
|
|
rootCmd.AddCommand(sub, aes, trans, vig, guess)
|
2016-03-11 21:31:57 -08:00
|
|
|
trans.AddCommand(random, railfence, split)
|
2016-02-22 18:01:19 -08:00
|
|
|
rootCmd.Execute()
|
2016-02-11 22:43:09 -08:00
|
|
|
}
|