I am trying to mimic coldfusion Blowfish/Hex encryption using BouncyCastle for ASP.NET MVC. I was able to successfully decrypt a string encrypted in CF using the steps laid out in this link: Encrypt and Decrypt data using Blowfish/CBC/PKCS5Padding
Other than adding a \0 to the end of the 7 character string, it works great.
I am not trying to utilize Blowfish/CBC/PKCS5Padding, just Blowfish/Hex. I am unable to get the reverse (encryption) working. Below is the code I have so far
I have tried using manual padding of =, as well as ignoring. However, even when using the additional character, it still doesn't sync.
Decryption (does work):
string keyString = "TEST";
BlowfishEngine engine = new BlowfishEngine();
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(engine);
cipher.Init(false, new KeyParameter(Convert.FromBase64String(keyString)));
byte[] out1 = Hex.Decode(hexstring);
byte[] out2 = new byte[cipher.GetOutputSize(out1.Length)];
int len2 = cipher.ProcessBytes(out1, 0, out1.Length, out2, 0);
cipher.DoFinal(out2, len2);
string myval = Encoding.UTF8.GetString(out2);
Encryption (doesn't work):
string keyString = "TEST";
string stringToEncrypt = "0123456";
stringToEncrypt = stringToEncrypt + "=";
BlowfishEngine engine2 = new BlowfishEngine();
PaddedBufferedBlockCipher cipher2 = new PaddedBufferedBlockCipher(engine2);
cipher2.Init(true, new KeyParameter(Convert.FromBase64String(keyString)));
byte[] inB = Hex.Encode(Convert.FromBase64String(stringToEncrypt));
byte[] outB = new byte[cipher2.GetOutputSize(inB.Length)];
int len1 = cipher2.ProcessBytes(inB, 0, inB.Length, outB, 0);
cipher2.DoFinal(outB, len1);
var myval2 = BitConverter.ToString(outB).Replace("-", "");
"Doesn't work" meaning - it returns an encrypted string that in no way mirrors the string encrypted by CF. On top of that, the returned string, when decrypted using the above method, does not match with what was originally entered (using the .NET/BouncyCastle encryption)