我正在尝试使用CBC河豚加密与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();
}
下面是我得到的错误:
'初始化向量的长度必须与块大小相同'
正如我上面提到的,我对一个客户的最后期限很陌生,我尽我最大的努力去阅读和理解它,但我真的在这里碰壁,不知道从哪里开始。这对你们中的一些人来说可能是很明显的事情,所以请宽容一点:)
编辑:有一件事要提的是,我试图将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();
}