Skip to content

Commit 26611f5

Browse files
committed
First commit
1 parent e3edf8b commit 26611f5

File tree

5 files changed

+147
-0
lines changed

5 files changed

+147
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313

1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
16+
.idea

atlas_aes.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package AtlasInsideAES
2+
3+
import (
4+
"bytes"
5+
"crypto/aes"
6+
"crypto/cipher"
7+
"crypto/sha1"
8+
b64 "encoding/base64"
9+
"golang.org/x/crypto/pbkdf2"
10+
)
11+
12+
const IterationCount = 65536
13+
const SaltLength = 16
14+
const KeyLength = 16
15+
16+
func setKey(key []byte) (cipher.Block, []byte, error) {
17+
h := sha1.New()
18+
h.Write(key)
19+
salt := h.Sum(nil)
20+
keyEnc := pbkdf2.Key(key, salt, IterationCount, KeyLength, sha1.New)
21+
block, err := aes.NewCipher(keyEnc)
22+
if err != nil {
23+
return nil, nil, err
24+
}
25+
return block, salt[:SaltLength], nil
26+
}
27+
28+
func AESEncrypt(src string, key []byte) (string, error) {
29+
if len(src) == 0 {
30+
return "", &InvalidEncryptedDataError{"Invalid crypto"}
31+
}
32+
blkEncrypt, ivEncrypt, err := setKey(key)
33+
if err != nil {
34+
return "", &InvalidAESKeyError{"Invalid crypto"}
35+
}
36+
ecb := cipher.NewCBCEncrypter(blkEncrypt, ivEncrypt)
37+
content := []byte(src)
38+
content = PKCS5Padding(content, blkEncrypt.BlockSize())
39+
crypted := make([]byte, len(content))
40+
ecb.CryptBlocks(crypted, content)
41+
base64 := b64.StdEncoding.EncodeToString(crypted)
42+
return base64, nil
43+
}
44+
45+
func AESDecrypt(crypt string, key []byte) (string, error) {
46+
encryptedData, _ := b64.StdEncoding.DecodeString(crypt)
47+
if len(crypt) == 0 {
48+
return "", &InvalidPassphraseError{"Invalid crypto"}
49+
}
50+
blk, iv, err := setKey(key)
51+
if err != nil {
52+
return "", &InvalidAESKeyError{"Invalid crypto"}
53+
}
54+
ecb := cipher.NewCBCDecrypter(blk, iv)
55+
decrypted := make([]byte, len(encryptedData))
56+
ecb.CryptBlocks(decrypted, encryptedData)
57+
return string(PKCS5Trimming(decrypted)), nil
58+
}
59+
60+
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
61+
padding := blockSize - len(ciphertext)%blockSize
62+
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
63+
return append(ciphertext, padtext...)
64+
}
65+
66+
func PKCS5Trimming(encrypt []byte) []byte {
67+
padding := encrypt[len(encrypt)-1]
68+
return encrypt[:len(encrypt)-int(padding)]
69+
}

errors.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package AtlasInsideAES
2+
3+
type InvalidPassphraseError struct {
4+
Msg string
5+
}
6+
7+
func (err *InvalidPassphraseError) Error() string {
8+
return "Invalid encryption or decryption passphrase: " + err.Msg
9+
}
10+
11+
type InvalidEncryptedDataError struct {
12+
Msg string
13+
}
14+
15+
func (err *InvalidEncryptedDataError) Error() string {
16+
return "Invalid encrypted data: " + err.Msg
17+
}
18+
19+
type CouldNotObtainRandomSaltError struct {
20+
Msg string
21+
}
22+
23+
func (err *CouldNotObtainRandomSaltError) Error() string {
24+
return "Could not obtain random salt: " + err.Msg
25+
}
26+
27+
type CouldNotObtainRandomIVError struct {
28+
Msg string
29+
}
30+
31+
func (err *CouldNotObtainRandomIVError) Error() string {
32+
return "Could not obtain random IV: " + err.Msg
33+
}
34+
35+
type InvalidAESKeyError struct {
36+
Msg string
37+
}
38+
39+
func (err *InvalidAESKeyError) Error() string {
40+
return "Invalid AES key: " + err.Msg
41+
}
42+
43+
type InvalidIVError struct {
44+
Msg string
45+
}
46+
47+
func (err *InvalidIVError) Error() string {
48+
return "Invalid IV: " + err.Msg
49+
}
50+
51+
type InvalidSaltError struct {
52+
Msg string
53+
}
54+
55+
func (err *InvalidSaltError) Error() string {
56+
return "Invalid Salt: " + err.Msg
57+
}
58+
59+
type IncorrectPassphraseError struct{}
60+
61+
func (err *IncorrectPassphraseError) Error() string {
62+
return "Invalid decryption passphrase"
63+
}

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/AtlasInsideCorp/AtlasInsideAES
2+
3+
go 1.17
4+
5+
require golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29

go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29 h1:tkVvjkPTB7pnW3jnid7kNyAMPVWllTNOf/qKDze4p9o=
2+
golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
3+
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
4+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
5+
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
6+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
8+
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
9+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

0 commit comments

Comments
 (0)