You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.3 KiB
51 lines
1.3 KiB
package service |
|
|
|
import ( |
|
"bytes" |
|
"crypto/aes" |
|
"crypto/cipher" |
|
) |
|
|
|
var key = []byte("bili_account_enc") |
|
|
|
// Encrypt aes encrypt |
|
func Encrypt(origData []byte) ([]byte, error) { |
|
block, err := aes.NewCipher(key) |
|
if err != nil { |
|
return nil, err |
|
} |
|
blockSize := block.BlockSize() |
|
origData = PKCS5Padding(origData, blockSize) |
|
blockMode := cipher.NewCBCEncrypter(block, key[:blockSize]) |
|
crypted := make([]byte, len(origData)) |
|
blockMode.CryptBlocks(crypted, origData) |
|
return crypted, nil |
|
} |
|
|
|
// Decrypt aes encrypt |
|
func Decrypt(crypted []byte) ([]byte, error) { |
|
block, err := aes.NewCipher(key) |
|
if err != nil { |
|
return nil, err |
|
} |
|
blockSize := block.BlockSize() |
|
blockMode := cipher.NewCBCDecrypter(block, key[:blockSize]) |
|
origData := make([]byte, len(crypted)) |
|
blockMode.CryptBlocks(origData, crypted) |
|
origData = PKCS5UnPadding(origData) |
|
return origData, nil |
|
} |
|
|
|
// PKCS5Padding padding |
|
func PKCS5Padding(ciphertext []byte, blockSize int) []byte { |
|
padding := blockSize - len(ciphertext)%blockSize |
|
padtext := bytes.Repeat([]byte{byte(padding)}, padding) |
|
return append(ciphertext, padtext...) |
|
} |
|
|
|
// PKCS5UnPadding unpadding |
|
func PKCS5UnPadding(origData []byte) []byte { |
|
length := len(origData) |
|
unpadding := int(origData[length-1]) |
|
return origData[:(length - unpadding)] |
|
}
|
|
|