I Want Text Encryption In PHP.
Should I do?
Example:
Decrypt Text >
I Am Ali
Encryption >
Without Space:
5x7s6c7s2x9m
With Space:
5x 6c 2x9m
I Want Text Encryption In PHP.
Should I do?
Example:
Decrypt Text >
I Am Ali
Encryption >
Without Space:
5x7s6c7s2x9m
With Space:
5x 6c 2x9m
as an author you can’t use Encryption method for your item coding. All code should be readable and editable by the customer.
Encryption is hard, and easy to mess up. Please be careful. You will want to use a dedicated security library such as:
Here’s a class I wrote many years ago for simple password-based encryption (with phpseclib 3.0):
use Exception;
use phpseclib3\Crypt\AES;
use phpseclib3\Crypt\Random;
/**
* Lightweight and fast symmetric encryption with AES.
*/
class FastEncrypt {
/**
* @param string $data The text to encrypt.
* @param string $key The password to use (any length).
*/
public static function encrypt($data, $key) {
$iv = self::generateIV();
$cipher = self::getCipher($iv, $key);
$data = $cipher->encrypt($data);
return $iv . $data;
}
/**
* @param string $encrypted The encrypted data to decrypt.
* @param string $key The password to use (any length).
*/
public static function decrypt($encrypted, $key) {
list($iv, $data) = self::extractIV($encrypted);
$cipher = self::getCipher($iv, $key);
return $cipher->decrypt($data);
}
/**
* Generates and returns the cipher AES object with the key and IV loaded.
*
* @return AES
*/
private static function getCipher($iv, $key) {
$cipher = new AES('cbc');
$cipher->setKeyLength(256);
$cipher->setKey(hex2bin(hash('sha256', $key))); // coerce to 256 bits
$cipher->setIV($iv);
return $cipher;
}
/**
* Generates a random IV for encryption.
*
* @param $length
* @return string Encryption IV containing $length number of characters.
*/
private static function generateIV($length = 16) {
return Random::string($length);
}
/**
* Extracts the data and IV from a cipher.
*
* @param string $data
* @param int $length
* @return string[]
*/
private static function extractIV($data, $length = 16) {
if (strlen($data) <= $length) {
throw new Exception('Encrypted data is too short and may be lacking an initialization vector');
}
$iv = substr($data, 0, $length);
$data = substr($data, $length);
return [$iv, $data];
}
}
Example usage:
$password = '123456';
$encrypted = base64_encode(FastEncrypt::encrypt('Hello world!', $password));
$decrypted = FastEncrypt::decrypt(base64_decode($encrypted), $password);
var_dump($encrypted);
var_dump($decrypted);
Result:
string(44) "FApU+JW9PhbyZwZx2MuyySn8kp1uQ9qJSDWEx/kE9ZA="
string(12) "Hello world!"
It looks like the website you linked has the same strategy, except they use their own identical key each time.
I will create my own API but I am confused where should I save data? Should I use mysqli?
And I want upload in codecanyon.
@baileyherbert
Save data however you want Some apps use mysqli, some use pdo, some even use json files (which is not good!). It’s up to you, you’re the engineer here!
Because I want create my own API in PHP, what should I do?
You should go look at other APIs on the market to see what they do and make a decision on your own based on your findings and your experience. Research is the foundation of a good product after all!
I decided to go with mysqli, will it be approved by codecanyon?
I can’t say if your product will be approved, because CodeCanyon only approves the best quality scripts out there and is rather strict these days
But mysqli
itself won’t cause you to be rejected.