github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/coder/sym.go (about) 1 package coder 2 3 import ( 4 "bytes" 5 "crypto/aes" 6 "crypto/cipher" 7 "crypto/des" 8 "crypto/rc4" 9 "encoding/base64" 10 "encoding/hex" 11 "fmt" 12 "strings" 13 ) 14 15 func AesDecrypt(content string, key string, iv string) string { 16 b, _ := base64.StdEncoding.DecodeString(content) 17 block, _ := aes.NewCipher([]byte(key)) 18 mode := cipher.NewCBCDecrypter(block, []byte(iv)) 19 originData := make([]byte, len(b)) 20 mode.CryptBlocks(originData, b) 21 origData := pkcs5UnPadding(originData) 22 return string(origData) 23 } 24 25 func AesEncrypt(content string, key string, iv string) string { 26 origData := pkcs5Padding([]byte(content), aes.BlockSize) 27 block, _ := aes.NewCipher([]byte(key)) 28 mode := cipher.NewCBCEncrypter(block, []byte(iv)) 29 crypted := make([]byte, len(origData)) 30 mode.CryptBlocks(crypted, origData) 31 return base64.StdEncoding.EncodeToString(crypted) 32 } 33 34 // AesDecryptECB 兼容java的AES解密方式 35 func AesDecryptECB(content string, key string) string { 36 b, _ := base64.StdEncoding.DecodeString(content) 37 cp, _ := aes.NewCipher([]byte(key)) 38 d := make([]byte, len(b)) 39 size := 16 40 for bs, be := 0, size; bs < len(b); bs, be = bs+size, be+size { 41 cp.Decrypt(d[bs:be], b[bs:be]) 42 } 43 return strings.TrimSpace(string(d)) 44 } 45 46 // AesEncryptECB 兼容java的AES加密方式 47 func AesEncryptECB(content string, key string) string { 48 b := padding([]byte(content)) 49 cp, _ := aes.NewCipher([]byte(key)) 50 d := make([]byte, len(b)) 51 size := 16 52 for bs, be := 0, size; bs < len(b); bs, be = bs+size, be+size { 53 cp.Encrypt(d[bs:be], b[bs:be]) 54 } 55 return base64.StdEncoding.EncodeToString(d) 56 } 57 58 func AesEncryptCBC(content string, key string) string { 59 origData := []byte(content) 60 k := []byte(key) 61 // NewCipher该函数限制了输入k的长度必须为16, 24或者32 62 block, _ := aes.NewCipher(k) 63 blockSize := block.BlockSize() // 获取秘钥块的长度 64 origData = pkcs5Padding(origData, blockSize) // 补全码 65 blockMode := cipher.NewCBCEncrypter(block, k[:blockSize]) // 加密模式 66 encrypted := make([]byte, len(origData)) // 创建数组 67 blockMode.CryptBlocks(encrypted, origData) // 加密 68 return hex.EncodeToString(encrypted) 69 } 70 71 func AesDecryptCBC(content string, key string) string { 72 encrypted, _ := hex.DecodeString(content) 73 k := []byte(key) 74 block, _ := aes.NewCipher(k) // 分组秘钥 75 blockSize := block.BlockSize() // 获取秘钥块的长度 76 blockMode := cipher.NewCBCDecrypter(block, k[:blockSize]) // 加密模式 77 decrypted := make([]byte, len(encrypted)) // 创建数组 78 blockMode.CryptBlocks(decrypted, encrypted) // 解密 79 decrypted = pkcs5UnPadding(decrypted) // 去除补全码 80 return string(decrypted) 81 } 82 83 func DESEncryptCBC(content string, key string, iv string) string { 84 block, _ := des.NewCipher([]byte(key)) 85 data := pkcs5Padding([]byte(content), block.BlockSize()) 86 dest := make([]byte, len(data)) 87 blockMode := cipher.NewCBCEncrypter(block, []byte(iv)) 88 blockMode.CryptBlocks(dest, data) 89 return fmt.Sprintf("%x", dest) 90 } 91 92 func DESDecryptCBC(content string, key string, iv string) string { 93 b, _ := hex.DecodeString(content) 94 block, _ := des.NewCipher([]byte(key)) 95 blockMode := cipher.NewCBCDecrypter(block, []byte(iv)) 96 originData := make([]byte, len(b)) 97 blockMode.CryptBlocks(originData, b) 98 origData := pkcs5UnPadding(originData) 99 return string(origData) 100 } 101 102 func DESEncryptECB(content string, key string) string { 103 block, _ := des.NewCipher([]byte(key)) 104 size := block.BlockSize() 105 data := pkcs5Padding([]byte(content), size) 106 if len(data)%size != 0 { 107 return "" 108 } 109 out := make([]byte, len(data)) 110 dst := out 111 for len(data) > 0 { 112 block.Encrypt(dst, data[:size]) 113 data = data[size:] 114 dst = dst[size:] 115 } 116 return fmt.Sprintf("%x", out) 117 } 118 119 func DESDecryptECB(content string, key string) string { 120 b, _ := hex.DecodeString(content) 121 block, _ := des.NewCipher([]byte(key)) 122 size := block.BlockSize() 123 out := make([]byte, len(b)) 124 dst := out 125 for len(b) > 0 { 126 block.Decrypt(dst, b[:size]) 127 b = b[size:] 128 dst = dst[size:] 129 } 130 return string(pkcs5UnPadding(out)) 131 } 132 133 func RC4Encrypt(content string, key string) string { 134 dest := make([]byte, len(content)) 135 cp, _ := rc4.NewCipher([]byte(key)) 136 cp.XORKeyStream(dest, []byte(content)) 137 return fmt.Sprintf("%x", dest) 138 } 139 140 func RC4Decrypt(content string, key string) string { 141 b, _ := hex.DecodeString(content) 142 dest := make([]byte, len(b)) 143 cp, _ := rc4.NewCipher([]byte(key)) 144 cp.XORKeyStream(dest, b) 145 return string(dest) 146 } 147 148 func Base64Encrypt(content string) string { 149 return base64.StdEncoding.EncodeToString([]byte(content)) 150 } 151 152 func Base64Decrypt(content string) string { 153 b, _ := base64.StdEncoding.DecodeString(content) 154 return string(b) 155 } 156 157 func pkcs5Padding(ciphertext []byte, blockSize int) []byte { 158 padding := blockSize - len(ciphertext)%blockSize 159 padtext := bytes.Repeat([]byte{byte(padding)}, padding) 160 return append(ciphertext, padtext...) 161 } 162 func pkcs5UnPadding(origData []byte) []byte { 163 length := len(origData) 164 unpadding := int(origData[length-1]) 165 return origData[:(length - unpadding)] 166 } 167 168 func padding(src []byte) []byte { 169 paddingCount := aes.BlockSize - len(src)%aes.BlockSize 170 if paddingCount == 0 { 171 return src 172 } else { 173 return append(src, bytes.Repeat([]byte{byte(0)}, paddingCount)...) 174 } 175 }