Certainly! Below is the JavaScript code for a custom encryption class as per your specifications. This class, Encryption, contains two static methods encrypt and decrypt. The encryption method only uses the specified special characters and follows a simple XOR cipher mechanism for encryption and decryption.
class Encryption {
// Allowed characters for the encryption output
static allowedChars = [
"+", "-", "*", "/", "=", "(", ")", "{", "}", "[", "]", ";", ":", "!", ",", ".", "'", "\"", "`", "|", "@", "&", "~", "#"
];
// Mapping for 4-bit binary values to allowed characters
static charMapping = Encryption.allowedChars.slice(0, 16);
static encrypt(encryptionKey, string) {
const encoder = new TextEncoder();
// Convert the input string to a bit string
const stringBytes = encoder.encode(string);
let bitString = '';
for (const byte of stringBytes) {
bitString += byte.toString(2).padStart(8, '0');
}
// Convert the encryption key to a bit string
const keyBytes = encoder.encode(encryptionKey);
let keyBitString = '';
for (const byte of keyBytes) {
keyBitString += byte.toString(2).padStart(8, '0');
}
// Extend the key bit string to match the length of the input bit string
let extendedKeyBitString = keyBitString.repeat(Math.ceil(bitString.length / keyBitString.length)).slice(0, bitString.length);
// XOR the input bit string with the key bit string
let encryptedBitString = '';
for (let i = 0; i < bitString.length; i++) {
encryptedBitString += bitString[i] ^ extendedKeyBitString[i];
}
// Map the encrypted bit string to the allowed characters
let encryptedString = '';
for (let i = 0; i < encryptedBitString.length; i += 4) {
const chunk = encryptedBitString.substr(i, 4);
const index = parseInt(chunk, 2);
encryptedString += Encryption.charMapping[index];
}
return encryptedString;
}
static decrypt(encryptionKey, encryptedString) {
const encoder = new TextEncoder();
// Convert the encrypted string back to the bit string
let encryptedBitString = '';
for (const char of encryptedString) {
const index = Encryption.charMapping.indexOf(char);
if (index === -1) {
throw new Error('Invalid character in encrypted string');
}
encryptedBitString += index.toString(2).padStart(4, '0');
}
// Convert the encryption key to a bit string
const keyBytes = encoder.encode(encryptionKey);
let keyBitString = '';
for (const byte of keyBytes) {
keyBitString += byte.toString(2).padStart(8, '0');
}
// Extend the key bit string to match the length of the encrypted bit string
let extendedKeyBitString = keyBitString.repeat(Math.ceil(encryptedBitString.length / keyBitString.length)).slice(0, encryptedBitString.length);
// XOR the encrypted bit string with the key bit string
let bitString = '';
for (let i = 0; i < encryptedBitString.length; i++) {
bitString += encryptedBitString[i] ^ extendedKeyBitString[i];
}
// Convert the bit string back to the original string
let bytes = [];
for (let i = 0; i < bitString.length; i += 8) {
const byte = parseInt(bitString.substr(i, 8), 2);
bytes.push(byte);
}
const decoder = new TextDecoder();
return decoder.decode(new Uint8Array(bytes));
}
}
Usage Example:
const encryptionKey = 'mysecretkey';
const originalString = 'Hello, World!';
// Encrypt the string
const encryptedString = Encryption.encrypt(encryptionKey, originalString);
console.log('Encrypted String:', encryptedString);
// Decrypt the string
const decryptedString = Encryption.decrypt(encryptionKey, encryptedString);
console.log('Decrypted String:', decryptedString);
Explanation:
-
The encrypt method:
- Converts the input string and the encryption key into their respective bit strings.
- Extends the key bit string to match the length of the input bit string.
- Performs an XOR operation between the input bit string and the key bit string.
- Divides the result into 4-bit chunks and maps each chunk to an allowed character.
-
The decrypt method:
- Converts the encrypted string back into the encrypted bit string using the reverse mapping.
- Extends the key bit string to match the length of the encrypted bit string.
- Performs an XOR operation between the encrypted bit string and the key bit string.
- Converts the resulting bit string back into the original string.
Note:
- The encryption method ensures that only the specified special characters are used in the encrypted output.
- This method handles any UTF-8 encoded strings and works with non-ASCII characters.
- The encryption uses a simple XOR cipher, which is not secure for real-world applications, but meets the requirements specified.