一、RSA加密

RSA只说PHP中的应用,详细的算法原理解释,请自行百度,或者参考(RSA加密算法-详细解释以及公钥加密为什么每次都不一样

总结:公钥加密、私钥解密、私钥签名、公钥验签。

注意:

1、加密方式:公钥加密、私钥解密、私钥签名、公钥验签。

2、明文超出长度,请分段加密,解密也一样

<?php

class Rsa
{
const CHAR_SET = "UTF-8";
const BASE_64_FORMAT = "UrlSafeNoPadding";
const RSA_ALGORITHM_KEY_TYPE = OPENSSL_KEYTYPE_RSA;
const RSA_ALGORITHM_SIGN = OPENSSL_ALGO_SHA256; protected $public_key;
protected $private_key;
protected $key_len; public function __construct($pub_key, $pri_key = null)
{
$this->public_key = $pub_key;
$this->private_key = $pri_key; $pub_id = openssl_get_publickey($this->public_key);
$this->key_len = openssl_pkey_get_details($pub_id)['bits'];
} /*
* 创建密钥对
*/
public static function createKeys($key_size = 2048)
{
$config = array(
"private_key_bits" => $key_size,
"private_key_type" => self::RSA_ALGORITHM_KEY_TYPE,
);
$res = openssl_pkey_new($config);
openssl_pkey_export($res, $private_key);
$public_key_detail = openssl_pkey_get_details($res);
$public_key = $public_key_detail["key"]; return [
"public_key" => $public_key,
"private_key" => $private_key,
];
} /*
* 公钥加密
*/
public function publicEncrypt($data)
{
$encrypted = '';
$part_len = $this->key_len / 8 - 11;
$parts = str_split($data, $part_len); foreach ($parts as $part) {
$encrypted_temp = '';
openssl_public_encrypt($part, $encrypted_temp, $this->public_key);
$encrypted .= $encrypted_temp;
} return $this->url_safe_base64_encode($encrypted);
} /*
* 私钥解密
*/
public function privateDecrypt($encrypted)
{
$decrypted = "";
$part_len = $this->key_len / 8;
$base64_decoded = url_safe_base64_decode($encrypted);
$parts = str_split($base64_decoded, $part_len); foreach ($parts as $part) {
$decrypted_temp = '';
openssl_private_decrypt($part, $decrypted_temp,$this->private_key);
$decrypted .= $decrypted_temp;
}
return $decrypted;
} /*
* 私钥加密
*/
public function privateEncrypt($data)
{
$encrypted = '';
$part_len = $this->key_len / 8 - 11;
$parts = str_split($data, $part_len); foreach ($parts as $part) {
$encrypted_temp = '';
openssl_private_encrypt($part, $encrypted_temp, $this->private_key);
$encrypted .= $encrypted_temp;
} return $this->url_safe_base64_encode($encrypted);
} /*
* 公钥解密
*/
public function publicDecrypt($encrypted)
{
$decrypted = "";
$part_len = $this->key_len / 8;
$base64_decoded = url_safe_base64_decode($encrypted);
$parts = str_split($base64_decoded, $part_len); foreach ($parts as $part) {
$decrypted_temp = '';
openssl_public_decrypt($part, $decrypted_temp,$this->public_key);
$decrypted .= $decrypted_temp;
}
return $decrypted;
} /*
* 数据签名
*/
public function sign($data)
{
openssl_sign($data, $sign, $this->private_key, self::RSA_ALGORITHM_SIGN); return $this->url_safe_base64_encode($sign);
} /*
* 数据签名验证
*/
public function verify($data, $sign)
{
$pub_id = openssl_get_publickey($this->public_key);
   //如果$data没有进行base64,请进行 base64后再验证
$res = openssl_verify($data, $this->url_safe_base64_decode($sign), $pub_id, self::RSA_ALGORITHM_SIGN);
return $res;
} /*
* base64_encode处理特殊字符(也有可能不需要替换,直接base64)
*/
public function url_safe_base64_encode ($data) {
return str_replace(array('+','/', '='),array('-','_', ''), base64_encode($data));
} /*
* base64_decode处理特殊字符(也有可能不需要,直接base64)
*/
public function url_safe_base64_decode ($data) {
$base_64 = str_replace(array('-','_'),array('+','/'), $data);
return base64_decode($base_64);
} } ?>

二、AES加密

AES加密算法原理自行百度或者:参考链接

mcrypt模块也可以实现:参考链接

php的openssl模块实现aes加密,比较简单

<?php

/* 
* AES加密
*/
class Aes
{
/**
*var string $method 加解密方法,可通过openssl_get_cipher_methods()获得
   */
private $method; /**
* var string $secret_key 加解密的密钥
*/
private $secret_key; /**
* var string $iv 密初始化向量,加解密的向量,有些方法需要设置
*/
private $iv; /**
* var string $options false-原始数据;true-base64后的数据
*/
private $options; /**
* 构造函数
*
* @param string $key 密钥
* @param string $method 加密方式
* @param string $iv iv向量
* @param mixed $options false-原始数据;true-base64后的数据
*
*/
public function __construct()
{
$this->secret_key = 'xxxxxxxxxx'; //自己的秘钥
$this->method = 'AES-128-CBC';
$this->iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($this->method));
$this->options = false;
} /**
* 加密方法,对数据进行加密,返回加密后的数据
*
* @param string $data 要加密的数据
*
* @return string
*
*/
public function encrypt($data)
{
$encrypted = openssl_encrypt($data, $this->method, $this->secret_key, $this->options, $this->iv);
return base64_encode($encrypted . '::' . $this->iv);
} /**
* 解密方法,对数据进行解密,返回解密后的数据
*
* @param string $data 要解密的数据
*
* @return string
*
*/
public function decrypt($data)
{
$arr = explode('::', base64_decode($data));
$encrypted_data = $arr[0];
$iv = $arr[1];
return openssl_decrypt($encrypted_data, $this->method, $this->secret_key, $this->options, $iv);
} }

最新文章

  1. JQ动画的简单介绍
  2. android WebView网页浏览器
  3. ios 音乐播放
  4. js获取时间戳
  5. 《Play for Java》学习笔记(五)Form
  6. windos系统快捷键 2015-05-08 23:31 24人阅读 评论(0) 收藏
  7. javaweb学习总结二十六(response对象的用法二 下载文件)
  8. Lambda 表达式型的排序法
  9. 10+ commonly using find command switches with example Unix/Linux
  10. java.lang.NoClassDefFoundError 异常
  11. 自定义函数动态执行SQL语句
  12. 微信JS分享功能--微信JS系列文章(二)
  13. C++ 中 double、 long double、long 和 long long
  14. C# 实现中国象棋【棋盘,棋子】
  15. Vue+Django2.0 restframework打造前后端分离的生鲜电商项目(1)
  16. 通过修改配置文件修改MySQL的时区设置
  17. word发布博客
  18. css3-盒模型display:-webkit-box;的使用
  19. Excel技巧--做一去重复的数据下拉列表
  20. 【bzoj4516】 Sdoi2016—生成魔咒

热门文章

  1. XSS Challenge(1)
  2. python学习04数据
  3. 使用Spring Boot搭建你的第一个应用程序
  4. 【JAVA基础】06 面向对象
  5. log4j MDC NDC详解
  6. Fourier Transform
  7. HDU - 1253 胜利大逃亡 (搜索)
  8. 聊聊select, poll 和 epoll
  9. 学习Vue第二节,v-cloak,v-text,v-html,v-bind,v-on使用
  10. CTF-Reverse-[GXYCTF2019]luck_guy