简体   繁体   中英

Encrypt and Decrypt data using Blowfish/CBC/PKCS5Padding

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 )

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM