1

I've 4 bytes of data and need an 8 bytes array for a security operation. I should produce these 8 bytes form the 4 bytes byte array and this should be reproducible.

I was thinking of using exact byte array and adding 4 extra bytes and fill them with AND, OR, XOR... of the initial array in a known sequence. I'm not sure if it's a good idea. I just need an 8 byte array from this 4 bytes and the operation should be reproducible (same 8 bytes with same given 4 bytes). Please give an example in C#

Xaqron
  • 29,931
  • 42
  • 140
  • 205
  • I don't know if I'm missing the point here, but surely the best thing to do would just be to pad with 4 empty bytes? It's valid and reproducible. – Simon P Stevens Nov 19 '10 at 08:32
  • Lots of ways to do that.... just copy it twice, pad with 0s, copy and reverse, hash it,... can even mix the order so its not always the last or first 4 bytes that are significant. – mpen Nov 19 '10 at 08:35
  • Is the code required to work on big-endian systems too? – CodesInChaos Nov 19 '10 at 09:13

4 Answers4

4

Why not just pad the existing 4 bytes with another 4 bytes of zeroes? Or repeat the original 4 bytes. For example:

static byte[] Pad(byte[] input)
{
    // Alternatively use Array.Resize
    byte[] output = new byte[input.Length + 4];
    Buffer.BlockCopy(input, 0, output, 0, input.Length);
    return output;
}

static byte[] Repeat(byte[] input)
{
    byte[] output = new byte[input.Length * 2];
    Buffer.BlockCopy(input, 0, output, 0, input.Length);
    Buffer.BlockCopy(input, 0, output, input.Length, input.Length);
    return output;
}

Both of these fulfil your original criteria, I believe... but I suspect you're looking for something else. If that's the case, you need to be explicit about what you need.

EDIT: As I've said in the comments, you're basically not adding any real security here - padding will make that clearer, IMO. On the other hand, if you do want some security-through-obscurity, you could find a random number generator that allows seeding, and use that as a starting point. For example:

// Don't use this - see below. Just the concept...
int seed = BitConverter.ToInt32(input, 0); // TODO: Cope with endianness
Random rng = new Random(seed);
byte[] output = new byte[8];
Buffer.BlockCopy(input, 0, output, 0, 4);
for (int i = 4; i < 8; i++) {
    output[i] = (byte) rng.Next(256);
}

Now, the reason I've got the comment above is that you probably need an algorithm which is guaranteed not to change between versions of .NET. Find code to something like the Mersenne Twister, for exmaple.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

There are multiple methods of doing padding for block ciphers.

This Wikipedia article covers some of the more accepted solutions: http://en.wikipedia.org/wiki/Padding_(cryptography)#Padding_methods

Barring any other considerations, I would use PKCS#7 padding.

Yann Ramin
  • 32,895
  • 3
  • 59
  • 82
1

How about

bytes.Concat(bytes)
Paul Creasey
  • 28,321
  • 10
  • 54
  • 90
1

I would be very careful. If a security operation requires 64 bits worth of data it is probably because it requires that much data. If you create your 64 bits from 32 bits with a known reproducible formula you will still only have 32 bits worth of data.

If the security is not affected by the data you have you can just fill the the remaining four bytes with ones or zeros. But you should really try to get 8 bytes of "real" data.

Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108
  • I agree but project limitations force me to do so (at least this is a dynamic solution in my case) rather than hard code an 8 byte array in my code. In fact 4 bytes is enough but I was a little pessimistic about it. – Xaqron Nov 19 '10 at 08:43