implmented aes subcommand and functions
aes is now done! Uses default key value that is stored in program and handles block sizes that are not 16 hex values wide by adding 0x00 until it is 16 wide
This commit is contained in:
parent
878ca0be78
commit
05ec030738
81
main.go
81
main.go
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
@ -15,9 +16,30 @@ func init() {
|
|||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var key string
|
var key string
|
||||||
|
var keysize int
|
||||||
var decrypt bool
|
var decrypt bool
|
||||||
|
var ascii bool
|
||||||
|
|
||||||
var trans = &cobra.Command{
|
var trans = &cobra.Command{
|
||||||
Use: "trans mode",
|
Use: "trans mode",
|
||||||
@ -45,11 +67,40 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var aes = &cobra.Command{
|
var aes = &cobra.Command{
|
||||||
Use: "aes mode -c [cipher] -k [key] -i [input]",
|
Use: "aes",
|
||||||
Short: "aes cipher",
|
Short: "aes cipher",
|
||||||
Long: `perform aes cipher`,
|
Long: `perform aes cipher`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
fmt.Println("missing input, see -h (--help) for more info")
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,6 +174,7 @@ func main() {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//transpose flags
|
||||||
split.Flags().BoolVarP(
|
split.Flags().BoolVarP(
|
||||||
&decrypt,
|
&decrypt,
|
||||||
"decrypt",
|
"decrypt",
|
||||||
@ -137,7 +189,7 @@ func main() {
|
|||||||
false,
|
false,
|
||||||
"decrypt",
|
"decrypt",
|
||||||
)
|
)
|
||||||
|
//sub flags
|
||||||
sub.Flags().StringVarP(
|
sub.Flags().StringVarP(
|
||||||
&key,
|
&key,
|
||||||
"key",
|
"key",
|
||||||
@ -145,10 +197,31 @@ func main() {
|
|||||||
"",
|
"",
|
||||||
"file containing key",
|
"file containing key",
|
||||||
)
|
)
|
||||||
|
//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",
|
||||||
|
)
|
||||||
|
|
||||||
var rootCmd = &cobra.Command{Use: "app"}
|
var rootCmd = &cobra.Command{Use: "app"}
|
||||||
rootCmd.AddCommand(sub, aes, trans, vig)
|
rootCmd.AddCommand(sub, aes, trans, vig)
|
||||||
aes.AddCommand(crack)
|
|
||||||
trans.AddCommand(random, railfence, split)
|
trans.AddCommand(random, railfence, split)
|
||||||
vig.AddCommand(crack)
|
vig.AddCommand(crack)
|
||||||
rootCmd.Execute()
|
rootCmd.Execute()
|
||||||
|
@ -1,5 +1,12 @@
|
|||||||
package psyfer
|
package psyfer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
var Sbox = [][]byte{
|
var Sbox = [][]byte{
|
||||||
{0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76},
|
{0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76},
|
||||||
{0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0},
|
{0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0},
|
||||||
@ -73,7 +80,63 @@ type Block []byte
|
|||||||
var keyexpanded []Block
|
var keyexpanded []Block
|
||||||
var key Block
|
var key Block
|
||||||
|
|
||||||
|
func ToString(all []Block, keysize int, k Block, decrypt bool) string {
|
||||||
|
final := ""
|
||||||
|
for _, bl := range all {
|
||||||
|
result := Block{}
|
||||||
|
if decrypt {
|
||||||
|
result = InvCipher(bl, keysize, k)
|
||||||
|
} else {
|
||||||
|
result = Cipher(bl, keysize, k)
|
||||||
|
}
|
||||||
|
final += string(result)
|
||||||
|
}
|
||||||
|
return final
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToHex(all []Block, keysize int, k Block, decrypt bool) string {
|
||||||
|
final := ""
|
||||||
|
for _, bl := range all {
|
||||||
|
result := Block{}
|
||||||
|
if decrypt {
|
||||||
|
result = InvCipher(bl, keysize, k)
|
||||||
|
} else {
|
||||||
|
result = Cipher(bl, keysize, k)
|
||||||
|
}
|
||||||
|
for i := 0; i < 16; i++ {
|
||||||
|
final += fmt.Sprintf("0x%x ", result[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return final
|
||||||
|
}
|
||||||
|
|
||||||
|
func BlockGen(arg string) []Block {
|
||||||
|
all := []Block{}
|
||||||
|
b := Block{}
|
||||||
|
for i, char := range arg {
|
||||||
|
value, err := strconv.Atoi(hex.EncodeToString([]byte(string(char))))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
if i%16 == 0 && i > 0 {
|
||||||
|
all = append(all, b)
|
||||||
|
b = b[:0]
|
||||||
|
}
|
||||||
|
b = append(b, byte(value))
|
||||||
|
if i == len(arg)-1 {
|
||||||
|
all = append(all, b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return all
|
||||||
|
}
|
||||||
|
|
||||||
func Cipher(cur Block, bit int, incomingKey Block) Block {
|
func Cipher(cur Block, bit int, incomingKey Block) Block {
|
||||||
|
if len(cur) != 16 {
|
||||||
|
missing := 16 - len(cur)
|
||||||
|
for i := 0; i < missing; i++ {
|
||||||
|
cur = append(cur, 0x00)
|
||||||
|
}
|
||||||
|
}
|
||||||
key = Block{}
|
key = Block{}
|
||||||
keyexpanded = []Block{}
|
keyexpanded = []Block{}
|
||||||
AssignKey(incomingKey)
|
AssignKey(incomingKey)
|
||||||
@ -85,14 +148,6 @@ func Cipher(cur Block, bit int, incomingKey Block) Block {
|
|||||||
cur = ShiftRows(cur)
|
cur = ShiftRows(cur)
|
||||||
cur = MixColumns(cur)
|
cur = MixColumns(cur)
|
||||||
cur = AddRoundKey(cur, i+1)
|
cur = AddRoundKey(cur, i+1)
|
||||||
//fmt.Printf("\n")
|
|
||||||
//for i := 0; i < 16; i++ {
|
|
||||||
// fmt.Printf("% x", cur[i])
|
|
||||||
// if i%4 == 3 {
|
|
||||||
// fmt.Printf("\n")
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
cur = SubBytes(cur)
|
cur = SubBytes(cur)
|
||||||
cur = ShiftRows(cur)
|
cur = ShiftRows(cur)
|
||||||
@ -135,6 +190,12 @@ func Cipher(cur Block, bit int, incomingKey Block) Block {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func InvCipher(cur Block, bit int, incomingKey Block) Block {
|
func InvCipher(cur Block, bit int, incomingKey Block) Block {
|
||||||
|
if len(cur) != 16 {
|
||||||
|
missing := 16 - len(cur)
|
||||||
|
for i := 0; i < missing; i++ {
|
||||||
|
cur = append(cur, 0x00)
|
||||||
|
}
|
||||||
|
}
|
||||||
key = Block{}
|
key = Block{}
|
||||||
keyexpanded = []Block{}
|
keyexpanded = []Block{}
|
||||||
AssignKey(incomingKey)
|
AssignKey(incomingKey)
|
||||||
|
Loading…
Reference in New Issue
Block a user