Card Encryption

Flow

Encryption Code

package main

import (
	"crypto/aes"
	"crypto/cipher"
	"crypto/rand"
	"encoding/base64"
	"fmt"
	"io"
)

func AesGcmEncryptToBase64String(plaintext, key string) (string, error) {
	block, err := aes.NewCipher([]byte(key))
	if err != nil {
		return "", err
	}

	gcm, err := cipher.NewGCM(block)
	if err != nil {
		return "", err
	}

	nonce := make([]byte, gcm.NonceSize())
	if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
		return "", err
	}

	ciphertext := gcm.Seal(nonce, nonce, []byte(plaintext), nil)
	return base64.StdEncoding.EncodeToString(ciphertext), nil
}

func main() {
	strJSON := `
{
  "clientReferenceId": "1751336752",
  "card": {
    "number": "5123456789012346",
    "expiryMonth": "01",
    "expiryYear": "39",
    "nameOnCard": "Tralalero Tralala"
  },
  "deviceInformations": {
    "type": "",
    "userAgent": "XYXYY",
    "ipAddress": "172.168.10.1",
    "acceptLanguage": "EN",
    "cookieToken": "XOOKXOKXO",
    "deviceId": "uuid",
    "browserWidth": "1234",
    "browserHeight": "1234",
    "country": "ID"
  },
  "metadata": {}
}
`

	secret := "L6VzRddFCwEga3274YDfS2PaQKkVdsUY" // encryptionKey
	encryptedCard, err := AesGcmEncryptToBase64String(strJSON, secret)
	if err != nil {
		fmt.Println("Encryption error:", err)
		return
	}

	fmt.Println("Encrypted base64:", encryptedCard)
}

Last updated