簡體   English   中英

如何使用非十六進制 IV 進行 CBC Blowfish 加密

[英]How to CBC Blowfish encrypt with non hex IV

我對加密的工作原理相當陌生,但是,有人要求我使用 Blowfish CBC 加密來加密和解密一個值。 現在我在這上面花了相當長的時間,我認為我做對了,直到他們把他們希望我使用的 IV 寄給我,這讓我很困惑。 我的印象是 IV 必須是十六進制值,但是,他們向我發送了一個看起來類似於: cl5PxDOt的 IV。

我設法生成的代碼如下所示:

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);
    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();
}

我希望有人能告訴我如何修改上面的代碼以使用以下示例 iv: cl5PxDOt

編輯:這是我嘗試過的,但每次我都會pad block corrupted

var newIv = "cl5PxDOt";
var newByteIv = newIv.ToArray().Select(Convert.ToByte).ToArray();
var newDecryptedValue = CbcBlowfishDecrypt(ospToken, key, newByteIv);
//var newDecryptedValue = Encoding.UTF8.GetBytes(newIv) -- Also tried this, but same result.

在 Org.BouncyCastle.Crypto.Paddings.Pkcs7Padding.PadCount(Byte[] 輸入)
在 Org.BouncyCastle.Crypto.Paddings.PaddedBufferedBlockCipher.DoFinal(Byte[] output, Int32 outOff)

如果密鑰不是 Base64 解碼但 UTF-8 編碼,即在CbcBlowfishDecrypt() (和CbcBlowfishEncrypt() )中,則可以解密密文

KeyParameter keyParam = new KeyParameter(Convert.FromBase64String(key));

將被替換為

KeyParameter keyParam = new KeyParameter(Encoding.UTF8.GetBytes(key));

UTF-8 編碼的密鑰長度為 448 位,因此是一個有效密鑰( Blowfish定義為 32 到 448 位之間的密鑰長度)。

IV 可以是 UTF-8 以模擬方式編碼,而不是當前的實現。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM