描述
对方需要传输密文调我方服务,解密处理, 但我的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;
...
北京的同事?你是部分乱码还是全部乱码