Reputation: 156
I have a method in C++ created for AES 256 Encryption which works:
void AES_Encrypt(unsigned char* message, unsigned char* expandedKey)
{
unsigned char numOfRounds = 13;
unsigned char* state = new unsigned char[16];
AddRoundKey(state, expandedKey);
for (int i = 0; i < numOfRounds; i++)
{
//bla bla
AddRoundKey(state, expandedKey + (16 * (i + 1)));
}
// bla bla
AddRoundKey(state, expandedKey + 224);
}
and
void AddRoundKey(unsigned char *state, unsigned char* roundKey)
{
for (int i = 0; i < 16; i++)
state[i] = state[i] ^ roundKey[i];
}
but when I translate it into C#:
private void AddRoundKey(byte[] state, byte[] roundKey)
{
for (int i = 0; i < 16; i++)
state[i] = (byte)(state[i] ^ roundKey[i]);
}
I get errors on the exact translated function:
AddRoundKey(state, expandedKey + (16 * (i + 1)));
AddRoundKey(state, expandedKey + 224);
How could I translate void AddRoundKey(unsigned char *state, unsigned char* roundKey)
correctly in this case?
Upvotes: 1
Views: 234
Reputation: 352
Simplest way will be passing the offset:
void AddRoundKey(byte[] state, byte[] roundKey, int offset)
{
for (int i = 0; i < 16; i++)
state[i] = (byte)(state[i] ^ roundKey[i + offset]);
}
then you call it:
AddRoundKey(state, expandedKey, (16 * (i + 1)));
...
AddRoundKey(state, expandedKey, 244);
Other
You can use unsafe keyword (notice to enable unsafe in your project settings)
unsafe void AddRoundKey(byte* state, byte* roundKey)
{
for (int i = 0; i < 16; i++)
state[i] = (byte)(state[i] ^ roundKey[i]);
}
then use fixed when you call it:
fixed (byte* state_pointer = state, expandedKey_pointer = expandedKey)
{
AddRoundKey(state_pointer, expandedKey_pointer + 244);
}
when state and expandedKey is byte[].
Upvotes: 1