views:

2294

answers:

2

Hello,

In C#,I'm using Blowfish.NET 2.1.3's BlowfishECB.cs file(can be found here)

In C++,It's unknown,but it is similiar.

In C++,the Initialize(blowfish) procedure is the following:

void cBlowFish::Initialize(BYTE key[], int keybytes)

In C#,the Initialize(blowfish) procedure is the same

public void Initialize(byte[] key, int ofs, int len)

This is the problem:

This is how the key is initialized in C++

DWORD keyArray[2] = {0}; //declaration
...some code
blowfish.Initialize((LPBYTE)keyArray, 8);

As you see,the key is an array of two DWORDS,which is 8 bytes total.

In C# I declare it like that,but I get an error

BlowfishECB blowfish = new BlowfishECB();
UInt32[] keyarray = new UInt32[2];
..some code
blowfish.Initialize(keyarray, 0, 8);

The error is:

Argument '1': cannot convert from 'uint[]' to 'byte[]'

What am I doing wrong?

Thanks in advance!

+4  A: 

You can use BitConverter to get the bytes from a UInt32.


To do this, you'll need to convert each element in a loop. I would do something like:

private byte[] ConvertFromUInt32Array(UInt32[] array)
{
    List<byte> results = new List<byte>();
    foreach(UInt32 value in array)
    {
        byte[] converted = BitConverter.GetBytes(value);
        results.AddRange(converted);
    }
    return results.ToArray();
}

To go back:

private UInt32[] ConvertFromByteArray(byte[] array)
{
    List<UInt32> results = new List<UInt32>();
    for(int i=0;i<array.Length;i += 4)
    {
        byte[] temp = new byte[4];
        for (int j=0;j<4;++j)
            temp[j] = array[i+j];
        results.Add(BitConverter.ToUInt32(temp);
    }
    return results.ToArray();
}

Reed Copsey
@Reed Copsey,I tried it,but It gets even worse: Argument '1': cannot convert from 'uint[]' to 'double'It's an array of UInt,not Uint.
John
JaredPar's option is a nice option if you're using C# 3 and linq.
Reed Copsey
I'm using C# with C# 2008 Express SP1 ,but I get error there too.
John
@John: The code in my edit should work... what error are you receiving?
Reed Copsey
@Reed Copsey, Thanks! I'm trying it right now,but can you add a function that converts from byte[] to Uint[] aswell? The same 8 bytes should be converted into a UInt[] array with 2 members as its in the source code.
John
@John: That should do it.
Reed Copsey
+4  A: 

If you are using VS2008 or C# 3.5, try the following LINQ + BitConverter solution

var converted = 
  keyArray
    .Select(x => BitConverter.GetBytes(x))
    .SelectMany(x => x)
    .ToArray();

Breaking this down

  • The Select converts every UInt32 into a byte[]. The result is an IEnumerable<byte[]>
  • The SelectMany calls flattes the IEnumerable<byte[]> to IEnumerable<byte>
  • ToArray() simply converts the enumerable into an array

EDIT Non LINQ solution

List<byte> list = new List<byte>();
foreach ( uint32 k in keyArray) {
  list.AddRange(BitConverter.GetBytes(k));
}
return list.ToArray();
JaredPar
Nice, elegant version of the code I pasted.
Reed Copsey
@JaredPar I get this error: 'System.Array' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?) //end error
John
@John, are you using VS2005 or VS2008? If 2008, make sure you add using System.Linq to the top of the file. If not, I'll update for a VS2005 solution
JaredPar
@John: You can use my code (which does the same), or reference System.Linq and use C# 3 / VS 2008.
Reed Copsey
@Reed Copsey,I'll try your code,but I might need a function that converts byte[] to Uint32[] ,because below this code I call blowfish.Encode@JaredPar,I'm very new to C#,my previous language is Delphi and I cannot get used to C#.I'm using C# Express 2008.Could you explain step by step what to do.
John
@John, if you're using C# Expression 2008, either solution should work. If the first one is not working, go to the top of the file and make sure that you have a line saying using System.Linq;
JaredPar
@JaredPar, Thank you too,could you give a solution how to eventually turn those 8 bytes back into an array of Uint32? Non LINQ solution would be better,because im new.
John
@John: I added it to my answer for you.
Reed Copsey
Ok.You both know too much,I digged up both answers.@Reed Copsey - yours is the one I accepted,but I want to Thank JaredPar too.Thank you guys!
John
@Jared - you can make the non-LINQ solution even more concise by using Array.ForEach().
Cheeso