Skip to content

Commit 895cc38

Browse files
committed
added aes cbc and ctr modes
1 parent 57cb5c5 commit 895cc38

File tree

6 files changed

+70
-23
lines changed

6 files changed

+70
-23
lines changed

cmd/aes/aes.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func main() {
1313
configureFlags()
1414
validateOptions()
1515

16-
gologger.Silent().Msg("AES Encryption/Decryption\n\n")
16+
gologger.Silent().Msg("AES Encryption & Decryption\n\n")
1717

1818
if options.Encrypt {
1919
aes.Encrypt(options)
@@ -33,7 +33,7 @@ func configureFlags() {
3333
set.BoolVarP(&options.Decrypt, "decrypt", "d", false, "run decryption"),
3434
set.StringVarP(&options.Message, "message", "m", "", "message to encrypt/decrypt"),
3535
set.StringVarP(&options.Key, "key", "k", "", "encryption/decryption key"),
36-
set.BoolVar(&options.ModeCBC, "cbc", true, "encrypt/decrypt with CBC Mode"),
36+
set.BoolVar(&options.ModeCBC, "cbc", false, "encrypt/decrypt with CBC Mode"),
3737
set.BoolVar(&options.ModeCTR, "ctr", false, "encrypt/decrypt with CTR Mode"),
3838
)
3939

@@ -57,6 +57,10 @@ func validateOptions() {
5757
if options.Message != "" && options.Key == "" {
5858
gologger.Fatal().Msg("encryption key wasn't provided")
5959
}
60+
61+
if !options.ModeCBC && !options.ModeCTR {
62+
options.ModeCBC = true
63+
}
6064
}
6165

6266
func setGroup(set *goflags.FlagSet, groupName, description string, flags ...*goflags.FlagData) {

go.mod

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ module github.com/arielril/aes
22

33
go 1.20
44

5+
require (
6+
github.com/pkg/errors v0.9.1
7+
github.com/projectdiscovery/goflags v0.1.8
8+
github.com/projectdiscovery/gologger v1.1.8
9+
)
10+
511
require (
612
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
713
github.com/aymerick/douceur v0.2.0 // indirect
@@ -18,10 +24,8 @@ require (
1824
github.com/modern-go/reflect2 v1.0.2 // indirect
1925
github.com/nwaples/rardecode v1.1.0 // indirect
2026
github.com/pierrec/lz4 v2.6.0+incompatible // indirect
21-
github.com/pkg/errors v0.9.1 // indirect
22-
github.com/projectdiscovery/goflags v0.1.8 // indirect
23-
github.com/projectdiscovery/gologger v1.1.8 // indirect
2427
github.com/projectdiscovery/utils v0.0.4-0.20221214110533-9f95ee986a54 // indirect
28+
github.com/rogpeppe/go-internal v1.10.0 // indirect
2529
github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca // indirect
2630
github.com/ulikunitz/xz v0.5.7 // indirect
2731
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect

go.sum

+11
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ github.com/cnf/structhash v0.0.0-20201127153200-e1b16c1ebc08 h1:ox2F0PSMlrAAiAdk
66
github.com/cnf/structhash v0.0.0-20201127153200-e1b16c1ebc08/go.mod h1:pCxVEbcm3AMg7ejXyorUXi6HQCzOIBf7zEDVPtw0/U4=
77
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
88
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
9+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
910
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1011
github.com/dsnet/compress v0.0.1 h1:PlZu0n3Tuv04TzpfPbrnI0HW/YwodEXDS+oPKahKF0Q=
1112
github.com/dsnet/compress v0.0.1/go.mod h1:Aw8dCMJ7RioblQeTqt88akK31OvO8Dhf5JflhBbQEHo=
1213
github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY=
14+
github.com/frankban/quicktest v1.11.3 h1:8sXhOn0uLys67V8EsXLc6eszDs8VXWxL3iRvebPhedY=
1315
github.com/frankban/quicktest v1.11.3/go.mod h1:wRf/ReqHper53s+kmmSZizM8NamnL3IM0I9ntUbOk+k=
1416
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
1517
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
1618
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
19+
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
1720
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
1821
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
1922
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
@@ -22,8 +25,10 @@ github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHm
2225
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
2326
github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
2427
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
28+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
2529
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
2630
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
31+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
2732
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
2833
github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8=
2934
github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
@@ -44,18 +49,22 @@ github.com/pierrec/lz4 v2.6.0+incompatible h1:Ix9yFKn1nSPBLFl/yZknTp8TU5G4Ps0JDm
4449
github.com/pierrec/lz4 v2.6.0+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
4550
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
4651
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
52+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4753
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
4854
github.com/projectdiscovery/goflags v0.1.8 h1:Urhm2Isq2BdRt8h4h062lHKYXO65RHRjGTDSkUwex/g=
4955
github.com/projectdiscovery/goflags v0.1.8/go.mod h1:Yxi9tclgwGczzDU65ntrwaIql5cXeTvW5j2WxFuF+Jk=
5056
github.com/projectdiscovery/gologger v1.1.8 h1:CFlCzGlqAhPqWIrAXBt1OVh5jkMs1qgoR/z4xhdzLNE=
5157
github.com/projectdiscovery/gologger v1.1.8/go.mod h1:bNyVaC1U/NpJtFkJltcesn01NR3K8Hg6RsLVce6yvrw=
5258
github.com/projectdiscovery/utils v0.0.4-0.20221214110533-9f95ee986a54 h1:/fZvw6gT1fzdmMLMBBw75OrJ0Z6g7dulQrxM9FRp1qU=
5359
github.com/projectdiscovery/utils v0.0.4-0.20221214110533-9f95ee986a54/go.mod h1:PCwA5YuCYWPgHaGiZmr53/SA9iGQmAnw7DSHuhr8VPQ=
60+
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
61+
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
5462
github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca h1:NugYot0LIVPxTvN8n+Kvkn6TrbMyxQiuvKdEwFdR9vI=
5563
github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca/go.mod h1:uugorj2VCxiV1x+LzaIdVa9b4S4qGAcH6cbhh4qVxOU=
5664
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
5765
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
5866
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
67+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
5968
github.com/ulikunitz/xz v0.5.6/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=
6069
github.com/ulikunitz/xz v0.5.7 h1:YvTNdFzX6+W5m9msiYg/zpkSURPPtOlzbqYjrFn7Yt4=
6170
github.com/ulikunitz/xz v0.5.7/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
@@ -77,6 +86,7 @@ golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
7786
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
7887
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
7988
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
89+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
8090
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
8191
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
8292
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -100,6 +110,7 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T
100110
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
101111
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
102112
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
113+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
103114
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
104115
gopkg.in/djherbis/times.v1 v1.3.0 h1:uxMS4iMtH6Pwsxog094W0FYldiNnfY/xba00vq6C2+o=
105116
gopkg.in/djherbis/times.v1 v1.3.0/go.mod h1:AQlg6unIsrsCEdQYhTzERy542dz6SFdQFZFv6mUY0P8=

internal/aes/decrypt.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"crypto/aes"
55
"crypto/cipher"
66
"crypto/rand"
7+
"encoding/hex"
78
"io"
89

910
"github.com/arielril/aes/pkg/types"
@@ -44,12 +45,13 @@ func decryptCBC(hexEncryptedMsg, hexKey string) string {
4445
if len(encryptedMsg)%aes.BlockSize != 0 {
4546
gologger.Fatal().Msgf("encrypted message size is not a multiple of the block size")
4647
}
48+
gologger.Info().Msgf("using iv=%s\n", hex.EncodeToString(iv))
4749

4850
mode := cipher.NewCBCDecrypter(aesBlock, []byte(iv))
4951
decryptedMsg := make([]byte, len(encryptedMsg))
5052
mode.CryptBlocks(decryptedMsg, []byte(encryptedMsg))
5153

52-
return string(pkcs5Trimming(decryptedMsg))
54+
return hex.EncodeToString(pkcs5Trimming(decryptedMsg))
5355
}
5456

5557
func decryptCTR(hexMsg, hexKey string) string {
@@ -68,6 +70,7 @@ func decryptCTR(hexMsg, hexKey string) string {
6870
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
6971
gologger.Fatal().Msgf("could not read iv value: %s\n", err)
7072
}
73+
gologger.Info().Msgf("using iv=%s\n", hex.EncodeToString(iv))
7174

7275
stream := cipher.NewCTR(block, iv)
7376
stream.XORKeyStream(decryptedMsg, msg[aes.BlockSize:])

internal/aes/encrypt.go

+32-9
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import (
44
"crypto/aes"
55
"crypto/cipher"
66
"crypto/rand"
7+
"encoding/hex"
78
"io"
89

910
"github.com/arielril/aes/pkg/types"
1011
"github.com/projectdiscovery/gologger"
1112
)
1213

14+
// Encrypt Run AES encryption using CBC or CTR modes
1315
func Encrypt(options *types.Options) {
1416
var encryptedMsg string
1517

@@ -19,9 +21,11 @@ func Encrypt(options *types.Options) {
1921
encryptedMsg = encryptCTR(options.Message, options.Key)
2022
}
2123

22-
gologger.Silent().Msgf("encrypted text: \n\n----------------------------\n%s\n----------------------------", encryptedMsg)
24+
gologger.Silent().Msgf("encrypted text: \n----------------------------\n%s\n----------------------------", encryptedMsg)
2325
}
2426

27+
// encryptCBC Encrypts a message using AES CBC mode with a random
28+
// initialization vector (iv)
2529
func encryptCBC(hexMsg, hexKey string) string {
2630
key, msg, err := decodeHexKeyMsg(hexKey, hexMsg)
2731
if err != nil {
@@ -33,23 +37,42 @@ func encryptCBC(hexMsg, hexKey string) string {
3337
gologger.Fatal().Msgf("could not create aes cipher: %s\n", err)
3438
}
3539

36-
cipherText := make([]byte, aes.BlockSize+len(msg))
37-
iv := cipherText[:aes.BlockSize]
38-
if _, err = io.ReadFull(rand.Reader, iv); err != nil {
39-
gologger.Fatal().Msgf("could not create iv: %s\n", err)
40+
msg = pkcs5Padding(msg, aes.BlockSize)
41+
42+
ciphertext := make([]byte, aes.BlockSize+len(msg))
43+
iv := ciphertext[:aes.BlockSize]
44+
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
45+
gologger.Fatal().Msgf("could not initialize iv: %s\n", err)
4046
}
47+
gologger.Info().Msgf("using iv=%s\n", hex.EncodeToString(iv))
4148

42-
cbc := cipher.NewCBCEncrypter(block, iv)
43-
cbc.CryptBlocks(cipherText[aes.BlockSize:], pkcs5Padding(cipherText, aes.BlockSize))
49+
mode := cipher.NewCBCEncrypter(block, iv)
50+
mode.CryptBlocks(ciphertext[aes.BlockSize:], msg)
4451

45-
return string(cipherText)
52+
return hex.EncodeToString(ciphertext)
4653
}
4754

55+
// encryptCTR Encrypts a message using AES CTR mode
4856
func encryptCTR(hexMsg, hexKey string) string {
4957
key, msg, err := decodeHexKeyMsg(hexKey, hexMsg)
5058
if err != nil {
5159
gologger.Fatal().Msg(err.Error())
5260
}
5361

54-
return string(key) + string(msg)
62+
block, err := aes.NewCipher(key)
63+
if err != nil {
64+
gologger.Fatal().Msgf("could not create aes cipher: %s\n", err)
65+
}
66+
67+
ciphertext := make([]byte, aes.BlockSize+len(msg))
68+
iv := ciphertext[:aes.BlockSize]
69+
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
70+
gologger.Fatal().Msgf("could initialize iv: %s\n", err)
71+
}
72+
gologger.Info().Msgf("using iv=%s\n", hex.EncodeToString(iv))
73+
74+
stream := cipher.NewCTR(block, iv)
75+
stream.XORKeyStream(ciphertext, msg)
76+
77+
return hex.EncodeToString(ciphertext)
5578
}

internal/aes/utilz.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/pkg/errors"
88
)
99

10+
// decodeHexKeyMsg Translates the Key and Message from hex to a byte slice
1011
func decodeHexKeyMsg(hexKey, hexMsg string) (key, msg []byte, err error) {
1112
key, err = hex.DecodeString(hexKey)
1213
if err != nil {
@@ -23,17 +24,18 @@ func decodeHexKeyMsg(hexKey, hexMsg string) (key, msg []byte, err error) {
2324
return
2425
}
2526

26-
/*
27-
Source: https://gist.github.com/hothero/7d085573f5cb7cdb5801d7adcf66dcf3
28-
*/
29-
27+
// pkcs5Padding Pad the msg content using PKCS5 algorithm
28+
//
29+
// Code Source: https://gist.github.com/hothero/7d085573f5cb7cdb5801d7adcf66dcf3
3030
func pkcs5Padding(cipherText []byte, blockSize int) []byte {
31-
padding := blockSize - len(cipherText)%blockSize
32-
padText := bytes.Repeat([]byte{byte(padding)}, padding)
31+
paddingLength := blockSize - len(cipherText)%blockSize
32+
padText := bytes.Repeat([]byte{byte(paddingLength)}, paddingLength)
3333
return append(cipherText, padText...)
3434
}
3535

36+
// pkcs5Trimming Removes the PKCS5 padding on the encrypted message
3637
func pkcs5Trimming(encryptedText []byte) []byte {
37-
padding := encryptedText[len(encryptedText)-1]
38-
return encryptedText[:len(encryptedText)-int(padding)]
38+
// the length of the padding IS in the last block, even if it is 0 length
39+
paddingLength := encryptedText[len(encryptedText)-1]
40+
return encryptedText[:len(encryptedText)-int(paddingLength)]
3941
}

0 commit comments

Comments
 (0)