Node.js的Blowfish加密默认使用哪种PaddingMethod?

打了个冷颤
  • 19

描述

对方需要传输密文调我方服务,解密处理, 但我的Blowfish密文解密后会出现乱码 {"uid":"dn0001176"}

尝试

这是对方服务的加密示例:

const crypto = require('crypto');
const security = {
    blowfishEncode: (options, encryptStr) => {
        var key = new Buffer(options.key);
        var iv = new Buffer(options.iv);
        var alg = "blowfish";
        if (options.alg) {
            alg = options.alg;
        }

        var encryptStrBuf = new Buffer(encryptStr);
        if (encryptStrBuf.length % 8 != 0) {
            var length = 0;
            if (encryptStrBuf.length < 8) {
                length = 8 - encryptStrBuf.length;
            } else {
                length = 8 - encryptStrBuf.length % 8;
            }
            for (var i = 0; i < length; i++) {
                encryptStr += ' ';
            }
        }
        var cipher = crypto.createCipheriv(alg, key, iv);
        cipher.setAutoPadding(false);
        var encryptRes = cipher.update(new Buffer(encryptStr), 'utf8', 'base64');
        encryptRes += cipher.final('base64');
        return encryptRes;
    },
    blowfishDecode: (options, encryptStr) => {
        var key = new Buffer(options.key);
        var iv = new Buffer(options.iv);
        var alg = "blowfish";
        if (options.alg) {
            alg = options.alg;
        }

        var decipher = crypto.createDecipheriv(alg, key, iv);
        decipher.setAutoPadding(false);
        var encryptRes = decipher.update(encryptStr, 'base64', 'utf8');
        encryptRes += decipher.final('utf8');
        return encryptRes;

    },
};

const blowOption = {
    key: 'keykeykey',
    iv: 'iviviv'
};


// 加密
let userName = '000hzy';
let ciphertext = '{"uid":"' + userName + '"}';
ciphertext = security.blowfishEncode(blowOption, ciphertext);
console.log(ciphertext);

// 解密
// let ciphertext = '9FdLNUd86FUxyyjbYI8I7t7ZMtIInOga';
console.log(security.blowfishDecode(blowOption, ciphertext))

补充

这是我这边java版本用的PaddingMethods


    /**
     * All supported methods
     */
    public enum Method {
        BLOWFISH_ECB_NoPadding("Blowfish/ECB/NoPadding"),
        BLOWFISH_ECB_PKCS5Padding("Blowfish/ECB/PKCS5Padding"),
        BLOWFISH_ECB_PKCS7Padding("Blowfish/ECB/PKCS7Padding"),
        BLOWFISH_ECB_ISO10126Padding("Blowfish/ECB/ISO10126Padding"),

        BLOWFISH_CBC_NoPadding("Blowfish/CBC/NoPadding"),
        BLOWFISH_CBC_PKCS5Padding("Blowfish/CBC/PKCS5Padding"),
        BLOWFISH_CBC_PKCS7Padding("Blowfish/CBC/PKCS7Padding"),
        BLOWFISH_CBC_ISO10126Padding("Blowfish/CBC/ISO10126Padding"),

        BLOWFISH_CTR_NoPadding("Blowfish/CTR/NoPadding"),
        BLOWFISH_CTR_PKCS5Padding("Blowfish/CTR/PKCS5Padding"),
        BLOWFISH_CTR_PKCS7Padding("Blowfish/CTR/PKCS7Padding"),
        BLOWFISH_CTR_ISO10126Padding("Blowfish/CTR/ISO10126Padding"),

        BLOWFISH_CTS_NoPadding("Blowfish/CTS/NoPadding"),
        BLOWFISH_CTS_PKCS5Padding("Blowfish/CTS/PKCS5Padding"),
        BLOWFISH_CTS_PKCS7Padding("Blowfish/CTS/PKCS7Padding"),
        BLOWFISH_CTS_ISO10126Padding("Blowfish/CTS/ISO10126Padding"),

        BLOWFISH_CFB_NoPadding("Blowfish/CFB/NoPadding"),
        BLOWFISH_CFB_PKCS5Padding("Blowfish/CFB/PKCS5Padding"),
        BLOWFISH_CFB_PKCS7Padding("Blowfish/CFB/PKCS7Padding"),
        BLOWFISH_CFB_ISO10126Padding("Blowfish/CFB/ISO10126Padding"),

        BLOWFISH_OFB_NoPadding("Blowfish/OFB/NoPadding"),
        BLOWFISH_OFB_PKCS5Padding("Blowfish/OFB/PKCS5Padding"),
        BLOWFISH_OFB_PKCS7Padding("Blowfish/OFB/PKCS7Padding"),
        BLOWFISH_OFB_ISO10126Padding("Blowfish/OFB/ISO10126Padding");

        private final String method;
        ...
回复
阅读 481
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