DEV Community

Samcorp
Samcorp

Posted on

Real-Time Encryption of Bets and Results

Objective:
Ensure that bet data and game results can't be tampered with in transmission or storage.

Technology Stack:
Backend Language: Node.js / Java / Python / Golang

Encryption: AES, RSA, SHA-256

Database: PostgreSQL / MongoDB with encrypted fields

*Example *(Node.js with AES):

const crypto = require('crypto');
const algorithm = 'aes-256-cbc';
const key = crypto.randomBytes(32); // secret key
const iv = crypto.randomBytes(16);  // initialization vector

function encrypt(data) {
  const cipher = crypto.createCipheriv(algorithm, key, iv);
  let encrypted = cipher.update(JSON.stringify(data), 'utf-8', 'hex');
  encrypted += cipher.final('hex');
  return { encryptedData: encrypted, iv: iv.toString('hex') };
}

function decrypt(encryptedData, iv) {
  const decipher = crypto.createDecipheriv(algorithm, key, Buffer.from(iv, 'hex'));
  let decrypted = decipher.update(encryptedData, 'hex', 'utf-8');
  decrypted += decipher.final('utf-8');
  return JSON.parse(decrypted);
}

Enter fullscreen mode Exit fullscreen mode

2. Device Fingerprinting & Anomaly Detection

Objective:
Track user devices and behavior to detect bots, duplicate accounts, or risky login patterns.

Technology Stack:
Frontend: JavaScript (React/Angular/Vue)

Backend: Python (Flask/FastAPI) or Node.js

Libraries: FingerprintJS, DeviceDetector, UA-parser, GeoIP2

ML Models (Optional): Scikit-learn or TensorFlow (Python)

*Example *(JavaScript + Node.js):
Client (browser)

<script src="https://openfpcdn.io/fingerprintjs/v3"></script>
<script>
FingerprintJS.load().then(fp => {
  fp.get().then(result => {
    fetch('/api/track-device', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        visitorId: result.visitorId,
        browser: result.components.userAgent.value,
        screen: window.screen.width + 'x' + window.screen.height
      })
    });
  });
});
</script>
Enter fullscreen mode Exit fullscreen mode

Backend (Node.js):

app.post('/api/track-device', async (req, res) => {
  const { visitorId, browser, screen } = req.body;
  const previousDevices = await db.findDevicesByUser(req.user.id);

  if (!previousDevices.includes(visitorId)) {
    // alert risk engine
    logSuspiciousActivity(req.user.id, visitorId);
  }

  res.sendStatus(200);
});

Enter fullscreen mode Exit fullscreen mode

3.2FA, CAPTCHA, and Geo-Fencing
Objective:
Add extra layers of access control and compliance.

Tech:
2FA: TOTP via Google Authenticator

CAPTCHA: Google reCAPTCHA v3

Geo-Fencing: IP-to-country + Rules Engine

Example 1 – Google reCAPTCHA (Frontend):

<form id="login-form">
  <input name="email">
  <input name="password">
  <div class="g-recaptcha" data-sitekey="your-site-key"></div>
  <button>Login</button>
</form>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
Enter fullscreen mode Exit fullscreen mode

Example 2 – TOTP (Node.js using speakeasy):

const speakeasy = require('speakeasy');
const secret = speakeasy.generateSecret({ name: "MyCasinoApp" });

// Send QR to user
console.log(secret.otpauth_url);

// Verify
const isVerified = speakeasy.totp.verify({
  secret: secret.base32,
  encoding: 'base32',
  token: userInputCode
});
Enter fullscreen mode Exit fullscreen mode

Example 3 – Geo-Fencing with IP:

import geoip2.database

reader = geoip2.database.Reader('/GeoLite2-Country.mmdb')
response = reader.country('103.31.144.0')
country = response.country.iso_code

if country not in ["UK", "MT", "GI", "IN"]:
    raise PermissionError("Access denied from restricted jurisdiction")

Enter fullscreen mode Exit fullscreen mode

End-to-End Platform Security
From encrypted game data and fingerprinting to real-time geofencing and multi-factor authentication, securing your platform is non-negotiable. For a complete overview of setting up a secure and fully compliant casino environment, refer to this step-by-step guide to launching an online game.

Top comments (0)