pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

PBEWithMD5AndTripleDES/CBC/PKCS5Padding的Nodejs javascript實(shí)現(xiàn)

Nodejs javascript implementation of PBEWithMD5AndTripleDES/CBC/PKCS5Padding(PBEWithMD5AndTripleDES/CBC/PKCS5Padding的Nodejs javascript實(shí)現(xiàn))
本文介紹了PBEWithMD5AndTripleDES/CBC/PKCS5Padding的Nodejs javascript實(shí)現(xiàn)的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

為了編寫一個(gè)簡單的 nodejs 應(yīng)用程序與用 java 編寫的服務(wù)器通信,我必須為 nodejs 實(shí)現(xiàn)以下功能.

In order to write an simple nodejs app talking to an server written in java I have to implement the following functionality for nodejs.

public class Crypto {
  Cipher decipher;

  byte[] salt = {
      (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
      (byte) 0x0A, (byte) 0x0B, (byte) 0x0C, (byte) 0x0D
  };
  int iterationCount = 10;

  public Crypto(String pass) {
    try {
      KeySpec keySpec = new PBEKeySpec(pass.toCharArray(), salt, iterationCount);

      SecretKey key = SecretKeyFactory.getInstance(
          "PBEWithMD5AndTripleDES").generateSecret(keySpec);

      ecipher = Cipher.getInstance("PBEWithMD5AndTripleDES/CBC/PKCS5Padding");

      AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);

      decipher.init(Cipher.DECRYPT_MODE, key, paramSpec);

    } catch (Exception ex) {
    }
  }
}

我使用 crypto nodejs 的模塊

var crypto = require('crypto'),
      pass = new Buffer(wek),
      salt = new Buffer([0x01, 0x02, 0x03, 0x04, 0x0A, 0x0B, 0x0C, 0x0D])
      password = 'mySecretPassword'
      key = crypto.pbkdf2(pass, salt, 10, 256)
      cipher, 
      encrypted;

cipher = crypto.createCipher('des-ede-cbc', key);
encrypted = cipher.update(new Buffer('the very secred information'));

將加密信息發(fā)送到服務(wù)器后,我無法使用上面 java 代碼示例中列出的 decipher 對象解密消息.我認(rèn)為主要問題是 md5 部分.我不知道如何使用 crypto nodejs 模塊來實(shí)現(xiàn)它.有誰知道如何解決這個(gè)問題?或者是否有任何其他模塊或庫來實(shí)現(xiàn)這一點(diǎn)?

After sending the encrypted information to the server, I can't decrypt the message with the decipher Object as listed in the java code sample above. I think the main problem is the md5 part. I can't figure out how to implement that with the crypto nodejs module. Has anyone an idea how to solve this problem? Or is ther any other module or library to achieve that?

我為 nodejs 嘗試了另一個(gè)模塊:node-forge

I tried another module for nodejs: node-forge

forge = require('node-forge')

var numIterations = 10,
      keyLength = 24,
      password = forge.util.createBuffer('mySecretPassword'),
      salt = new forge.util.ByteBuffer(new Uint8Array([0x01, 0x02, 0x03, 0x04, 0x0A, 0x0B, 0x0C, 0x0D])),
      derivedKey = forge.pkcs5.pbkdf2(password, salt.getBytes(), numIterations, keyLength, forge.md.md5.create())
      iv = {}; // TODO... ???

var cipher = forge.des.createEncryptionCipher(derivedKey);
cipher.start(iv);
cipher.update('the very secred information');
cipher.finish();
var encrypted = cipher.output;

但我有幾個(gè)問題/疑問:

But I have several problems/questions:

  • 我是否在 javascript 中使用了正確的算法?
  • salt計(jì)算是否與java實(shí)現(xiàn)匹配?
  • 如何確定在 java 實(shí)現(xiàn)中使用了哪個(gè) keyLength?
  • 初始化向量在java實(shí)現(xiàn)中是如何產(chǎn)生的?在 node-forge 的最后一個(gè)代碼示例中,我必須在 cipher.start(iv) 上提供 iv.在 java 代碼中,我看不到這是如何完成的.我認(rèn)為客戶端和服務(wù)器上的 iv 必須相同還是不正確?
  • Do I use the correct algorithm in javascript?
  • Is the salt calculation match with the java implementation?
  • How can I determine which keyLength is used in the java implementation?
  • How is the initialization vector generated in the java implementation? In the last code sample with node-forgeI have to provide the iv on cipher.start(iv). In the java code I can't see how this is done. In my opinion the iv must be the same on client and server or is this incorrect?

推薦答案

我對 com.sun.crypto.provider.PBES1Core#deriveCipherKey() 處的密鑰派生函數(shù)的 DESede 部分進(jìn)行了逆向工程;

I reverse engineered the DESede part of the key derivation function found at com.sun.crypto.provider.PBES1Core#deriveCipherKey();

