DEV Community

Peter + AI
Peter + AI

Posted on

🔓 Understanding Uniface 10.4 $decode Function: A Complete Guide

This blog post was created with AI assistance to help developers understand Uniface's cryptographic capabilities.

🤔 What is $decode?

The $decode function in Uniface 10.4 is a powerful tool for handling encrypted and encoded data. Think of it as your digital key 🔑 for unlocking three main types of data:

  • Encrypted data - Data that was scrambled using encryption algorithms
  • Encoded data - Data that was converted using encoding schemes like Base64
  • Digital signatures - Verification of message authenticity

📝 Basic Syntax

The function has two main formats:

For Decryption/Decoding:

$decode(Algorithm, Source, Key, Mode, InitializationVector)
Enter fullscreen mode Exit fullscreen mode

For Signature Verification:

$decode(Algorithm, Source, Key, Signature)
Enter fullscreen mode Exit fullscreen mode

🔧 Supported Algorithms

📊 Encoding Algorithms (Simple Data Conversion)

  • BASE64 - Most common web encoding
  • HEX - Hexadecimal encoding
  • URL - URL-safe encoding
  • BASE64URL - URL-safe Base64

🛡️ Block Ciphers (Strong Encryption)

  • AES - Advanced Encryption Standard (recommended)
  • DES/TDES - Older standards (less secure)
  • BLOWFISH/TWOFISH - Alternative encryption methods

🔐 RSA Algorithms (Public Key Cryptography)

RSA algorithms use key pairs (public/private keys). Examples include:

  • RSAES_OAEP_SHA256 - Modern RSA encryption
  • RSASSA_PSS_SHA256 - RSA digital signatures

💡 Practical Examples

Example 1: Simple Base64 Decoding

vDecoded = $decode("BASE64", "SGVsbG8gV29ybGQ=")
; Result: "Hello World"
Enter fullscreen mode Exit fullscreen mode

Example 2: AES Encryption/Decryption

; First encrypt some data
vEncrypted = $encode("AES", "Secret Message", "MySecretKey12345", "CBC", vIV)

; Then decrypt it back
vDecrypted = $decode("AES", vEncrypted, "MySecretKey12345", "CBC", vIV)
vMessage = $encode("USTRING", vDecrypted) ; Convert to string
Enter fullscreen mode Exit fullscreen mode

Example 3: Digital Signature Verification

vIsValid = $decode("RSASSA_PSS_SHA256", vMessage, vPublicKey, vSignature)
; Returns 1 (true) if signature is valid, 0 (false) if not
Enter fullscreen mode Exit fullscreen mode

⚠️ Common Errors and Solutions

Error Code Problem Solution
-1780 Algorithm not found Check algorithm spelling 📝
-1786 Invalid key length Use correct key size (AES: 16/24/32 bytes) 🔑
-1787 Invalid HEX format Ensure data contains only 0-9, A-F characters 🔤
-1788 Invalid BASE64 format Check for proper Base64 padding (=) 📋

🎯 Best Practices

  • Always check $procerror after calling $decode 🔍
  • Use AES over DES for new applications 🛡️
  • Store keys securely - never hardcode them 🚫
  • Use proper IV (Initialization Vector) for CBC mode 🎲
  • Convert raw data to string when needed using USTRING 🔄

🔄 Working with Raw Data

Important: $decode returns data in Uniface's raw data type. This can contain null bytes (0x00) that would break regular strings. To convert raw data back to a readable string:

vRawData = $decode("BASE64", vEncodedData)
vStringData = $encode("USTRING", vRawData) ; Convert to string
Enter fullscreen mode Exit fullscreen mode

🚀 Real-World Use Cases

  • API Integration - Decoding Base64 responses from web services 🌐
  • File Processing - Decrypting uploaded files 📁
  • Database Security - Decrypting sensitive data fields 🗄️
  • Authentication - Verifying JWT token signatures 🎫

🎉 Conclusion

The $decode function is a versatile tool in Uniface 10.4 that handles various cryptographic operations. Whether you're working with simple Base64 data or complex RSA encryption, understanding this function opens up many possibilities for secure application development. Remember to always validate your inputs and handle errors properly! 💪

Happy coding! 👨‍💻👩‍💻

Top comments (0)