Skip to Content

GO

Encrypt

package main import ( "bytes" "crypto/aes" "crypto/cipher" "encoding/base64" "fmt" ) // aesEncrypt performs AES encryption in CBC mode with Zero Padding. func aesEncrypt(plainText, key, iv []byte) (string, error) { block, err := aes.NewCipher(key) if err != nil { return "", fmt.Errorf("invalid encryption key: %w", err) } // Apply zero padding plainText = zeroPadding(plainText, block.BlockSize()) // Encrypt cipherText := make([]byte, len(plainText)) blockMode := cipher.NewCBCEncrypter(block, iv) blockMode.CryptBlocks(cipherText, plainText) // Encode cipherText to base64 encryptedString := base64.RawURLEncoding.EncodeToString(cipherText) return encryptedString, nil } // zeroPadding pads plainText with zeros to a multiple of blockSize. func zeroPadding(plainText []byte, blockSize int) []byte { padding := blockSize - len(plainText)%blockSize paddingText := bytes.Repeat([]byte{0}, padding) return append(plainText, paddingText...) } func main() { data := `{"data":"SampleData"}` key := []byte("key1234567Sample") iv := []byte("iv12345678Sample") // Encrypt data encryptedData, err := aesEncrypt([]byte(data), key, iv) if err != nil { fmt.Println("Encryption error:", err) return } fmt.Println("Encrypted data:", encryptedData) // Encrypted data: OwWtJinD7DpLsZv-bIAP1F97lOnvwii1ifLyX5G-aeA }

Decrypt

package main import ( "bytes" "crypto/aes" "crypto/cipher" "encoding/base64" "fmt" ) // aesDecrypt performs AES decryption in CBC mode and removes Zero Padding. func aesDecrypt(encryptedText string, key, iv []byte) (string, error) { // Decode base64-encoded encryptedText decodedData, err := base64.RawURLEncoding.DecodeString(encryptedText) if err != nil { return "", fmt.Errorf("invalid encrypted data: %w", err) } // Decrypt block, err := aes.NewCipher(key) if err != nil { return "", fmt.Errorf("invalid decryption key: %w", err) } plainText := make([]byte, len(decodedData)) blockMode := cipher.NewCBCDecrypter(block, iv) blockMode.CryptBlocks(plainText, decodedData) // Remove zero padding plainText = bytes.TrimRight(plainText, string([]byte{0})) return string(plainText), nil } func main() { encryptedData := `OwWtJinD7DpLsZv-bIAP1F97lOnvwii1ifLyX5G-aeA` key := []byte("key1234567Sample") iv := []byte("iv12345678Sample") // Decrypt data decryptedData, err := aesDecrypt(encryptedData, key, iv) if err != nil { fmt.Println("Decryption error:", err) return } fmt.Println("Decrypted data:", decryptedData) // Decrypted data: {"data":"SampleData"} }

Call API

  • Example: Action 47 Get Demo Game Launch URL
  • AESEncrypt: Please refer to the “Encrypt” sample code
package main import ( "fmt" "io" "net/http" "strconv" "strings" "time" ) func main() { // Replace with actual values or environment variables dc := "${DC}" key := "${KEY}" iv := "${IV}" apiURL := "${API_URL}" // Prepare action 47 data timestamp := strconv.Itoa(int(time.Now().UnixMilli())) data := `{"action":47,"ts":` + timestamp + `,"lang":"en","gType":0,"mType":"8001","windowMode":"2"}` // Encrypt data encData, err := aesEncrypt([]byte(data), []byte(key), []byte(iv)) if err != nil { fmt.Println("Encryption error:", err) return } payload := strings.NewReader("dc=" + dc + "&x=" + encData) // Create and send POST request req, err := http.NewRequest("POST", apiURL, payload) if err != nil { fmt.Println("Request creation error:", err) return } req.Header.Add("Content-Type", "application/json") // Execute request res, err := http.DefaultClient.Do(req) if err != nil { fmt.Println("Request execution error:", err) return } defer res.Body.Close() // Read and print response body body, err := io.ReadAll(res.Body) if err != nil { fmt.Println("Error reading response body:", err) return } fmt.Println("Response:", string(body)) }
Last updated on