我們在 Java 服務(wù)器中使用 Jasypt 作為加密庫,我們的 node.js 服務(wù)器能夠使用它進(jìn)行加密和解密.我希望它有所幫助(用 ES2015 編寫,在 node v4.0.0 及更高版本中運(yùn)行):

We use Jasypt as encryption library in a Java server and our node.js server is able to encrypt and decrypt with this. I hope it helps (Written in ES2015, runs in node v4.0.0 and up):

'use strict';
var crypto = require('crypto');

class Encryption {
    constructor() {
        this.privateKey = new Buffer('<your password>', 'utf-8');
    }

    encrypt(message) {
        var salt = crypto.randomBytes(8);
        var key = this._generateKey(this.privateKey, salt);
        var cipher = crypto.createCipheriv('des-ede3-cbc', this._subBuf(key, 0, 24), this._subBuf(key, 24));
        var result = cipher.update(message, 'utf-8', 'hex');
        return salt.toString('hex') + result + cipher.final('hex');
    }

    decrypt(message) {
        var salt = new Buffer(message.substr(0, 16), 'hex');
        var key = this._generateKey(this.privateKey, salt);
        message = message.substr(16);
        var decipher = crypto.createDecipheriv('des-ede3-cbc', this._subBuf(key, 0, 24), this._subBuf(key, 24));
        var result = decipher.update(message, 'hex', 'utf-8');
        return result + decipher.final('utf-8');
    }

    _generateKey(password, salt) {
        if (!(password instanceof Buffer)) {
            throw new Error('Password needs to be a buffer');
        }
        if (!(salt instanceof Buffer) || salt.length != 8) {
            throw new Error('Salt needs to be an 8 byte buffer');
        }

        var iterations;
        for(iterations = 0; iterations < 4 && salt[iterations] == salt[iterations + 4]; ++iterations) {}

        if(iterations == 4) {
            for(iterations = 0; iterations < 2; ++iterations) {
                var tmp = salt[iterations];
                salt[iterations] = salt[3 - iterations];
                salt[2] = tmp; // Seems like an error that we have to live with now
            }
        }

        var result = new Buffer(32);
        for(iterations = 0; iterations < 2; ++iterations) {
            var intermediate = new Buffer(salt.length / 2);
            for (let i = 0; i < salt.length / 2; i++) {
                intermediate[i] = salt[iterations * (salt.length / 2) + i];
            }

            for(let i = 0; i < 1000; ++i) {
                var hash = crypto.createHash('md5');
                hash.update(intermediate);
                hash.update(password);
                intermediate = hash.digest();
            }

            for (let i = 0; i<intermediate.length; i++) {
                result[i + (iterations * 16)] = intermediate[i];
            }
        }
        return result;
    }

    _subBuf(buffer, start, length) {
        if (!length) {
            length = buffer.length - start;
        }
        var result = new Buffer(length, 'hex');
        for (let i = 0; i < length; i++) {
            result[i] = buffer[i + start]
        }
        return result;
    }
}

解釋一下發(fā)生了什么:

  • 加密消息以十六進(jìn)制格式返回,其他內(nèi)容可能更適合您的實(shí)現(xiàn).
  • _generateKey() 是來自 java 源代碼的直接副本.
  • 使用的密鑰長度為 32 字節(jié),并假設(shè)前 24 個(gè)字節(jié)是 TripleDES 的密鑰,后 8 個(gè)字節(jié)是鹽
  • 生成的消息以用于加密消息的隨機(jī)生成的鹽為前綴.
  • 根據(jù) JVM 的安全設(shè)置,您可能實(shí)際上并未使用 des-ede3(cbc 似乎是一個(gè)固定設(shè)置).您絕對應(yīng)該仔細(xì)檢查這是否適用于您的設(shè)置.

這里可能需要清理一些代碼,但它至少應(yīng)該讓您朝著正確的方向開始.

Some code clean up might be necessary here, but it should at least get you started in the right direction.

