UMIVERSE SDK
  • UMIVERSE SDK Doc
    • Overview
    • Configure Secret Key and API Domain
    • Sync Account
      • OAuth login
        • Preparations for OAuth
        • Use Authorization_Code to get UMI_UID
        • Decrypt Authorization Code
          • AES Decrypt
    • Dive Points
      • Increase DivePoint
        • API
      • Mission System
        • Request task panel view in H5 game
    • Game Recharge
      • Initiate Recharge Request
      • Handle Recharge Completion
  • Signature
    • Typescript
    • PHP
    • Java
    • C++
  • SDK DEMO
  • Legacy
    • API Key/Secret
    • SignUp & SignIn
    • Get UMI UID
    • Authentication
    • Umiverse Features
      • Mint
      • Send a server-generated item to Umi account
      • Get all items that the player can put up for sale
      • Send an item to a player
      • Deduct an item from a player
    • Redirect
  • Developer Console
    • Add Game Information
Powered by GitBook
On this page
  1. UMIVERSE SDK Doc
  2. Sync Account
  3. OAuth login
  4. Decrypt Authorization Code

AES Decrypt

The AES (Advanced Encryption Standard) decryption algorithm works as follows:

  1. Ciphertext Input: The ciphertext (encrypted data) is provided, along with the secret key and initialization vector (IV).

  2. Key Expansion: The secret key is used to generate a series of round keys, which are used in the decryption process.

  3. Inverse Cipher Operations: AES uses a sequence of operations to reverse the encryption steps, which include:

    • Inverse SubBytes: Each byte in the ciphertext is replaced with its inverse value from a predefined lookup table (S-box).

    • Inverse ShiftRows: The rows of the state (a matrix representation of the data) are shifted back to their original positions.

    • Inverse MixColumns: The columns of the state are transformed using a matrix inverse operation to restore the original data.

    • AddRoundKey: A round key derived from the secret key is XOR-ed with the state to undo its effect.

  4. Repetition of Rounds: The decryption process repeats these steps for a set number of rounds (depending on the key size: 128, 192, or 256 bits).

  5. Final Round: The final round excludes the Inverse MixColumns operation, and the result is the original plaintext.

In this decryption code, we:

  • Extract the IV and ciphertext from the input.

  • Apply AES decryption using the secret key and the IV.

  • The result is the decrypted plaintext, which can then be converted back to a human-readable format (UTF-8).

AES is a symmetric encryption algorithm, meaning the same key is used for both encryption and decryption.

// Some code
```typescriptreact
import CryptoJS from 'crypto-js';

// Encrypt using CryptoJS with AES
export function encrypt(text: string, secretKey: string) {
  const iv = CryptoJS.lib.WordArray.random(16); // Generate random IV
  const encrypted = CryptoJS.AES.encrypt(text, CryptoJS.enc.Hex.parse(secretKey), {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  });

  // Return the iv and the encrypted data, separated by a colon
  return iv.toString(CryptoJS.enc.Hex) + ':' + encrypted.ciphertext.toString(CryptoJS.enc.Hex);
}

// Decrypt using CryptoJS with AES
export function decrypt(encryptedData: string, secretKey: string) {
  const [ivHex, encryptedText] = encryptedData.split(':');
  const iv = CryptoJS.enc.Hex.parse(ivHex);
  const encryptedBytes = CryptoJS.enc.Hex.parse(encryptedText);

  const decrypted = CryptoJS.AES.decrypt(
    { ciphertext: encryptedBytes } as any, // Decrypt using the ciphertext
    CryptoJS.enc.Hex.parse(secretKey),
    {
      iv: iv,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7
    }
  );

  // Return the decrypted text
  return decrypted.toString(CryptoJS.enc.Utf8);
}

// Create a 32-byte random key and return it in Hex format
export function createSecretKey() {
  const secretKey = CryptoJS.lib.WordArray.random(32); // AES-256 requires a 32-byte key
  const secretKeyHex = secretKey.toString(CryptoJS.enc.Hex);
  return secretKeyHex;
}

// Restore the key from Hex format to WordArray
export function restoreSecretKey(secretKeyHex: string) {
  const secretKey = CryptoJS.enc.Hex.parse(secretKeyHex);
  return secretKey;
}

```
PreviousDecrypt Authorization CodeNextDive Points

Last updated 7 months ago