php加密笔记[通俗易懂]

2022-07-19 09:07:49 浏览数 (1)

大家好,又见面了,我是全栈君。

1.加密解决那些问题

防止通信内容被窃听

防止通讯内容被篡改

2. des(data encryption standard:数据加密标准)

des是一种将64bit的明文加密成64bit的密文的对称算法.

现在des已经可以被暴力破解.

3.aes :目前没有被破解,推荐使用的

4.aes 加密步骤

5.des 加密操作

代码语言:javascript复制
//openssl_encrypt($data, $method, $key, $options = 0, $iv = "", &$tag = NULL, $aad = "", $tag_length = 16)
//加密
$data = "wo shi a student";
$method = 'DES-CBC';
$key = uniqid();
$options = OPENSSL_RAW_DATA;
$iv = '12345678';
$content = openssl_encrypt($data, $method, $key, $options, $iv);
var_dump($content);

//解密
$content = openssl_decrypt($content, $method, $key, $options, $iv);
dd($content);

6.aes 加密操作

代码语言:javascript复制
//加密
$data = "wo shi a student";
//$method = 'DES-CBC';  //des加密方法
$method = 'AES-128-CBC'; //aes加密方法
$key = uniqid();
$options = OPENSSL_RAW_DATA;
$iv = '1234567812345678'; 
$content = openssl_encrypt($data, $method, $key, $options, $iv);
var_dump($content);

//解密
$content = openssl_decrypt($content, $method, $key, $options, $iv);
dd($content);
代码语言:javascript复制
//改变key的写法,获得动态的iv向量
//加密
$data = "wo shi a student";
//$method = 'DES-CBC';  //des加密方法
$method = 'AES-128-CBC'; //aes加密方法
$id = uniqid();
$key = md5($id);
$options = OPENSSL_RAW_DATA;
$iv = substr($key, 0, 16);
//var_dump($id);
//var_dump($key);
$content = openssl_encrypt($data, $method, $key, $options, $iv);
var_dump($content);

//解密
$content = openssl_decrypt($content, $method, $key, $options, $iv);
dd($content);

7. rsa 加密

8.rsa 非对称加密操作

代码语言:javascript复制
//公钥私钥内容,自己生成  在http://web.chacuo.net/netrsakeypair
$public = '-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuXLbkP3vKy0qSA n4Bhs
UxP8lw4sAYF15ZhkInRnA50BzdUn8oukl 3/bejKhoH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2F C2eTz9uECgYEA5wyRyjWiXR9WTE3T
QwNQZpWpOhGQKBy70CJDWBZNPFjW1or C/fWhiG6y i d9ywi7s5KqDtnJMOciQL
pft3fNjmRGx29RBE4DxWuH4QgGv9ALC4Dxd8mZ8t8cjE249ytDKZeCurBlw403f 
ONDc1P50YR2hZ7FsHwbMAwpCl9cCgYEAzXmmEKDBlE6DkqeyorkjYZhTJaR56SJV
S6TR5MBzhzefQ vZEpFF5mE0SrV6i8eLELozBr1rOc41ltcMoHOd PqiK6FekgMM
Do6dDNEyM4GNLT7yJ/CFleOSKkJsLRBglV QYHb5db39okrQ7mOD2DGfho/Z4RMm
qS qE3pj9XsCgYEAxQHRufKbm/2hbvSZI7XKs33/SrU5DwrZFfJxK8XwW3mAznS7
g byc3KFPWMaLUfu/WZJq1CG6o/1rFuR1xihXMHxLfyyPel YRjx9OQBHA94LkOA
3dVv9 ljmptPR8t7yayvcWkCriyqg534Fef5tF 3SiuiubDt VKpWBpCiAcCgYBL
Ft 3R g2WJMAr0Q670mcytvn77mVlzRwlmUWIbaWfZ84ZSJJBMn0rSU9ACEun9eU
X1dyaRqcX116KFC2 kmKyZ0mMKys02w jaObIYAlDn0XQd g9Ob8pdOumeOKfGev
buMGPbRgcPQVsR21YKy3/zp7JGrNNAB257JRt/J7rQKBgQCB3TiQbyY/2YuhzF9W
drTHcz16s8gabv5KGOwyMw5EbqTN7T/6HJKLqRg/hHNaneexZitIFmJIPEwejWyj
 TtvrnxzO7zLDjsS6yEhTJDCKR/ylmx3jCvdI8k7tB2lBKy2CM5jkeTPDNMDG9Dq
crL/RLzgZSKIRpdWXb9KShWsGg==
-----END PRIVATE KEY-----';

//公钥(私钥)加密
$content = 'this is rsa   test ';
$encrypted='';
$res = openssl_public_encrypt($content, $encrypted, $public);

//私钥(公钥)解密
$decrypted='';
$dec = openssl_private_decrypt($encrypted, $decrypted, $private);
//$dec =openssl_public_decrypt( $encrypted,$decrypted,$public); //如果使用公钥加密,再使用公钥解密的话,会返回false,失败,这就是rsa非对称加密算法

var_dump($dec);
var_dump($decrypted);
die;

解密后结果:

9.中间人攻击

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/111630.html原文链接:https://javaforall.cn

0 人点赞