This repository contains implementations of a simple string encryption and decryption utility across various programming languages. The goal is to provide interoperable encryption, allowing a string encrypted in one language to be decrypted in another, using a consistent scheme.
The encryption scheme employed is as follows:
- Salt Generation: A 16-byte random salt is generated.
- Hex Encoding Salt: The 16 random salt bytes are hex-encoded to produce a 32-character string (
salt_hex). - Combine Salt and Plaintext: The
salt_hexstring is prepended to the original plaintext string:combined_string = salt_hex + original_plaintext. - UTF-8 Encode: The
combined_stringis converted to a byte array using UTF-8 encoding:combined_bytes = combined_string.getBytes("UTF-8"). - XOR Encryption: The
combined_bytesare XORed byte-by-byte with a static key:"test_key". The key (also UTF-8 encoded) is repeated cyclically if shorter than the data. - Base64 Encoding: The resulting XORed byte array is Base64 encoded to produce the final encrypted string.
Decryption reverses this process:
- Base64 Decode the encrypted string to get XORed bytes.
- XOR Decrypt these bytes with the static key (UTF-8 encoded) to get
decrypted_combined_bytes. - Convert
decrypted_combined_bytesback to a string using UTF-8 encoding:decrypted_combined_string. - Extract the first 32 characters from
decrypted_combined_stringas theretrieved_salt_hex. - The remainder of
decrypted_combined_stringis the original plaintext.
Below is a list of implemented languages, their respective files, and basic instructions for running them. All implementations aim to provide a processString('e', "plaintext") function for encryption and processString('d', "encrypted_text") for decryption, or an equivalent.
- File:
StringEncryption.php - Usage: Contains a
StringEncryptionclass with aprocessString($type, $data)method. - To Run: Include the file and call the methods. Requires a PHP environment.
// <?php // require_once 'StringEncryption.php'; // $crypter = new StringEncryption(); // $encrypted = $crypter->processString('e', "Hello PHP"); // echo "Encrypted: " . $encrypted . "\n"; // $decrypted = $crypter->processString('d', $encrypted); // echo "Decrypted: " . $decrypted . "\n"; // ?>
- File:
StringCrypter.java - Usage: Contains a
StringCrypterclass with a staticprocessString(char type, String data)method and amainmethod for tests. - To Run:
javac StringCrypter.java java StringCrypter
- File:
string_crypter.py - Usage: Contains a
StringCrypterclass and aprocess_string(type_char, data_string)function, with example usage inif __name__ == "__main__":. - To Run:
python string_crypter.py
- File:
StringCrypter.cs - Usage: Contains a
StringCrypterclass withProcessString(char type, string data)and aMainmethod for tests. - To Run: Requires .NET SDK.
(Note: Compilation and execution were not fully tested due to potential environment constraints during development.)
# Assuming StringCrypter.cs is in a project or compiled directly # dotnet run (if part of a project) # or csc StringCrypter.cs StringCrypter.exe
- Files:
StringCrypter.h(Header-only library)TestStringCrypter.cpp(Example usage)
- Usage: Include
StringCrypter.hand use theStringCrypter::processString(char type, const std::string& data)function. - To Compile & Run Example (e.g., with g++):
g++ TestStringCrypter.cpp -o test_cpp -std=c++11 ./test_cpp
- Files:
string_crypter.h(Header file)string_crypter.c(Implementation)test_string_crypter_c.c(Example usage)
- Usage: Link against
string_crypter.cand useprocess_string_c(char type, const char* data, char** result). Caller mustfree(*result). - To Compile & Run Example (e.g., with gcc):
gcc test_string_crypter_c.c string_crypter.c -o test_c ./test_c
- Files:
stringCrypter.js(Core logic, supports Node.jsrequireand browser global)testJs.html(Browser test page)
- Usage:
- Node.js:
const sc = require('./stringCrypter.js'); sc.processString('e', "data"); - Browser: Include
stringCrypter.jsin a script tag.processStringwill be available globally.
- Node.js:
- To Run (Node.js example):
To Run (Browser): Open
node -e "const sc = require('./stringCrypter.js'); console.log(sc.processString('e', 'Hello JS'));"testJs.htmlin a web browser and check the console.
- File:
stringcrypter.go - Usage: Contains
ProcessStringGo(operationType rune, data string)and amainfunction for tests. - To Run:
go run stringcrypter.go
- File:
string_crypter.rs - Usage: Contains
process_string_rust(operation_type: char, data: &str)and amainfunction for tests. - Dependencies (add to
Cargo.toml):[dependencies] base64 = "0.21" # Or latest compatible rand = "0.8" # Or latest compatible hex = "0.4" # Or latest compatible
- To Run (within a Cargo project):
# Example: cargo new rust_crypto_test && cd rust_crypto_test # Replace src/main.rs with string_crypter.rs content # Add dependencies to Cargo.toml cargo run
- File:
string_crypter.lua - Usage: Returns a module table. Use
M.process_string_lua(type, data). Contains test examples. - Requirements: Lua 5.3+ recommended for
bit32library. For older versions, an external bitwise operations library might be needed. - To Run:
lua string_crypter.lua
- File:
StringCrypter.kt - Usage: Contains
processStringKt(type: Char, data: String)and amainfunction for tests. - To Run: Requires Kotlin compiler.
kotlinc StringCrypter.kt -include-runtime -d StringCrypterKt.jar java -jar StringCrypterKt.jar
- File:
string_crypter.dart - Usage: Contains
processStringDart(String type, String data)and amainfunction for tests.- Dart CLI: Run directly.
- Flutter: Copy
string_crypter.dartinto your Flutter project'slibdirectory and import it.
- To Run (Dart CLI):
dart string_crypter.dart
- File:
StringCrypter.swift - Usage: Contains
processStringSwift(type: Character, data: String)and arunSwiftTests()function. - To Run: Requires Swift compiler.
(Or run in an Xcode Playground.)
swift StringCrypter.swift
- File:
string_crypter.rb - Usage: Contains
process_string_rb(type, data)and test code withinif __FILE__ == $0. - To Run:
ruby string_crypter.rb
- File:
string_crypter.pl - Usage: Contains
process_string_pl($type, $data)and test code. - Requirements:
MIME::Base64andEncodemodules (Encode is usually core). - To Run:
(Install modules if needed:
perl string_crypter.pl
cpan MIME::Base64) - Interoperability Note: The current Perl implementation (
string_crypter.pl) has a deviation from the common scheme. It prepends the raw 16 salt bytes to the UTF-8 encoded plaintext before XORing. For full interoperability, the Perl script should be modified to prepend the 32-character hex string representation of the salt to the plaintext string before the combined string is UTF-8 encoded (i.e.,encode_utf8(salt_hex_string . plaintext)should be the data fed to XOR).
- File:
StringCrypter.scala - Usage: An object
StringCrypterwithprocessStringScala(operationType: Char, data: String)and amainmethod for tests. - To Run: Requires Scala compiler.
scalac StringCrypter.scala scala StringCrypter
Each implementation includes placeholder tests for decrypting a string encrypted by another language (often Python as an example). To properly test interoperability:
- Encrypt a known string (e.g., "Hello from [SourceLanguage] for [TargetLanguage]!") using the source language script.
- Copy the resulting Base64 encoded string.
- Replace the placeholder
*_encryptedvariable in the target language's test section with this string. - Run the target language's test script to verify successful decryption.
Similarly, each script generates an encrypted string that can be used to test decryption in other languages.
Feel free to add implementations in other languages or improve existing ones! Please ensure the core encryption logic remains consistent for interoperability. If you encounter issues or have suggestions, please open an issue or a pull request.