mirror of
https://github.com/D3Ext/Hooka
synced 2026-06-08 10:49:57 +00:00
68 lines
1.4 KiB
Go
68 lines
1.4 KiB
Go
package utils
|
|
|
|
import (
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
"crypto/rand"
|
|
"errors"
|
|
"bytes"
|
|
)
|
|
|
|
func AESEncrypt(plaintext []byte, iv []byte, key []byte) ([]byte, error) {
|
|
block, err := aes.NewCipher(key)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if string(plaintext) == "" {
|
|
return nil, errors.New("string to encrypt is empty")
|
|
}
|
|
|
|
ecb := cipher.NewCBCEncrypter(block, iv)
|
|
content := []byte(plaintext)
|
|
content = PKCS5Padding(content, block.BlockSize())
|
|
crypted := make([]byte, len(content))
|
|
ecb.CryptBlocks(crypted, content)
|
|
|
|
return crypted, nil
|
|
}
|
|
|
|
func AESDecrypt(ciphertext []byte, iv []byte, key []byte) ([]byte, error) {
|
|
block, err := aes.NewCipher(key)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if len(ciphertext) == 0 {
|
|
return nil, errors.New("ciphertext cannot be empty")
|
|
}
|
|
|
|
ecb := cipher.NewCBCDecrypter(block, iv)
|
|
decrypted := make([]byte, len(ciphertext))
|
|
ecb.CryptBlocks(decrypted, ciphertext)
|
|
|
|
return PKCS5Trimming(decrypted), nil
|
|
}
|
|
|
|
func GenerateIV() ([]byte, error) {
|
|
iv := make([]byte, aes.BlockSize)
|
|
_, err := rand.Read(iv)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return iv, nil
|
|
}
|
|
|
|
func PKCS5Trimming(encrypt []byte) []byte {
|
|
padding := encrypt[len(encrypt)-1]
|
|
return encrypt[:len(encrypt)-int(padding)]
|
|
}
|
|
|
|
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
|
|
padding := blockSize - len(ciphertext)%blockSize
|
|
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
|
|
return append(ciphertext, padtext...)
|
|
}
|
|
|