這篇關(guān)于PBEWithMD5AndTripleDES/CBC/PKCS5Padding的Nodejs javascript實(shí)現(xiàn)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Using discord.js to detect image and respond(使用 discord.js 檢測圖像并響應(yīng))
Check if user ID exists in Discord server(檢查 Discord 服務(wù)器中是否存在用戶 ID)
Guild Member Add does not work (discordjs)(公會成員添加不起作用(discordjs))
Creating my first bot using REPLIT but always error Discord.JS(使用 REPLIT 創(chuàng)建我的第一個(gè)機(jī)器人,但總是錯(cuò)誤 Discord.JS)
How do I code event/command handlers for my Discord.js bot?(如何為我的 Discord.js 機(jī)器人編寫事件/命令處理程序?)
How to find a User ID from a Username in Discord.js?(如何從 Discord.js 中的用戶名中查找用戶 ID?)
主站蜘蛛池模板: 上海软件开发-上海软件公司-软件外包-企业软件定制开发公司-咏熠科技 | 礼仪庆典公司,礼仪策划公司,庆典公司,演出公司,演艺公司,年会酒会,生日寿宴,动工仪式,开工仪式,奠基典礼,商务会议,竣工落成,乔迁揭牌,签约启动-东莞市开门红文化传媒有限公司 | 注塑模具_塑料模具_塑胶模具_范仕达【官网】_东莞模具设计与制造加工厂家 | 酶联免疫分析仪-多管旋涡混合仪|混合器-莱普特科学仪器(北京)有限公司 | 仪器仪表网 - 永久免费的b2b电子商务平台| 罗茨真空机组,立式无油往复真空泵,2BV水环真空泵-力侨真空科技 | 电气控制系统集成商-PLC控制柜变频控制柜-非标自动化定制-电气控制柜成套-NIDEC CT变频器-威肯自动化控制 | 座椅式升降机_无障碍升降平台_残疾人升降平台-南京明顺机械设备有限公司 | 施工电梯_齿条货梯_烟囱电梯_物料提升机-河南大诚机械制造有限公司 | Q361F全焊接球阀,200X减压稳压阀,ZJHP气动单座调节阀-上海戎钛 | 带锯机|木工带锯机圆木推台锯|跑车带锯机|河北茂业机械制造有限公司| | 济南网站建设_济南网站制作_济南网站设计_济南网站建设公司_富库网络旗下模易宝_模板建站 | 【同风运车官网】一站式汽车托运服务平台,验车满意再付款 | 探鸣起名网-品牌起名-英文商标起名-公司命名-企业取名包满意 | 24位ADC|8位MCU-芯易德科技有限公司 | 胶辊硫化罐_胶鞋硫化罐_硫化罐厂家-山东鑫泰鑫智能装备有限公司 意大利Frascold/富士豪压缩机_富士豪半封闭压缩机_富士豪活塞压缩机_富士豪螺杆压缩机 | 福建自考_福建自学考试网 | 大型冰雕-景区冰雕展制作公司,3D创意设计源头厂家-[赛北冰雕] | WTB5光栅尺-JIE WILL磁栅尺-B60数显表-常州中崴机电科技有限公司 | 液压升降平台_剪叉式液压/导轨式升降机_传菜机定做「宁波日腾升降机厂家」 | 煤矿支护网片_矿用勾花菱形网_缝管式_管缝式锚杆-邯郸市永年区志涛工矿配件有限公司 | 标准件-非标紧固件-不锈钢螺栓-非标不锈钢螺丝-非标螺母厂家-三角牙锁紧自攻-南京宝宇标准件有限公司 | 变色龙PPT-国内原创PPT模板交易平台 - PPT贰零 - 西安聚讯网络科技有限公司 | 陕西安玻璃自动感应门-自动重叠门-磁悬浮平开门厂家【捷申达门业】 | 高速混合机_锂电混合机_VC高效混合机-无锡鑫海干燥粉体设备有限公司 | 污水/卧式/潜水/钻井/矿用/大型/小型/泥浆泵,价格,参数,型号,厂家 - 安平县鼎千泵业制造厂 | 东莞压铸厂_精密压铸_锌合金压铸_铝合金压铸_压铸件加工_东莞祥宇金属制品 | 厂厂乐-汇聚海量采购信息的B2B微营销平台-厂厂乐官网 | 冰晶石|碱性嫩黄闪蒸干燥机-有机垃圾烘干设备-草酸钙盘式干燥机-常州市宝康干燥 | 「银杏树」银杏树行情价格_银杏树种植_山东程锦园林 | 丝印油墨_水性油墨_环保油墨油漆厂家_37国际化工 | 电采暖锅炉_超低温空气源热泵_空气源热水器-鑫鲁禹电锅炉空气能热泵厂家 | 科研ELISA试剂盒,酶联免疫检测试剂盒,昆虫_植物ELISA酶免试剂盒-上海仁捷生物科技有限公司 | 商用绞肉机-熟肉切片机-冻肉切丁机-猪肉开条机 - 广州市正盈机械设备有限公司 | 隔爆型防爆端子分线箱_防爆空气开关箱|依客思 | 焊缝跟踪系统_激光位移传感器_激光焊缝跟踪传感器-创想智控 | 低温等离子清洗机(双气路进口)-嘉润万丰 | 假肢-假肢价格-假肢厂家-河南假肢-郑州市力康假肢矫形器有限公司 | pbootcms网站模板|织梦模板|网站源码|jquery建站特效-html5模板网 | 阀门智能定位器_电液动执行器_气动执行机构-赫尔法流体技术(北京)有限公司 | 锌合金压铸-铝合金压铸厂-压铸模具-冷挤压-誉格精密压铸 |