> For the complete documentation index, see [llms.txt](https://java-util.k43.ch/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://java-util.k43.ch/examples/utility-class.md).

# Utility Class

### 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

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

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

### Example Encode/Decode

```java
// 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

```java
// 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

```java
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));
```
