使用Blowfish / CBC / PKCS5Padding加密和解密数据 [英] Encrypt and Decrypt data using Blowfish/CBC/PKCS5Padding

查看:178
本文介绍了使用Blowfish / CBC / PKCS5Padding加密和解密数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

旧版应用程序(ColdFusion)使用 Blowfish / CBC / PKCS5Padding 加密。我们如何使用BouncyCastle lib加密和解密这些数据?

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

对于其他字段,使用以下方法在ColdFusion中加密:

For other fields, encrypted in ColdFusion using this:

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

我们使用此代码

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

问题是如何解密某些东西,在ColdFusion中加密,如下所示:

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

这篇关于使用Blowfish / CBC / PKCS5Padding加密和解密数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