gpt4 book ai didi

javascript - 在 NodeJS 中使用 Blowfish 加密字符串

转载 作者:行者123 更新时间:2023-11-29 17:53:29 28 4
gpt4 key购买 nike

我需要加密一个字符串,但我几乎得到了我想要的输出,我在网上看到它与 padding 和最后的 iv_vector 有关,以完成剩余的 8 个字节,使其与 txtToEncrypt 的长度相同。

我正在使用这个库 https://github.com/agorlov/javascript-blowfish

// function in Java that I need
// javax.crypto.Cipher.getInstance("Blowfish/CBC/NoPadding").doFinal("spamshog")


var iv_vector = "2278dc9wf_178703";
var txtToEncrypt = "spamshog";
var bf = new Blowfish("spamshog", "cbc");

var encrypted = bf.encrypt(txtToEncrypt, iv_vector);

console.log(bf.base64Encode(encrypted));

Actual output: /z9/n0FzBJQ=
What I need: /z9/n0FzBJRGS6nPXso5TQ==

If anyone has any clue please let me know. I searched all over Google all day.

最佳答案

最后,这里是如何使用 Blowfish 在 NodeJS 中加密字符串

// Module crypto already included in NodeJS
var crypto = require('crypto');

var iv = "spamshog";
var key = "spamshog";
var text = "2278dc9wf_178703";
var decipher = crypto.createCipheriv('bf-cbc', key, iv);
decipher.setAutoPadding(false);
var encrypted = decipher.update(text, 'utf-8', "base64");
encrypted += decipher.final('base64');

console.log(encrypted);

Returns: /z9/n0FzBJRGS6nPXso5TQ==

关于javascript - 在 NodeJS 中使用 Blowfish 加密字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41248700/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com