繁体   English   中英

初始化密码时出错

[英]Error while initializing Cipher

我有加密字符串的功能:

BASE64Decoder decoder = new BASE64Decoder();
BASE64Encoder encoder = new BASE64Encoder();

public String encryptValueWithBlowfish(String data, String secretKey) {

    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    try {
        SecretKeySpec key = new SecretKeySpec(decoder.decodeBuffer(secretKey), "Blowfish");
        Cipher cipher = Cipher.getInstance("Blowfish/CBC/NoPadding", "BC");
        String iv = "\0\0\0\0\0\0\0\0";
        IvParameterSpec ivs = new IvParameterSpec(iv.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, key, ivs);
        MessageDigest sha = MessageDigest.getInstance("SHA-1");
        return encoder.encode(sha.digest(cipher.doFinal(decoder.decodeBuffer(data))));
    } catch (Exception e) {
        lg.info("Failed to encryptValueWithBlowfish: " + e.getMessage());
        return "";
    }
}

cipher.init(Cipher.ENCRYPT_MODE, key, ivs); 出现异常"Unsupported keysize or algorithm parameters" 该代码可以在另一台Linux机器上正常运行。 两种情况下传递的参数都相同。 我不擅长加密货币。 可能是什么问题?

可能是什么问题?

好吧,您在这里使用默认的字符编码: iv.getBytes() -从来都不是一个好的开始。 也许这两个不同的机器具有不同的默认编码。

如果要创建全零和特定大小的字节数组,为什么不使用:

IvParameterSpec ivs = new IvParameterSpec(new byte[8]);

或如果需要16字节IV,则使用16。

目前尚不清楚此处使用的是哪种decoder -但您要使用它两次,如果使用的是默认字符编码,则可能因机器而异。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM