2

我有一个应用程序,它最初是用 Borland C++ 编写的,并使用了在 TurboPower LockBox 组件中实现的 Blowfish 算法。

此应用程序现已移植到 C#。目前我调用了一个使用该算法的 Borland C++ dll。但是,在 64 位操作系统上运行应用程序时,每当尝试使用此 dll 时都会出错。如果我将应用程序编译为 32 位,一切正常,但我们希望让此应用程序作为 64 位应用程序工作。据我所知,这意味着我需要一个像 C++ 一样工作的 .Net Blowfish 算法。

我找到了 Blowfish.Net,它看起来很有希望。但是,当我使用相同的密钥和文本时,加密的结果不匹配。我确实发现 C++ dll 使用 BlowfishECB 算法。它还将结果转换为 Base 64,我也这样做了。

对此的任何帮助将不胜感激。这是 C# 中的一些测试代码。

//Convert the key to a byte array.  In C++ the key was 16 bytes long
byte[] _key = new byte[16];
Array.Clear(_key, 0, _key.Length);
var pwdBytes = System.Text.Encoding.Default.GetBytes(LicEncryptKey);
int max = Math.Min(16, pwdBytes.Length);
Array.Copy(pwdBytes, _key, max);

//Convert the string to a byte[] and pad it to to the 8 byte block size
var decrypted = System.Text.Encoding.ASCII.GetBytes(originalString);
var blowfish = new BlowfishECB();
blowfish.Initialize(_key, 0, _key.Length);
int arraySize = decrypted.Length;
int diff = arraySize%BlowfishECB.BLOCK_SIZE;
if (diff != 0)
{
    arraySize += (BlowfishECB.BLOCK_SIZE - diff);
}        
var decryptedBytes = new Byte[arraySize];
Array.Clear(decryptedBytes, 0, decryptedBytes.Length);            
Array.Copy(decrypted, decryptedBytes, decrypted.Length);    
//Prepare the byte array for the encrypted string
var encryptedBytes = new Byte[decryptedBytes.Length];
Array.Clear(encryptedBytes, 0, encryptedBytes.Length);
blowfish.Encrypt(decryptedBytes, 0, encryptedBytes, 0, decryptedBytes.Length);
//Convert to Base64
string result = Convert.ToBase64String(encryptedBytes);
4

1 回答 1

2

It won't compatible with your TurboPower LockBox data.

I'd suggest that you provide a utility to do the data migration by decoding using LockBox in C++ (32-bit), outputting to temp files/tables and re-encoding using Blowfish.Net and C# (64-bit).

This data migration is done once before any upgrade to the .NET version, then it's all compatible with it.

Since you're changing the format: you could also change the format and omit the Base64 conversion by storing binary files/BLOBs, other ideas may also be useful like applying multiple encryptions, or replacing Blowfish by something else.

于 2009-05-28T21:05:41.263 回答