12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- package utilx
- import (
- "crypto/aes"
- "crypto/md5"
- "encoding/hex"
- )
- func Md5(input []byte) string {
- hash := md5.New()
- hash.Write(input)
- return hex.EncodeToString(hash.Sum(nil))
- }
- //AesEncryptECB AES-EBC
- func AesEncryptECB(origData []byte, key []byte) (encrypted []byte) {
- cipher, _ := aes.NewCipher(generateKey(key))
- length := (len(origData) + aes.BlockSize) / aes.BlockSize
- plain := make([]byte, length*aes.BlockSize)
- copy(plain, origData)
- pad := byte(len(plain) - len(origData))
- for i := len(origData); i < len(plain); i++ {
- plain[i] = pad
- }
- encrypted = make([]byte, len(plain))
- // 分组分块加密
- for bs, be := 0, cipher.BlockSize(); bs <= len(origData); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
- cipher.Encrypt(encrypted[bs:be], plain[bs:be])
- }
- return encrypted
- }
- //AesDecryptECB AES-EBC
- func AesDecryptECB(encrypted []byte, key []byte) (decrypted []byte) {
- cipher, _ := aes.NewCipher(generateKey(key))
- decrypted = make([]byte, len(encrypted))
- //
- for bs, be := 0, cipher.BlockSize(); bs < len(encrypted); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
- cipher.Decrypt(decrypted[bs:be], encrypted[bs:be])
- }
- trim := 0
- if len(decrypted) > 0 {
- trim = len(decrypted) - int(decrypted[len(decrypted)-1])
- }
- return decrypted[:trim]
- }
- func generateKey(key []byte) (genKey []byte) {
- genKey = make([]byte, 16)
- copy(genKey, key)
- for i := 16; i < len(key); {
- for j := 0; j < 16 && i < len(key); j, i = j+1, i+1 {
- genKey[j] ^= key[i]
- }
- }
- return genKey
- }
|