• php常用加密算法大全aes、3des、rsa等


    目录

    一、可解密加解密算法

    1、aes 加解密算法

    2、旧3des加解密方法

     3、新3des加解密方法

    4、rsa公私钥加解密、签名验签方法

    5、自定义加密算法1 

    6、自定义加密算法2

    7、自定义加密算法3

    二、不可解密加密算法 

    1、md5算法 

    2、crypt算法

    3、sha1算法

    5、hash 算法

    6、 password_hash算法


    一、可解密加解密算法

    1、aes 加解密算法
    1. class AES {
    2. private $key;
    3. private $iv;
    4. public function __construct($key, $iv) {
    5. $this->key = $key;
    6. $this->iv = $iv;
    7. }
    8. public function encrypt($data) {
    9. $encrypted = openssl_encrypt($data, 'AES-128-ECB', $this->key, OPENSSL_RAW_DATA);
    10. return base64_encode($encrypted);
    11. }
    12. public function decrypt($encryptedData) {
    13. $decrypted = openssl_decrypt(base64_decode($encryptedData), 'AES-128-ECB', $this->key, OPENSSL_RAW_DATA);
    14. return $decrypted;
    15. }
    16. public function encryptCBC($data) {
    17. $encrypted = openssl_encrypt($data, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA, $this->iv);
    18. return base64_encode($encrypted);
    19. }
    20. public function decryptCBC($encryptedData) {
    21. $decrypted = openssl_decrypt(base64_decode($encryptedData), 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA, $this->iv);
    22. return $decrypted;
    23. }
    24. }
    25. // 使用示例:
    26. $key = '0123456789abcdef'; // 16字节长度的密钥
    27. $iv = 'fedcba9876543210'; // 16字节长度的初始向量
    28. $aes = new AES($key, $iv);
    29. $data = 'Hello, World!';
    30. $encryptedECB = $aes->encrypt($data);
    31. echo 'AES-128-ECB加密后的数据:' . $encryptedECB . "\n";
    32. $decryptedECB = $aes->decrypt($encryptedECB);
    33. echo 'AES-128-ECB解密后的数据:' . $decryptedECB . "\n";
    34. $encryptedCBC = $aes->encryptCBC($data);
    35. echo 'AES-256-CBC加密后的数据:' . $encryptedCBC . "\n";
    36. $decryptedCBC = $aes->decryptCBC($encryptedCBC);
    37. echo 'AES-256-CBC解密后的数据:' . $decryptedCBC . "\n";
    2、旧3des加解密方法
    1. class Encrypt_3DES
    2. {
    3. //加密秘钥,
    4. private $_key;
    5. private $_iv;
    6. public function __construct($key, $iv)
    7. {
    8. $this->_key = $key;
    9. $this->_iv = $iv;
    10. }
    11. /**
    12. * 对字符串进行3DES加密
    13. * @param string 要加密的字符串
    14. * @return mixed 加密成功返回加密后的字符串,否则返回false
    15. */
    16. public function encrypt3DES($str)
    17. {
    18. $td = mcrypt_module_open(MCRYPT_3DES, "", MCRYPT_MODE_CBC, "");
    19. if ($td === false) {
    20. return false;
    21. }
    22. //检查加密key,iv的长度是否符合算法要求
    23. $key = $this->fixLen($this->_key, mcrypt_enc_get_key_size($td));
    24. $iv = $this->fixLen($this->_iv, mcrypt_enc_get_iv_size($td));
    25. if (mcrypt_generic_init($td, $key, $iv) !== 0) {
    26. return false;
    27. }
    28. $result = mcrypt_generic($td, $str);
    29. mcrypt_generic_deinit($td);
    30. mcrypt_module_close($td);
    31. return base64_encode($result); // 对加密后的结果进行Base64编码
    32. }
    33. /**
    34. * 对加密的字符串进行3DES解密
    35. * @param string 要解密的字符串
    36. * @return mixed 解密成功返回解密后的字符串,否则返回false
    37. */
    38. public function decrypt3DES($str)
    39. {
    40. $td = mcrypt_module_open(MCRYPT_3DES, "", MCRYPT_MODE_CBC, "");
    41. if ($td === false) {
    42. return false;
    43. }
    44. //检查加密key,iv的长度是否符合算法要求
    45. $key = $this->fixLen($this->_key, mcrypt_enc_get_key_size($td));
    46. $iv = $this->fixLen($this->_iv, mcrypt_enc_get_iv_size($td));
    47. if (mcrypt_generic_init($td, $key, $iv) !== 0) {
    48. return false;
    49. }
    50. $str = base64_decode($str); // 对加密字符串进行Base64解码
    51. $result = mdecrypt_generic($td, $str);
    52. mcrypt_generic_deinit($td);
    53. mcrypt_module_close($td);
    54. return $this->strUnPad($result);
    55. }
    56. /**
    57. * 返回适合算法长度的key,iv字符串
    58. * @param string $str key或iv的值
    59. * @param int $td_len 符合条件的key或iv长度
    60. * @return string 返回处理后的key或iv值
    61. */
    62. private function fixLen($str, $td_len)
    63. {
    64. $str_len = strlen($str);
    65. if ($str_len > $td_len) {
    66. return substr($str, 0, $td_len);
    67. } else if ($str_len < $td_len) {
    68. return str_pad($str, $td_len, "\0");
    69. }
    70. return $str;
    71. }
    72. /**
    73. * 返回适合算法的分组大小的字符串长度,末尾使用\0补齐
    74. * @param string $str 要加密的字符串
    75. * @param int $td_group_len 符合算法的分组长度
    76. * @return string 返回处理后字符串
    77. */
    78. private function strPad($str, $td_group_len)
    79. {
    80. $padding_len = $td_group_len - (strlen($str) % $td_group_len);
    81. return str_pad($str, strlen($str) + $padding_len, "\0");
    82. }
    83. /**
    84. * 返回适合算法的分组大小的字符串长度,去除末尾的\0
    85. * @param string $str 要解密的字符串
    86. * @return string 返回处理后字符串
    87. */
    88. private function strUnPad($str)
    89. {
    90. return rtrim($str, "\0");
    91. }
    92. }
    93. $key = '1a2bc@';
    94. $iv = '12345678'; // 将iv改为字符串形式
    95. $str = "abcd123";
    96. $encrypt = new Encrypt_3DES($key, $iv);
    97. $jiaData = $encrypt->encrypt3DES($str);
    98. $jieData = $encrypt->decrypt3DES($jiaData);
    99. echo "未加密字符串:".$str;
    100. echo "\n";
    101. echo "加密字符串:".$jiaData;
    102. echo "\n";
    103. echo "解密密字符串:".$jieData;
     3、新3des加解密方法
    1. class TripleDes {
    2. private $key;
    3. private $iv;
    4. public function __construct($key, $iv) {
    5. $this->key = $key;
    6. $this->iv = $iv;
    7. }
    8. public function encrypt($data) {
    9. $encrypted = openssl_encrypt($data, 'des-ede3-cbc', $this->key, OPENSSL_RAW_DATA, $this->iv);
    10. return base64_encode($encrypted);
    11. }
    12. public function decrypt($encryptedData) {
    13. $decrypted = openssl_decrypt(base64_decode($encryptedData), 'des-ede3-cbc', $this->key, OPENSSL_RAW_DATA, $this->iv);
    14. return $decrypted;
    15. }
    16. }
    17. $key = "2312342132";
    18. $iv = "12345678"; // 初始化向量长度必须为8位
    19. $des = new TripleDes($key, $iv);
    20. $data = "adsadb123";
    21. echo "要加密的数据:{$data}\n";
    22. $encrypted = $des->encrypt($data);
    23. echo "加密后的数据:" . $encrypted . "\n";
    24. $decrypted = $des->decrypt($encrypted);
    25. echo "解密后的数据:" . $decrypted . "\n";
    4、rsa公私钥加解密、签名验签方法
    1. /**
    2. * RSA签名类
    3. */
    4. class Rsa
    5. {
    6. public $publicKey = '';
    7. public $privateKey = '';
    8. private $_privKey;
    9. /**
    10. * * private key
    11. */
    12. private $_pubKey;
    13. /**
    14. * * public key
    15. */
    16. private $_keyPath;
    17. /**
    18. * * the keys saving path
    19. */
    20. /**
    21. * * the construtor,the param $path is the keys saving path
    22. * @param string $publicKey 公钥
    23. * @param string $privateKey 私钥
    24. */
    25. public function __construct($publicKey = null, $privateKey = null)
    26. {
    27. $this->setKey($publicKey, $privateKey);
    28. }
    29. /**
    30. * 设置公钥和私钥
    31. * @param string $publicKey 公钥
    32. * @param string $privateKey 私钥
    33. */
    34. public function setKey($publicKey = null, $privateKey = null)
    35. {
    36. if (!is_null($publicKey)) {
    37. $this->publicKey = $publicKey;
    38. }
    39. if (!is_null($privateKey)) {
    40. $this->privateKey = $privateKey;
    41. }
    42. }
    43. /**
    44. * * setup the private key
    45. */
    46. private function setupPrivKey()
    47. {
    48. if (is_resource($this->_privKey)) {
    49. return true;
    50. }
    51. $pem = chunk_split($this->privateKey, 64, "\n");
    52. $pem = "-----BEGIN PRIVATE KEY-----\n" . $pem . "-----END PRIVATE KEY-----\n";
    53. $this->_privKey = openssl_pkey_get_private($pem);
    54. return true;
    55. }
    56. /**
    57. * * setup the public key
    58. */
    59. private function setupPubKey()
    60. {
    61. if (is_resource($this->_pubKey)) {
    62. return true;
    63. }
    64. $pem = chunk_split($this->publicKey, 64, "\n");
    65. $pem = "-----BEGIN PUBLIC KEY-----\n" . $pem . "-----END PUBLIC KEY-----\n";
    66. $this->_pubKey = openssl_pkey_get_public($pem);
    67. return true;
    68. }
    69. /**
    70. * * encrypt with the private key
    71. */
    72. public function privEncrypt($data)
    73. {
    74. if (!is_string($data)) {
    75. return null;
    76. }
    77. $this->setupPrivKey();
    78. $r = openssl_private_encrypt($data, $encrypted, $this->_privKey);
    79. if ($r) {
    80. return base64_encode($encrypted);
    81. }
    82. return null;
    83. }
    84. /**
    85. * * decrypt with the private key
    86. */
    87. public function privDecrypt($encrypted)
    88. {
    89. if (!is_string($encrypted)) {
    90. return null;
    91. }
    92. $this->setupPrivKey();
    93. $encrypted = base64_decode($encrypted);
    94. $r = openssl_private_decrypt($encrypted, $decrypted, $this->_privKey);
    95. if ($r) {
    96. return $decrypted;
    97. }
    98. return null;
    99. }
    100. /**
    101. * * encrypt with public key
    102. */
    103. public function pubEncrypt($data)
    104. {
    105. if (!is_string($data)) {
    106. return null;
    107. }
    108. $this->setupPubKey();
    109. $r = openssl_public_encrypt($data, $encrypted, $this->_pubKey);
    110. if ($r) {
    111. return base64_encode($encrypted);
    112. }
    113. return null;
    114. }
    115. /**
    116. * * decrypt with the public key
    117. */
    118. public function pubDecrypt($crypted)
    119. {
    120. if (!is_string($crypted)) {
    121. return null;
    122. }
    123. $this->setupPubKey();
    124. $crypted = base64_decode($crypted);
    125. $r = openssl_public_decrypt($crypted, $decrypted, $this->_pubKey);
    126. if ($r) {
    127. return $decrypted;
    128. }
    129. return null;
    130. }
    131. /**
    132. * 构造签名
    133. * @param string $dataString 被签名数据
    134. * @return string
    135. */
    136. public function sign($dataString)
    137. {
    138. $this->setupPrivKey();
    139. $signature = false;
    140. openssl_sign($dataString, $signature, $this->_privKey);
    141. return base64_encode($signature);
    142. }
    143. /**
    144. * 验证签名
    145. * @param string $dataString 被签名数据
    146. * @param string $signString 已经签名的字符串
    147. * @return number 1签名正确 0签名错误
    148. */
    149. public function verify($dataString, $signString)
    150. {
    151. $this->setupPubKey();
    152. $signature = base64_decode($signString);
    153. $flg = openssl_verify($dataString, $signature, $this->_pubKey);
    154. return $flg;
    155. }
    156. public function __destruct()
    157. {
    158. is_resource($this->_privKey) && @openssl_free_key($this->_privKey);
    159. is_resource($this->_pubKey) && @openssl_free_key($this->_pubKey);
    160. }
    161. }
    162. $publicKey = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKZ1mKTymRoGKnHiP1xAy4aiyt5r0BscCZnDAonCrMFZ4kBGriPNHxEaLr5lfBnMKw7k6i+2dsFPSEZooTvqtPUCAwEAAQ==';
    163. $privateKey = 'MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEApnWYpPKZGgYqceI/XEDLhqLK3mvQGxwJmcMCicKswVniQEauI80fERouvmV8GcwrDuTqL7Z2wU9IRmihO+q09QIDAQABAkBunx3nGHXYjppsfn++7iyTd+I7+Agfy/0xWyB3rpEiGGgfemjcRFaeq5SC2vUNXsrEOY5gbUSQmFxH//Cym18NAiEA1z1cZx/Q9cbIjFPwp1a+K5CVFDXDcfbi/AQgAkVs0/cCIQDF+2fr23AoBslcOC4S0yAx94AbgxCntYuRqztxybsrcwIgMW86ZcT87TX2oaQ1xXk6vC68zqN6fBZEE7Wu1Fa1pAkCIElmOJP3qfAc/AAlj+dIwLHlqWgJwl3674CU9Bfui2bDAiEA0CKJpF8x7KANCcopEQC93PsbIztuML322LOfDV1Lw/k=';
    164. $rsa=new Rsa($publicKey,$privateKey);
    165. $str="abc";
    166. echo "原始数据:".$str;
    167. echo "

      "
      ;
    168. $res=$rsa->privEncrypt($str);
    169. echo "私钥加密数据:".$res;
    170. echo "
      "
      ;
    171. $res2=$rsa->pubDecrypt($res);
    172. echo "公钥解密数据:".$res2;
    173. echo "

      "
      ;
    174. $res3=$rsa->pubEncrypt($str);
    175. echo "公钥加密数据:".$res3;
    176. echo "
      "
      ;
    177. $res4=$rsa->privDecrypt($res3);
    178. echo "私钥解密数据:".$res4;
    179. echo "

      "
      ;
    180. echo "签名数据:".$str;
    181. $res5=$rsa->sign($str);
    182. echo "
      "
      ;
    183. echo "签名结果:".$res5;
    184. $res6=$rsa->verify($str,$res5);
    185. echo "
      "
      ;
    186. echo "验证签结果:".$res6;
    5、自定义加密算法
    1. function encrypt($data, $key)
    2. {
    3. // 将字符串转化为字节数组
    4. $data = str_split($data);
    5. // 将密钥转化为字节数组
    6. $key = str_split($key);
    7. // 加密结果
    8. $result = '';
    9. foreach ($data as $index => $char) {
    10. // 获取密钥字符的 ASCII 值
    11. $keyChar = ord($key[$index % count($key)]);
    12. // 将字符的 ASCII 值与密钥字符的 ASCII 值进行异或运算
    13. $encryptedChar = ord($char) ^ $keyChar;
    14. // 将加密后的字符拼接到结果字符串中
    15. $result .= chr($encryptedChar);
    16. }
    17. // 将结果字符串转换为 base64 编码
    18. $result = base64_encode($result);
    19. // 返回加密结果
    20. return $result;
    21. }
    22. function decrypt($data, $key)
    23. {
    24. // 将 base64 编码字符串转换为普通字符串
    25. $data = base64_decode($data);
    26. // 将字符串转化为字节数组
    27. $data = str_split($data);
    28. // 将密钥转化为字节数组
    29. $key = str_split($key);
    30. // 解密结果
    31. $result = '';
    32. foreach ($data as $index => $char) {
    33. // 获取密钥字符的 ASCII 值
    34. $keyChar = ord($key[$index % count($key)]);
    35. // 将字符的 ASCII 值与密钥字符的 ASCII 值进行异或运算
    36. $decryptedChar = ord($char) ^ $keyChar;
    37. // 将解密后的字符拼接到结果字符串中
    38. $result .= chr($decryptedChar);
    39. }
    40. // 返回解密结果
    41. return $result;
    42. }
    43. // 使用示例
    44. $data = "Hello, World!";
    45. $key = "secretKey";
    46. $encryptedData = encrypt($data, $key);
    47. echo "加密后的数据: " . $encryptedData . "\n";
    48. $decryptedData = decrypt($encryptedData, $key);
    49. echo "解密后的数据: " . $decryptedData . "\n";
    6、自定义加密算法2
    1. function encrypt($data, $key) {
    2. $encryptedData = '';
    3. $keyLength = strlen($key);
    4. $dataLength = strlen($data);
    5. for ($i = 0; $i < $dataLength; $i++) {
    6. $encryptedData .= chr(ord($data[$i]) ^ ord($key[$i % $keyLength]));
    7. }
    8. return base64_encode($encryptedData);
    9. }
    10. function decrypt($data, $key) {
    11. $data = base64_decode($data);
    12. $decryptedData = '';
    13. $keyLength = strlen($key);
    14. $dataLength = strlen($data);
    15. for ($i = 0; $i < $dataLength; $i++) {
    16. $decryptedData .= chr(ord($data[$i]) ^ ord($key[$i % $keyLength]));
    17. }
    18. return $decryptedData;
    19. }
    20. // 使用示例
    21. $data = 'Hello, World!';
    22. $key = 'secretKey';
    23. $encryptedData = encrypt($data, $key);
    24. echo '加密后的数据: ' . $encryptedData . "\n";
    25. $decryptedData = decrypt($encryptedData, $key);
    26. echo '解密后的数据: ' . $decryptedData . "\n";
    7、自定义加密算法3
    1. function encrypt($data, $key) {
    2. $encryptedData = '';
    3. $keyLength = strlen($key);
    4. $dataLength = strlen($data);
    5. for ($i = 0; $i < $dataLength; $i++) {
    6. $encryptedData .= chr((ord($data[$i]) + ord($key[$i % $keyLength])) % 256);
    7. }
    8. return bin2hex($encryptedData);
    9. }
    10. function decrypt($data, $key) {
    11. $data = hex2bin($data);
    12. $decryptedData = '';
    13. $keyLength = strlen($key);
    14. $dataLength = strlen($data);
    15. for ($i = 0; $i < $dataLength; $i++) {
    16. $decryptedData .= chr((ord($data[$i]) - ord($key[$i % $keyLength]) + 256) % 256);
    17. }
    18. return $decryptedData;
    19. }
    20. // 使用示例
    21. $data = 'Hello, World!';
    22. $key = 'secretKey';
    23. $encryptedData = encrypt($data, $key);
    24. echo '加密后的数据: ' . $encryptedData . "\n";
    25. $decryptedData = decrypt($encryptedData, $key);
    26. echo '解密后的数据: ' . $decryptedData . "\n";

    二、不可解密加密算法 

    1、md5算法 
    1. $pex='pwd';
    2. $pwd='123456';
    3. $pwdMd5=md5($pex.$pwd);
    4. //校验
    5. $pwd=$_POST['pwd'];
    6. $postPwdMd5=md5($pex.$pwd);
    7. if($pwdMd5==$postPwdMd5){
    8. echo '密码正确';
    9. }else{
    10. echo '密码错误';
    11. }
    2、crypt算法
    1. $pex='pwd';
    2. $pwd='123456';
    3. $pwdMd5=crypt($pwd,$pex);
    4. //校验
    5. $pwd=$_POST['pwd'];
    6. $postPwdMd5=crypt($pwd,$pex);
    7. if($pwdMd5==$postPwdMd5){
    8. echo '密码正确';
    9. }else{
    10. echo '密码错误';
    11. }
    3、sha1算法
    1. $pex='pwd';
    2. $pwd='123456';
    3. $pwdMd5=sha1($pwd,$pex);
    4. //校验
    5. $pwd=$_POST['pwd'];
    6. $postPwdMd5=sha1($pwd,$pex);
    7. if($pwdMd5==$postPwdMd5){
    8. echo '密码正确';
    9. }else{
    10. echo '密码错误';
    11. }
    5、hash 算法
    1. $pex='pwd';
    2. $pwd='123456';
    3. $pwdMd5=hash("sha256", $pwd.$pex);
    4. //校验
    5. $pwd=$_POST['pwd'];
    6. $postPwdMd5=hash("sha256", $pwd.$pex);
    7. if($pwdMd5==$postPwdMd5){
    8. echo '密码正确';
    9. }else{
    10. echo '密码错误';
    11. }
    6、 password_hash算法
    1. $pex='pwd';
    2. $pwd='123456';
    3. $pwdMd5=password_hash($pwd.$pex,PASSWORD_BCRYPT);
    4. //校验
    5. $pwd=$_POST['pwd'];
    6. $postPwdMd5=password_verify($pwd.$pex,$pwdMd5);
    7. if($pwdMd5==$postPwdMd5){
    8. echo '密码正确';
    9. }else{
    10. echo '密码错误';
    11. }

  • 相关阅读:
    Linux vi/vim
    牛客网:NC26 括号生成
    福州市仓山区融丰锦秀山庄别墅设计
    PS制作文字扫光gif
    【拆解Vue3】侦听器是如何实现的?
    java计算机毕业设计好物网站MyBatis+系统+LW文档+源码+调试部署
    11-定位
    Linux之find命令的参数
    二肽二氨基丁酰苄基酰胺二乙酸盐/Dipeptide Diaminobutyroyl Benzylamide Diacetate/SYN-AKE
    asp.net水资源检测系统VS开发sqlserver数据库web结构c#编程Microsoft Visual Studio
  • 原文地址:https://blog.csdn.net/weixin_39934453/article/details/132688226