古典密码学
方法:替换法,移位法
案例:凯撒密码
近代密码学
方法:替换法,移位法
替换法:单表替换和多表替换
案例:凯撒密码
破解:概率分析法
现代密码学
- 散列函数
MD5,SHA-1,SHA-256,SHA-512
md5('111')是消息摘要 - 对称加密
- DES
$key = '12345678'; $iv = '12345678'; //加解密算法/工作模式/填充方式 //DES/[CBC|ECB] //加密 OPENSSL_ZERO_PADDING //8位 $content = '你好啊!小米12'; $cipher = 'DES-CBC'; $ivlength = openssl_cipher_iv_length($cipher); //echo openssl_cipher_iv_length($cipher); $res1 = openssl_random_pseudo_bytes($ivlength); $encryptStr = openssl_encrypt($content, $cipher, $key, OPENSSL_RAW_DATA, $iv); echo 'DES-CBC加密:'.base64_encode($encryptStr).'<br>'; $decryptStr = openssl_decrypt($encryptStr, $cipher, $key, OPENSSL_RAW_DATA, $iv); echo 'DES-CBC解密:'.$decryptStr.'<br>'; $cipher = 'DES-ECB'; $encryptStr = openssl_encrypt($content, $cipher, $key, OPENSSL_RAW_DATA); echo 'DES-ECB加密:'.base64_encode($encryptStr).'<br>'; $decryptStr = openssl_decrypt($encryptStr, $cipher, $key, OPENSSL_RAW_DATA); echo 'DES-ECB解密:'.$decryptStr.'<br>';
- AES
$key = '123456'; $iv = '0102030405060708'; //16位 //加解密算法/工作模式/填充方式 //DES/[CBC|ECB] //加密 OPENSSL_ZERO_PADDING $content = 's123456'; $cipher = 'AES-128-CBC'; $ivlength = openssl_cipher_iv_length($cipher); //echo openssl_cipher_iv_length($cipher); $res1 = openssl_random_pseudo_bytes($ivlength); $encryptStr = openssl_encrypt($content, $cipher, $key, OPENSSL_RAW_DATA, $iv); echo 'DES-CBC加密:'.base64_encode($encryptStr).'<br>'; $decryptStr = openssl_decrypt($encryptStr, $cipher, $key, OPENSSL_RAW_DATA, $iv); echo 'DES-CBC解密:'.$decryptStr.'<br>'; $cipher = 'AES-128-ECB'; $encryptStr = openssl_encrypt($content, $cipher, $key, OPENSSL_RAW_DATA); echo 'DES-ECB加密:'.base64_encode($encryptStr).'<br>'; $decryptStr = openssl_decrypt($encryptStr, $cipher, $key, OPENSSL_RAW_DATA); echo 'DES-ECB解密:'.$decryptStr.'<br>';
- 非对称加密
- RSA
//RSA-private加密 $content = '你好啊!'; $filename = 'rsa_private.pem'; $fp = fopen($filename, 'r+'); $priKey = fread($fp, filesize($filename)); fclose($fp); $res = openssl_pkey_get_private($priKey); openssl_private_encrypt($content, $encryptPri, $res, OPENSSL_PKCS1_PADDING); echo 'RSA-private加密:'.base64_encode($encryptPri); echo '<br>'; //RSA-public解密 $filename = 'rsa_public.txt'; $fp = fopen($filename, 'r+'); $pbuKey = fread($fp, filesize($filename)); fclose($fp); openssl_public_decrypt($encryptPri, $decryptPbu, $pbuKey, OPENSSL_PKCS1_PADDING); echo 'RSA-public解密:'.$decryptPbu; echo '<br>'; //RSA-public加密 openssl_public_encrypt($content, $encryPbu, $pbuKey, OPENSSL_PKCS1_PADDING); echo 'RSA-public加密:'.base64_encode($encryPbu); echo '<br>'; //RSA-private解密 openssl_private_decrypt($encryPbu, $descryPri, $priKey, OPENSSL_PKCS1_PADDING); echo 'RSA-private加密:'.$descryPri;
备注
- 数字签名(又称公钥数字签名):是只有信息的发送者才能产生的别人无法伪造的一段数字串,这段数字串同时也是对信息的发送者发送信息真实性的一个有效证明。
- HTTPS原理: