Encrypt and Decrypt data using Blowfish/CBC/PKCS5Padding

三世轮回 提交于 2019-12-14 03:48:52

问题


A legacy application (ColdFusion) is using Blowfish/CBC/PKCS5Padding encryption. How can we encrypt and decrypt this data using the BouncyCastle lib?

For other fields, encrypted in ColdFusion using this:

encrypt( data, key, 'BLOWFISH', 'HEX')

We use this code

BlowfishEngine engine = new BlowfishEngine();
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(engine);
cipher.Init(false, new KeyParameter(Convert.FromBase64String(keyString)));
byte[] out1 = Hex.Decode(name);
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);

The problem is how to decrypt something, encrypted in ColdFusion like this:

encrypt( data, Key, "Blowfish/CBC/PKCS5Padding", "base64", IV )

回答1:


I figured it out. In case anyone is interested:

        BlowfishEngine engine = new BlowfishEngine();
        var cipher = new PaddedBufferedBlockCipher( new CbcBlockCipher( engine ), new Pkcs7Padding() );
        StringBuilder result = new StringBuilder();
        cipher.Init( false, new ParametersWithIV( new KeyParameter( Convert.FromBase64String( keyString ) ), System.Text.Encoding.ASCII.GetBytes( IV ) ) );
        byte[] out1 = Convert.FromBase64String( name );
        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 );


来源:https://stackoverflow.com/questions/53772039/encrypt-and-decrypt-data-using-blowfish-cbc-pkcs5padding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!