目录
一、可解密加解密算法
1、aes 加解密算法
2、旧3des加解密方法
3、新3des加解密方法
4、rsa公私钥加解密、签名验签方法
5、自定义加密算法1
6、自定义加密算法2
7、自定义加密算法3
二、不可解密加密算法
1、md5算法
2、crypt算法
3、sha1算法
5、hash 算法文章来源:https://www.toymoban.com/news/detail-700823.html
6、 password_hash算法文章来源地址https://www.toymoban.com/news/detail-700823.html
一、可解密加解密算法
1、aes 加解密算法
class AES {
private $key;
private $iv;
public function __construct($key, $iv) {
$this->key = $key;
$this->iv = $iv;
}
public function encrypt($data) {
$encrypted = openssl_encrypt($data, 'AES-128-ECB', $this->key, OPENSSL_RAW_DATA);
return base64_encode($encrypted);
}
public function decrypt($encryptedData) {
$decrypted = openssl_decrypt(base64_decode($encryptedData), 'AES-128-ECB', $this->key, OPENSSL_RAW_DATA);
return $decrypted;
}
public function encryptCBC($data) {
$encrypted = openssl_encrypt($data, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA, $this->iv);
return base64_encode($encrypted);
}
public function decryptCBC($encryptedData) {
$decrypted = openssl_decrypt(base64_decode($encryptedData), 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA, $this->iv);
return $decrypted;
}
}
// 使用示例:
$key = '0123456789abcdef'; // 16字节长度的密钥
$iv = 'fedcba9876543210'; // 16字节长度的初始向量
$aes = new AES($key, $iv);
$data = 'Hello, World!';
$encryptedECB = $aes->encrypt($data);
echo 'AES-128-ECB加密后的数据:' . $encryptedECB . "\n";
$decryptedECB = $aes->decrypt($encryptedECB);
echo 'AES-128-ECB解密后的数据:' . $decryptedECB . "\n";
$encryptedCBC = $aes->encryptCBC($data);
echo 'AES-256-CBC加密后的数据:' . $encryptedCBC . "\n";
$decryptedCBC = $aes->decryptCBC($encryptedCBC);
echo 'AES-256-CBC解密后的数据:' . $decryptedCBC . "\n";
2、旧3des加解密方法
<?php
class Encrypt_3DES
{
//加密秘钥,
private $_key;
private $_iv;
public function __construct($key, $iv)
{
$this->_key = $key;
$this->_iv = $iv;
}
/**
* 对字符串进行3DES加密
* @param string 要加密的字符串
* @return mixed 加密成功返回加密后的字符串,否则返回false
*/
public function encrypt3DES($str)
{
$td = mcrypt_module_open(MCRYPT_3DES, "", MCRYPT_MODE_CBC, "");
if ($td === false) {
return false;
}
//检查加密key,iv的长度是否符合算法要求
$key = $this->fixLen($this->_key, mcrypt_enc_get_key_size($td));
$iv = $this->fixLen($this->_iv, mcrypt_enc_get_iv_size($td));
if (mcrypt_generic_init($td, $key, $iv) !== 0) {
return false;
}
$result = mcrypt_generic($td, $str);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return base64_encode($result); // 对加密后的结果进行Base64编码
}
/**
* 对加密的字符串进行3DES解密
* @param string 要解密的字符串
* @return mixed 解密成功返回解密后的字符串,否则返回false
*/
public function decrypt3DES($str)
{
$td = mcrypt_module_open(MCRYPT_3DES, "", MCRYPT_MODE_CBC, "");
if ($td === false) {
return false;
}
//检查加密key,iv的长度是否符合算法要求
$key = $this->fixLen($this->_key, mcrypt_enc_get_key_size($td));
$iv = $this->fixLen($this->_iv, mcrypt_enc_get_iv_size($td));
if (mcrypt_generic_init($td, $key, $iv) !== 0) {
return false;
}
$str = base64_decode($str); // 对加密字符串进行Base64解码
$result = mdecrypt_generic($td, $str);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $this->strUnPad($result);
}
/**
* 返回适合算法长度的key,iv字符串
* @param string $str key或iv的值
* @param int $td_len 符合条件的key或iv长度
* @return string 返回处理后的key或iv值
*/
private function fixLen($str, $td_len)
{
$str_len = strlen($str);
if ($str_len > $td_len) {
return substr($str, 0, $td_len);
} else if ($str_len < $td_len) {
return str_pad($str, $td_len, "\0");
}
return $str;
}
/**
* 返回适合算法的分组大小的字符串长度,末尾使用\0补齐
* @param string $str 要加密的字符串
* @param int $td_group_len 符合算法的分组长度
* @return string 返回处理后字符串
*/
private function strPad($str, $td_group_len)
{
$padding_len = $td_group_len - (strlen($str) % $td_group_len);
return str_pad($str, strlen($str) + $padding_len, "\0");
}
/**
* 返回适合算法的分组大小的字符串长度,去除末尾的\0
* @param string $str 要解密的字符串
* @return string 返回处理后字符串
*/
private function strUnPad($str)
{
return rtrim($str, "\0");
}
}
$key = '1a2bc@';
$iv = '12345678'; // 将iv改为字符串形式
$str = "abcd123";
$encrypt = new Encrypt_3DES($key, $iv);
$jiaData = $encrypt->encrypt3DES($str);
$jieData = $encrypt->decrypt3DES($jiaData);
echo "未加密字符串:".$str;
echo "\n";
echo "加密字符串:".$jiaData;
echo "\n";
echo "解密密字符串:".$jieData;
3、新3des加解密方法
<?php
class TripleDes {
private $key;
private $iv;
public function __construct($key, $iv) {
$this->key = $key;
$this->iv = $iv;
}
public function encrypt($data) {
$encrypted = openssl_encrypt($data, 'des-ede3-cbc', $this->key, OPENSSL_RAW_DATA, $this->iv);
return base64_encode($encrypted);
}
public function decrypt($encryptedData) {
$decrypted = openssl_decrypt(base64_decode($encryptedData), 'des-ede3-cbc', $this->key, OPENSSL_RAW_DATA, $this->iv);
return $decrypted;
}
}
$key = "2312342132";
$iv = "12345678"; // 初始化向量长度必须为8位
$des = new TripleDes($key, $iv);
$data = "adsadb123";
echo "要加密的数据:{$data}\n";
$encrypted = $des->encrypt($data);
echo "加密后的数据:" . $encrypted . "\n";
$decrypted = $des->decrypt($encrypted);
echo "解密后的数据:" . $decrypted . "\n";
4、rsa公私钥加解密、签名验签方法
<?php
/**
* RSA签名类
*/
class Rsa
{
public $publicKey = '';
public $privateKey = '';
private $_privKey;
/**
* * private key
*/
private $_pubKey;
/**
* * public key
*/
private $_keyPath;
/**
* * the keys saving path
*/
/**
* * the construtor,the param $path is the keys saving path
* @param string $publicKey 公钥
* @param string $privateKey 私钥
*/
public function __construct($publicKey = null, $privateKey = null)
{
$this->setKey($publicKey, $privateKey);
}
/**
* 设置公钥和私钥
* @param string $publicKey 公钥
* @param string $privateKey 私钥
*/
public function setKey($publicKey = null, $privateKey = null)
{
if (!is_null($publicKey)) {
$this->publicKey = $publicKey;
}
if (!is_null($privateKey)) {
$this->privateKey = $privateKey;
}
}
/**
* * setup the private key
*/
private function setupPrivKey()
{
if (is_resource($this->_privKey)) {
return true;
}
$pem = chunk_split($this->privateKey, 64, "\n");
$pem = "-----BEGIN PRIVATE KEY-----\n" . $pem . "-----END PRIVATE KEY-----\n";
$this->_privKey = openssl_pkey_get_private($pem);
return true;
}
/**
* * setup the public key
*/
private function setupPubKey()
{
if (is_resource($this->_pubKey)) {
return true;
}
$pem = chunk_split($this->publicKey, 64, "\n");
$pem = "-----BEGIN PUBLIC KEY-----\n" . $pem . "-----END PUBLIC KEY-----\n";
$this->_pubKey = openssl_pkey_get_public($pem);
return true;
}
/**
* * encrypt with the private key
*/
public function privEncrypt($data)
{
if (!is_string($data)) {
return null;
}
$this->setupPrivKey();
$r = openssl_private_encrypt($data, $encrypted, $this->_privKey);
if ($r) {
return base64_encode($encrypted);
}
return null;
}
/**
* * decrypt with the private key
*/
public function privDecrypt($encrypted)
{
if (!is_string($encrypted)) {
return null;
}
$this->setupPrivKey();
$encrypted = base64_decode($encrypted);
$r = openssl_private_decrypt($encrypted, $decrypted, $this->_privKey);
if ($r) {
return $decrypted;
}
return null;
}
/**
* * encrypt with public key
*/
public function pubEncrypt($data)
{
if (!is_string($data)) {
return null;
}
$this->setupPubKey();
$r = openssl_public_encrypt($data, $encrypted, $this->_pubKey);
if ($r) {
return base64_encode($encrypted);
}
return null;
}
/**
* * decrypt with the public key
*/
public function pubDecrypt($crypted)
{
if (!is_string($crypted)) {
return null;
}
$this->setupPubKey();
$crypted = base64_decode($crypted);
$r = openssl_public_decrypt($crypted, $decrypted, $this->_pubKey);
if ($r) {
return $decrypted;
}
return null;
}
/**
* 构造签名
* @param string $dataString 被签名数据
* @return string
*/
public function sign($dataString)
{
$this->setupPrivKey();
$signature = false;
openssl_sign($dataString, $signature, $this->_privKey);
return base64_encode($signature);
}
/**
* 验证签名
* @param string $dataString 被签名数据
* @param string $signString 已经签名的字符串
* @return number 1签名正确 0签名错误
*/
public function verify($dataString, $signString)
{
$this->setupPubKey();
$signature = base64_decode($signString);
$flg = openssl_verify($dataString, $signature, $this->_pubKey);
return $flg;
}
public function __destruct()
{
is_resource($this->_privKey) && @openssl_free_key($this->_privKey);
is_resource($this->_pubKey) && @openssl_free_key($this->_pubKey);
}
}
$publicKey = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKZ1mKTymRoGKnHiP1xAy4aiyt5r0BscCZnDAonCrMFZ4kBGriPNHxEaLr5lfBnMKw7k6i+2dsFPSEZooTvqtPUCAwEAAQ==';
$privateKey = 'MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEApnWYpPKZGgYqceI/XEDLhqLK3mvQGxwJmcMCicKswVniQEauI80fERouvmV8GcwrDuTqL7Z2wU9IRmihO+q09QIDAQABAkBunx3nGHXYjppsfn++7iyTd+I7+Agfy/0xWyB3rpEiGGgfemjcRFaeq5SC2vUNXsrEOY5gbUSQmFxH//Cym18NAiEA1z1cZx/Q9cbIjFPwp1a+K5CVFDXDcfbi/AQgAkVs0/cCIQDF+2fr23AoBslcOC4S0yAx94AbgxCntYuRqztxybsrcwIgMW86ZcT87TX2oaQ1xXk6vC68zqN6fBZEE7Wu1Fa1pAkCIElmOJP3qfAc/AAlj+dIwLHlqWgJwl3674CU9Bfui2bDAiEA0CKJpF8x7KANCcopEQC93PsbIztuML322LOfDV1Lw/k=';
$rsa=new Rsa($publicKey,$privateKey);
$str="abc";
echo "原始数据:".$str;
echo "<br/><hr>";
$res=$rsa->privEncrypt($str);
echo "私钥加密数据:".$res;
echo "<br/>";
$res2=$rsa->pubDecrypt($res);
echo "公钥解密数据:".$res2;
echo "<br/><hr>";
$res3=$rsa->pubEncrypt($str);
echo "公钥加密数据:".$res3;
echo "<br/>";
$res4=$rsa->privDecrypt($res3);
echo "私钥解密数据:".$res4;
echo "<br/><hr>";
echo "签名数据:".$str;
$res5=$rsa->sign($str);
echo "<br/>";
echo "签名结果:".$res5;
$res6=$rsa->verify($str,$res5);
echo "<br/>";
echo "验证签结果:".$res6;
5、自定义加密算法1
function encrypt($data, $key)
{
// 将字符串转化为字节数组
$data = str_split($data);
// 将密钥转化为字节数组
$key = str_split($key);
// 加密结果
$result = '';
foreach ($data as $index => $char) {
// 获取密钥字符的 ASCII 值
$keyChar = ord($key[$index % count($key)]);
// 将字符的 ASCII 值与密钥字符的 ASCII 值进行异或运算
$encryptedChar = ord($char) ^ $keyChar;
// 将加密后的字符拼接到结果字符串中
$result .= chr($encryptedChar);
}
// 将结果字符串转换为 base64 编码
$result = base64_encode($result);
// 返回加密结果
return $result;
}
function decrypt($data, $key)
{
// 将 base64 编码字符串转换为普通字符串
$data = base64_decode($data);
// 将字符串转化为字节数组
$data = str_split($data);
// 将密钥转化为字节数组
$key = str_split($key);
// 解密结果
$result = '';
foreach ($data as $index => $char) {
// 获取密钥字符的 ASCII 值
$keyChar = ord($key[$index % count($key)]);
// 将字符的 ASCII 值与密钥字符的 ASCII 值进行异或运算
$decryptedChar = ord($char) ^ $keyChar;
// 将解密后的字符拼接到结果字符串中
$result .= chr($decryptedChar);
}
// 返回解密结果
return $result;
}
// 使用示例
$data = "Hello, World!";
$key = "secretKey";
$encryptedData = encrypt($data, $key);
echo "加密后的数据: " . $encryptedData . "\n";
$decryptedData = decrypt($encryptedData, $key);
echo "解密后的数据: " . $decryptedData . "\n";
6、自定义加密算法2
function encrypt($data, $key) {
$encryptedData = '';
$keyLength = strlen($key);
$dataLength = strlen($data);
for ($i = 0; $i < $dataLength; $i++) {
$encryptedData .= chr(ord($data[$i]) ^ ord($key[$i % $keyLength]));
}
return base64_encode($encryptedData);
}
function decrypt($data, $key) {
$data = base64_decode($data);
$decryptedData = '';
$keyLength = strlen($key);
$dataLength = strlen($data);
for ($i = 0; $i < $dataLength; $i++) {
$decryptedData .= chr(ord($data[$i]) ^ ord($key[$i % $keyLength]));
}
return $decryptedData;
}
// 使用示例
$data = 'Hello, World!';
$key = 'secretKey';
$encryptedData = encrypt($data, $key);
echo '加密后的数据: ' . $encryptedData . "\n";
$decryptedData = decrypt($encryptedData, $key);
echo '解密后的数据: ' . $decryptedData . "\n";
7、自定义加密算法3
function encrypt($data, $key) {
$encryptedData = '';
$keyLength = strlen($key);
$dataLength = strlen($data);
for ($i = 0; $i < $dataLength; $i++) {
$encryptedData .= chr((ord($data[$i]) + ord($key[$i % $keyLength])) % 256);
}
return bin2hex($encryptedData);
}
function decrypt($data, $key) {
$data = hex2bin($data);
$decryptedData = '';
$keyLength = strlen($key);
$dataLength = strlen($data);
for ($i = 0; $i < $dataLength; $i++) {
$decryptedData .= chr((ord($data[$i]) - ord($key[$i % $keyLength]) + 256) % 256);
}
return $decryptedData;
}
// 使用示例
$data = 'Hello, World!';
$key = 'secretKey';
$encryptedData = encrypt($data, $key);
echo '加密后的数据: ' . $encryptedData . "\n";
$decryptedData = decrypt($encryptedData, $key);
echo '解密后的数据: ' . $decryptedData . "\n";
二、不可解密加密算法
1、md5算法
$pex='pwd';
$pwd='123456';
$pwdMd5=md5($pex.$pwd);
//校验
$pwd=$_POST['pwd'];
$postPwdMd5=md5($pex.$pwd);
if($pwdMd5==$postPwdMd5){
echo '密码正确';
}else{
echo '密码错误';
}
2、crypt算法
$pex='pwd';
$pwd='123456';
$pwdMd5=crypt($pwd,$pex);
//校验
$pwd=$_POST['pwd'];
$postPwdMd5=crypt($pwd,$pex);
if($pwdMd5==$postPwdMd5){
echo '密码正确';
}else{
echo '密码错误';
}
3、sha1算法
$pex='pwd';
$pwd='123456';
$pwdMd5=sha1($pwd,$pex);
//校验
$pwd=$_POST['pwd'];
$postPwdMd5=sha1($pwd,$pex);
if($pwdMd5==$postPwdMd5){
echo '密码正确';
}else{
echo '密码错误';
}
5、hash 算法
$pex='pwd';
$pwd='123456';
$pwdMd5=hash("sha256", $pwd.$pex);
//校验
$pwd=$_POST['pwd'];
$postPwdMd5=hash("sha256", $pwd.$pex);
if($pwdMd5==$postPwdMd5){
echo '密码正确';
}else{
echo '密码错误';
}
6、 password_hash算法
<?php
$pex='pwd';
$pwd='123456';
$pwdMd5=password_hash($pwd.$pex,PASSWORD_BCRYPT);
//校验
$pwd=$_POST['pwd'];
$postPwdMd5=password_verify($pwd.$pex,$pwdMd5);
if($pwdMd5==$postPwdMd5){
echo '密码正确';
}else{
echo '密码错误';
}
到了这里,关于php常用加密算法大全aes、3des、rsa等的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!