zoukankan      html  css  js  c++  java
  • node加密

    var crypto = require('crypto');

    //md5加密

    exports.md5 = function (str) {

    var md5sum = crypto.createHash('md5');
    md5sum.update(str);
    str = md5sum.digest('hex');
    return str;
    }

    //aes192加密

    exports.encrypt = function (str, secret) {
    var cipher = crypto.createCipher('aes192', secret);
    var enc = cipher.update(str, 'utf8', 'hex');
    enc += cipher.final('hex');
    return enc;
    }

    //aes192解密

    exports.decrypt = function (str, secret) {
    var decipher = crypto.createDecipher('aes192', secret);
    var dec = decipher.update(str, 'hex', 'utf8');
    dec += decipher.final('utf8');
    return dec;
    }

    //aes-128-cbc加密和解密

    let str = 'aaaaaaaa';

    const crypto = require('crypto');
    let key = "08281690909";
    let iv = "0828164343434";
    let encipher = crypto.createCipheriv('aes-128-cbc', key, iv);
    let encoded = encipher.update(str,'utf8','hex');
    encoded += encipher.final('hex')
    console.log('加密'+encoded);//da4941c6e797895527305b6b6ca28b37

    let decipher = crypto.createDecipheriv('aes-128-cbc', key, iv);
    let decoded = decipher.update(encoded,'hex','utf8')
    decoded += decipher.final('utf8')
    console.log('解密'+decoded)

    //aes-128-cbc
    exports.wxPaValidate = function(sessionKey, encryptedData, iv) {
    const sessionKeyVal = new Buffer(sessionKey, 'base64')
    const encryptedDataVal = new Buffer(encryptedData, 'base64')
    const ivVal = new Buffer(iv, 'base64')
    try {
    // 解密
    var decipher = crypto.createDecipheriv('aes-128-cbc', sessionKeyVal, ivVal)
    // 设置自动 padding 为 true,删除填充补位
    decipher.setAutoPadding(true)
    var decoded = decipher.update(encryptedDataVal, 'binary', 'utf8')
    decoded += decipher.final('utf8')
    decoded = JSON.parse(decoded)
    } catch (err) {
    throw new Error('Illegal Buffer')
    }
    if (decoded.watermark.appid !== appId) {
    throw new Error('Illegal Buffer')
    }
    return decoded
    }

  • 相关阅读:
    Unity3D游戏-愤怒的小鸟游戏源码和教程(一)
    Unity插件-ShareSDK使用指南
    Unity 3D开发-C#脚本语言的一些基础用法
    Shader的函数公式以及使用的场景
    Shader的基本用法和语法结构
    iTween的用法总结
    Unity 3D游戏-消消乐(三消类)教程和源码
    Unity 3D游戏-NPC对话系统With XML
    XML教程、语法手册、数据读取方式大全
    ReSharper2017.3的列对齐、排版格式、列对齐错误的修复
  • 原文地址:https://www.cnblogs.com/qiyc/p/9591911.html
Copyright © 2011-2022 走看看