使用 CBC Blowfish Encryption 加密数据不起作用

Encrypt data with CBC Blowfish Encryption not working

我正在尝试使用 CBC blowfish 加密法对 bounycastle 进行加密。这是我无法更改的要求,因此我需要弄清楚它是如何工作的。我完全不熟悉加密,所以任何帮助都会非常感激,因为我有一个截止日期。

这是我目前的情况:

public void Test() {
    var value = "My Test Value";
    var key = "some key"; //56 characters long
    var iv = Encoding.GetBytes("some iv");
    var encryptedValue = CbcBlowfishEncrypt(encryptedText, key, iv);
}

private string CbcBlowfishEncrypt(string strValue, string key, byte[] iv)
{
    byte[] inputBytes = Encoding.UTF8.GetBytes(strValue);
    BlowfishEngine engine = new BlowfishEngine();
    CbcBlockCipher blockCipher = new CbcBlockCipher(engine); //CBC
    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(blockCipher); //Default scheme is PKCS5/PKCS7
    KeyParameter keyParam = new KeyParameter(Convert.FromBase64String(key));
    ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, iv, 0, 8);

    cipher.Init(true, keyParamWithIV);
    byte[] outputBytes = new byte[cipher.GetOutputSize(inputBytes.Length)];
    int length = cipher.ProcessBytes(inputBytes, outputBytes, 0);
    cipher.DoFinal(outputBytes, length);
    var result = BitConverter.ToString(outputBytes).Replace("-", "");
    return result;
}

public static byte[] StringToByteArray(string hex)
{
    return Enumerable.Range(0, hex.Length)
        .Where(x => x % 2 == 0)
        .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
        .ToArray();
}

这是我得到的错误:

'initialisation vector must be the same length as block size'

正如我上面提到的,我对这个有客户截止日期的新手,并且尽我最大的努力阅读并理解其中的很多内容,但我真的在这里碰壁了不知道从这里去哪里。这对你们中的一些人来说可能是显而易见的事情,所以请怜悯:)

编辑:要提到的一件事是,我尝试将 ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, iv, 0, 16); 的 16 更改为 8,它似乎让它通过了。但我对此感到困惑的是我的字节长 16 个字节,而不是 8 个字节。这是因为每个字符占用 2 个字节并且该值表示字符而不是字节吗?

没有人正式发布答案,所以我只是想结束这个问题以帮助其他尝试做同样事情的人。下面是我用来加密和解密值的代码:

function Run()
{
    var key = "enter your key";
    var value = "Enter your test value";
    var iv = StringToByteArray("enter your 16 char hex value");

    var encryptedValue = CbcBlowfishEncrypt(value, key, iv);
    var decryptedValue = CbcBlowfishDecrypt(encryptedValue, key, iv);
}

private string CbcBlowfishEncrypt(string strValue, string key, byte[] iv)
{
    byte[] inputBytes = Encoding.UTF8.GetBytes(strValue);
    BlowfishEngine engine = new BlowfishEngine();
    CbcBlockCipher blockCipher = new CbcBlockCipher(engine);
    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(blockCipher);
    KeyParameter keyParam = new KeyParameter(Convert.FromBase64String(key));
    ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, iv, 0, 8);

    cipher.Init(true, keyParamWithIV);
    byte[] outputBytes = new byte[cipher.GetOutputSize(inputBytes.Length)];
    int length = cipher.ProcessBytes(inputBytes, outputBytes, 0);
    cipher.DoFinal(outputBytes, length); //Do the final block
    var c = BitConverter.ToString(outputBytes);
    string encryptedInput = Convert.ToBase64String(outputBytes);
    var result = BitConverter.ToString(outputBytes).Replace("-", "");
    return result;
}

private string CbcBlowfishDecrypt(string strValue, string key, byte[] iv)
{
    BlowfishEngine engine = new BlowfishEngine();
    CbcBlockCipher blockCipher = new CbcBlockCipher(engine);
    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(blockCipher);
    StringBuilder result = new StringBuilder();
    KeyParameter keyParam = new KeyParameter(Convert.FromBase64String(key));
    ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, iv, 0, 8);

    cipher.Init(false, keyParamWithIV);
    byte[] out1 = Hex.Decode(strValue);
    byte[] out2 = new byte[cipher.GetOutputSize(out1.Length)];
    int len2 = cipher.ProcessBytes(out1, 0, out1.Length, out2, 0);
    cipher.DoFinal(out2, len2);
    return Encoding.UTF8.GetString(out2);
}

public static byte[] StringToByteArray(string hex)
{
    return Enumerable.Range(0, hex.Length)
        .Where(x => x % 2 == 0)
        .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
        .ToArray();
}