ApiKey

XoRx
in package

Class XoRx: Provides simple XOR-based encryption and decryption functionalities.

This class offers static methods for encrypting and decrypting strings using a provided key. It employs a bitwise XOR operation and extends the key if it's shorter than the plaintext by repeatedly hashing it.

REF https://www.codecauldron.dev/2021/02/12/simple-xor-encryption-in-php/

Tags
since
0.0.1

Table of Contents

Properties

$debug  : bool
Enables or disables debugging output within the encryption and decryption processes.

Methods

decrypt()  : string
Decrypts a hexadecimal encrypted string using the corresponding key.
encrypt()  : string
Encrypts a given plaintext string using a provided key.
key()  : string
Generates an encryption key that is at least as long as the plaintext.
test()  : void
Performs basic self-tests to verify the encryption and decryption functionality.

Properties

$debug

Enables or disables debugging output within the encryption and decryption processes.

public static bool $debug = false

When set to true, detailed information about each step of the encryption and decryption will be echoed to the output. Defaults to false.

Tags
since
0.0.1

Methods

decrypt()

Decrypts a hexadecimal encrypted string using the corresponding key.

public static decrypt(string $encryptedText, string $key[, string $algo = API_KEY_DEFAULT_ALGO ]) : string

This function takes a hexadecimal encrypted string. It iterates through the string, taking two characters at a time to convert them back to their ASCII representation. It then performs a bitwise XOR operation with the corresponding character in the key (which must be the same key used for encryption). The key is repeated cyclically if it is shorter than the decrypted text.

Parameters
$encryptedText : string

The hexadecimal encrypted string.

$key : string

The decryption key (must match the encryption key).

$algo : string = API_KEY_DEFAULT_ALGO

The hashing algorithm used to extend the key during encryption (default: API_KEY_DEFAULT_ALGO).

Tags
since
0.0.1
Return values
string

The original decrypted plaintext string.

encrypt()

Encrypts a given plaintext string using a provided key.

public static encrypt(string $plainText, string $key[, string $algo = API_KEY_DEFAULT_ALGO ]) : string

This function iterates through each character of the plaintext. For each character, it performs a bitwise XOR operation with the corresponding character in the key. The key is repeated cyclically if it is shorter than the plaintext. The resulting character is then converted to its hexadecimal representation.

Parameters
$plainText : string

The string to be encrypted.

$key : string

The encryption key.

$algo : string = API_KEY_DEFAULT_ALGO

The hashing algorithm used to extend the key (default: API_KEY_DEFAULT_ALGO).

Tags
since
0.0.1
Return values
string

The encrypted string in lowercase hexadecimal format.

key()

Generates an encryption key that is at least as long as the plaintext.

public static key(string $plainText, string $key[, string $algo = API_KEY_DEFAULT_ALGO ]) : string

If the provided key is shorter than the plaintext, this function repeatedly hashes the key using the specified algorithm until its length is sufficient.

Parameters
$plainText : string

The plaintext whose length determines the minimum key length.

$key : string

The initial encryption key.

$algo : string = API_KEY_DEFAULT_ALGO

The hashing algorithm to use for key extension (default: API_KEY_DEFAULT_ALGO).

Tags
since
0.0.1
Return values
string

The generated key, guaranteed to be at least as long as the plaintext.

test()

Performs basic self-tests to verify the encryption and decryption functionality.

public static test([bool $debug = false ]) : void

This method encrypts and then decrypts sample text strings using simple keys. It uses assertions to check if the encryption produces a non-empty and different string from the original, and if the decryption successfully recovers the original text. Optionally, it can output debugging information during the tests.

Parameters
$debug : bool = false

Enables or disables debugging output for the tests (default: false).

Tags
since
0.0.1

        
On this page

Search results