AES加密
-
api 接口 加密
API接口加密需采用HTTPS协议保障传输安全,结合Token或OAuth实现身份认证,敏感数据可通过AES/RSA等算法加密,防止接口被篡改或数据泄露
-
api接口鉴权 aes
# 使用AES算法进行API接口鉴权,“go,package main,,import (, “crypto/aes”, “crypto/cipher”, “encoding/base64″, “fmt”,),,// AES加密,func AesEncrypt(data, key []byte) (string, error) {, block, err := aes.NewCipher(key), if err != nil {, return “”, err, }, ciphertext := make([]byte, aes.BlockSize+len(data)), iv := ciphertext[:aes.BlockSize], stream := cipher.NewCFBEncrypter(block, iv), stream.XORKeyStream(ciphertext[aes.BlockSize:], data), return base64.StdEncoding.EncodeToString(ciphertext), nil,},,// AES解密,func AesDecrypt(encryptedData string, key []byte) ([]byte, error) {, ciphertext, err := base64.StdEncoding.DecodeString(encryptedData), if err != nil {, return nil, err, }, block, err := aes.NewCipher(key), if err != nil {, return nil, err, }, if len(ciphertext)˂ aes.BlockSize {, return nil, fmt.Errorf(“ciphertext too short”), }, iv := ciphertext[:aes.BlockSize], ciphertext = ciphertext[aes.BlockSize:], stream := cipher.NewCFBDecrypter(block, iv), stream.XORKeyStream(ciphertext, ciphertext), return ciphertext, nil,},,func main() {, key := []byte(“examplekey123456”) // 16 bytes for AES-128, data := []byte(“Hello, World!”),, encrypted, err := AesEncrypt(data, key), if err != nil {, fmt.Println(“Encryption error:”, err), return, }, fmt.Println(“Encrypted:”, encrypted),, decrypted, err := AesDecrypt(encrypted, key), if err != nil {, fmt.Println(“Decryption error:”, err), return, }, fmt.Println(“Decrypted:”, string(decrypted)),},“