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

发布于 2022-09-13 00:36:25 字数 3690 浏览 48 评论 0

描述

对方需要传输密文调我方服务,解密处理, 但我的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;
        ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文