🔨
Java Utility Package
🔨
Java Utility Package
  • Java Utility Package (Open Source)
  • Examples
    • 📖Logging
    • 🔧Utility Class
    • 🔑Password Vault
    • 🖥️HTTP / Socket Server
    • ⚒️HTTP Client
    • 🖥️Socket Client
    • 🗃️JDBC Database
    • ✉️SMTP Mailer
    • ⚔️Java Thread
    • 🗄️File Tools
    • ⌚Timer
    • 💡Tips / FAQ
  • Downloads
    • ⬇️Package ch.k43.util
    • ⬇️Sample Code
    • 📖JavaDoc API
    • 👨‍💻GitHub
Powered by GitBook
  1. Examples

Utility Class

Dozens of handy functions.

K Class Overview

  • Static Methods All methods are static, eliminating the need for object instantiation.

  • Encoding and Decoding Supports encoding and decoding for multiple formats, including URL, Base64, CSV, JSON, XML, and YAML.

  • AES-256 Encryption/Decryption Provides robust encryption and decryption using the AES/CBC/PKCS5Padding algorithm. The provided secure key is hashed with SHA-256 to create a 256-bit key. Use K.getRandomBytes(16) to generate the required initialization vector.

  • Data Compression Enables ZLIB and GZIP compression and decompression for efficient data handling.

  • Hash Generation Supports generating cryptographic hashes using MD5, SHA-2, or SHA-3 algorithms.

  • DNS Querying Allows querying of any DNS record type (e.g., MX, A). MX records are returned in priority order for convenience.

  • Thread Management Includes functionality to introduce delays in thread execution.

  • Environment Information Retrieves detailed environment data, such as JVM version, IP address, hostname, and more.

Example Delay Thread

// Wait 1/4 second
K.waitMilliseconds(250);

// Wait 5 minutes
K.waitMinutes(5);

Example Encode/Decode

// Encode Base-64 string
String httpAuth = K.encodeBase64(userName + ':' + password);

// Encode URL
String urlEncoded = K.encodeURL("https://example.com?q=1&p=done");

Example Query DNS Server

// Get DNS SOA record
String[] soaRRs = K.queryDNS("SOA", "google.com");

// Get DNS MX record(s)
String[] mxRRs = K.queryDNS("MX", "yahoo.com");

Example AES-256

String secretKey = "This is the secret key";
		
// Create a simple text file
KFile.writeFile("This is a simple text file", "AES-Text.txt");
		
// Generate and save random AES-256 initialization vector
KFile.writeFile(K.getRandomBytes(16), "AES.iv");
		
// AES-256 Encrypt
byte[] clearText  = KFile.readByteFile("AES-Text.txt");
byte[] initVector = KFile.readByteFile("AES.iv");
byte[] cipherText = K.encryptAES256(clearText, secretKey.getBytes(), initVector);
KFile.writeFile(cipherText, "AES-Text.encrypted");
		
// AES-256 Decrypt
cipherText = KFile.readByteFile("AES-Text.encrypted");
initVector = KFile.readByteFile("AES.iv");
clearText  = K.decryptAES256(cipherText, secretKey.getBytes(), initVector);
		
System.out.println(new String(clearText));

Last updated 1 month ago

🔧
Page cover image