对称加密(就是加密和解密是一个密钥)
AES(就用这个就行,DES不安全)(注意启用openssl)
$a = openssl_get_cipher_methods();
$data = '我是需要加密的内容';
$key = uniqid();
$method = 'AES-128-CBC';
$iv = '1234567812345678';
$a = openssl_encrypt($data,$method,$key,OPENSSL_RAW_DATA,$iv);
dump($b=base64_encode($a));
$d = openssl_decrypt(base64_decode($b),$method,$key,OPENSSL_RAW_DATA,$iv);
dump($d);
const crypto = require('crypto');
function aesEncrypt(data, key, iv) {
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
let encrypted = cipher.update(data, 'utf8', 'hex');
encrypted += cipher.final('hex');
return encrypted;
}
function aesDecrypt(encrypted, key, iv) {
const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
const data = 'hello world';
const key = crypto.randomBytes(32);
const iv = crypto.randomBytes(16);
const encrypted = aesEncrypt(data, key, iv);
const decrypted = aesDecrypt(encrypted, key, iv);
console.log(`原始数据: ${data}`);
console.log(`密钥: ${key}`);
console.log(`初始化向量: ${iv}`);
console.log(`加密后的数据: ${encrypted}`);
console.log(`解密后的数据: ${decrypted}`);
下载地址:https://cdnjs.com/libraries/crypto-js
var aseKey = "12345678"
var message = "80018000142";
var encrypt = CryptoJS.AES.encrypt(message, CryptoJS.enc.Utf8.parse(aseKey), {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
}).toString();
console.log(encrypt);
var decrypt = CryptoJS.AES.decrypt(encrypt, CryptoJS.enc.Utf8.parse(aseKey), {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
}).toString(CryptoJS.enc.Utf8);
console.log(decrypt);
var key = CryptoJS.enc.Utf8.parse("zhgerXHBVaaKm8xy")
var plaintText = 'onlystar'
var encryptedData = CryptoJS.AES.encrypt(plaintText, key, {
iv: CryptoJS.enc.Utf8.parse(key),
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
})
console.log("加密前:"+plaintText)
console.log("加密后:"+encryptedData)
encryptedData = encryptedData.ciphertext.toString()
var encryptedHexStr = CryptoJS.enc.Hex.parse(encryptedData)
console.log("解密前hex:"+encryptedHexStr)
var encryptedBase64Str = CryptoJS.enc.Base64.stringify(encryptedHexStr)
console.log("解密前:"+encryptedBase64Str)
var decryptedData = CryptoJS.AES.decrypt(encryptedBase64Str, key, {
iv: CryptoJS.enc.Utf8.parse(key),
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
})
var decryptedStr = decryptedData.toString(CryptoJS.enc.Utf8)
console.log("解密后:"+decryptedStr)
使用非对称加密(就是加密,解密不是一个密钥)(缺点不适合加载大文件,并且必对称加密慢了不少)
//执行php命令,目的生成公钥和私钥
openssl genrsa -out rsa_private_key.pem 1024
openssl pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt -out private_key.pem
openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
第一条命令生成原始 RSA私钥文件 rsa_private_key.pem
第二条命令将原始 RSA私钥转换为 pkcs8格式
第三条生成RSA公钥 rsa_public_key.pem
$public_key = "公钥";
$private_key = "私钥";
$text = "我是待加密的内容";
$res = openssl_private_encrypt($text,$encrypt,$private_key);
if($res==false) echo '加密失败';
$res = openssl_public_decrypt($encrypt,$decrypt,$public_key );
if($res==false) echo '解密失败';
echo '解密后的明文数据:'.$decrypt;
摘要加密
md5
$str = "this is zifuchuan";
$res = md5($str);
$res = md5($str,true);
md5下载地址:https://gitcode.net/mirrors/blueimp/javascript-md5?utm_source=csdn_github_accelerator
字节cdn:https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/blueimp-md5/2.19.0/js/md5.js
let hash = md5("value");
sha1
$str = "this is string";
$res = sha1($str);
$res = sha1($str,true);
sha1下载地址:https://gitcode.net/mirrors/emn178/js-sha1
字节cdn:https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/js-sha1/0.6.0/sha1.js
let hash = sha1("value");
Crypt(不建议用,每个平台不一样,配置在php里面设置,如果就在一个机器使用可以使用他)
$str = "this is string";
$res = crypt($str);
$res = crypt($str,'jm');
password (推荐使用)
$pass='123456';
$jieguo=password_hash($pass,PASSWORD_DEFAULT);
dump($jieguo);
dump(password_verify($pass,$jieguo));
password_hash() – 对密码加密.
password_verify() – 验证已经加密的密码,检验其hash字串是否一致.
password_needs_rehash() – 给密码重新加密.
password_get_info() – 返回加密算法的名称和一些相关信息.
其他
base64_encode($data);
base64_decode($data);
var str = "hello";
var str64 = window.btoa("hello");
console.log("字符串是:"+str);
console.log("经base64编码后:"+str64);
console.log("base64解码后:"+window.atob(str64));